[脚本] 改自相机的MouseOrbit脚本,增加了相机距对象的高度,鼠标滚轮操作还有鼠标右键移动相机操作

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse OrbitC")]
public class MouseOrbitC : MonoBehaviour {

	// Use this for initialization
	public Transform target;
	public float distance = 10.0f;//相机距对象距离
	public float height = 10.0f; //相机距对象高度
	
	public float Maxdistance = 40.0f; //相机距对象最大距离
	public float Mindistance = 10.0f;

        public float xSpeed = 250.0f;
        public float ySpeed = 120.0f;
	public float mousedown_Movespeen = 10.0f;

        public int yMinLimit = -20;
        public int yMaxLimit = 80;
	
	public float WheelSpeed = 5.0f;  //鼠标滚轮控制相机镜头移动速度

        private float x = 0.0f;
        private float y = 0.0f;
	private float scale = 0.0f;  //距离和高度比,当鼠标滚轮操作,距离变化,要据这个值求出相应的高度变化
	
	private bool mousedown = false; //判断鼠标右键是否按下
	private float startmouse = 0.0f; //鼠标右键按下时,鼠标的点位置
	
	//private intital intitalScript;
	//private tankrotation tankrotationScript;
	
	//private float wheeeldistance = 0.0f;
	void Start () {
	  
		 Vector3 angles = transform.eulerAngles;
                 x = angles.y;
                 y = angles.x;
		
		 scale = distance / height ;
//		 intitalScript = gameObject.GetComponent<intital>();
//		 tankrotationScript = target.gameObject.GetComponent<tankrotation>();

	// Make the rigid body not change rotation
     	         if (rigidbody){
		    rigidbody.freezeRotation = true;
		 }
	}

	
	// Update is called once per frame
	void Update () {
	
	}
	void LateUpdate(){
		if(Input.GetButtonDown("RightMouse")){ //检测是否按下鼠标右键 RightMouse是在InputManager里面设定的,右键值为mouse 1
			mousedown = true;
			Debug.Log("mouseRight");
//			intitalScript.enabled =  false;
//			tankrotationScript.enabled = false;
//			if(startmouse ==0.0f){
//			   startmouse = Input.mousePosition.x;
//			}
//			float mouse_md = Input.mousePosition.x - startmouse;
//		        Debug.Log(mouse_md+"");
//			//if(mouse_md >=0){
//				transform.Rotate(transform.up * mouse_md *Time.deltaTime);
//			startmouse = Input.mousePosition.x;
			//}
		}
		if(mousedown){ //当鼠标右键被按下
			if(startmouse ==0.0f){ //第一次被按下,对鼠标的初始位置进行初始化
			   startmouse = Input.mousePosition.x;
			}
		 	float mouse_md = Input.mousePosition.x - startmouse; //取得鼠标右键按下时移动的位移值
		        Debug.Log(mouse_md+"");
			//if(mouse_md >=0){
			transform.RotateAround(target.position,Vector3.up,mouse_md * mousedown_Movespeen * Time.deltaTime);//根据位移值正负和大小对摄相机对行旋转
			//transform.Rotate(transform.up * mouse_md * mousedown_Movespeen * Time.deltaTime);
			startmouse = Input.mousePosition.x;//将鼠标初始位置设为此帧鼠标位置
		}
		if(Input.GetButtonUp("RightMouse")){//判断当鼠标右键松开
			mousedown = false;
//			intitalScript.enabled = true;
//			tankrotationScript.enabled = true;
			Debug.Log("hello");
		}
		if (target&&!mousedown) { //当target存在且鼠标没有按住右键
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
		
		distance -= Input.GetAxis("Mouse ScrollWheel") * WheelSpeed; //当滚轮操作,对距离值进行改变
		distance = Mathf.Clamp(distance,Mindistance,Maxdistance);	
		height = distance / scale;	
 		
 		y = ClampAngle(y, yMinLimit, yMaxLimit);
 		       
                Quaternion rotation = Quaternion.Euler(y, x, 0);
                Vector3 position = rotation * new Vector3(0.0f, height, -distance) + target.position;
        
                transform.rotation = rotation;
                transform.position = position;
        }
    }
	
	static float ClampAngle (float angle, float min,float max) {
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);
    }
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值