修改后脚本:http://blog.csdn.net/u010841622/article/details/41857121
using UnityEngine; public class MouseFollowRotation : MonoBehaviour { public Transform target; public float xSpeed=200, ySpeed=200, mSpeed=10; public float yMinLimit=0, yMaxLimit=0; public float distance=0.35f, minDistance=0.35f, maxDistance=0.35f; //bool needDamping = false; public bool needDamping =true; float damping = 3.0f; public float x = 0.0f; public float y = 0.0f; public int leftMax = 95; public int rightMax = 265; public void SetTarget( GameObject go ) { target = go.transform; } // Use this for initialization void Start () { Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; } // Update is called once per frame void LateUpdate () { if (target) { //use the light button of mouse to rotate the camera if( Input.GetMouseButton(1) ) { if (x >= leftMax && x <= rightMax) { x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; } else { if (x < leftMax) x = leftMax; else if (x > rightMax) x = rightMax; } //x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; //y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y = ClampAngle(y, yMinLimit, yMaxLimit); //print(Input.GetAxis("Mouse X")); //print( Input.GetAxis("Mouse Y")); print("x : " + x); print("y : " + y); } distance -= Input.GetAxis("Mouse ScrollWheel")*mSpeed; distance = Mathf.Clamp(distance, 0.35f, 0.35f); Quaternion rotation = Quaternion.Euler(y, x, 0.0f); Vector3 disVector = new Vector3( 0.0f, 0.0f, -distance ); Vector3 position = rotation * disVector + target.position; //adjust the camera if( needDamping ) { transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime*damping); transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime*damping); } else { transform.rotation = rotation; transform.position = position; } } } static float ClampAngle (float angle, float min, float max) { if (angle < -90) angle += 90; if (angle > 45) angle -= 45; return Mathf.Clamp (angle, min, max); } }
上面的代码被改的有些乱,重新整理了下:
using UnityEngine; using System.Collections; public class MouseFollowRotation : MonoBehaviour { public Transform target; //调整 速度 public float xSpeed = 50f, ySpeed = 50f, mSpeed = 10f; //调整 转动角度 public float x = 0.0f, xMinLimit = 0f, xMaxLimit = 360f; //调整 高度 public float y = 0.0f, yMinLimit = 0f, yMaxLimit = 0f; //调整 距离 public float distance = 0.3f, minDistance = 0.3f, maxDistance = 0.3f; public bool needDamping = true; float damping = 3.0f;//设置 阻尼 public void SetTarget( GameObject go ) { target = go.transform; } // Use this for initialization void Start () { Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; } // Update is called once per frame void LateUpdate () { if (target) { //当按下了鼠标右键 if (Input.GetMouseButton(1)) { x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; x = ClampX(x, xMinLimit, xMaxLimit); y = ClampAngle(y, yMinLimit, yMaxLimit); } distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed; distance = Mathf.Clamp(distance, minDistance, maxDistance); //Camera.main.fieldOfView = distance;//设置主摄像机的y Quaternion rotation = Quaternion.Euler(y, x, 0.0f); Vector3 disVector = new Vector3( 0.0f, 0.0f, -distance ); Vector3 position = rotation * disVector + target.position; //adjust the camera if (needDamping) { transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping); transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping); } else { transform.rotation = rotation; transform.position = position; } } } static float ClampAngle (float angle, float min, float max) { if (angle < -90) angle += 90; if (angle > 45) angle -= 45; return Mathf.Clamp (angle, min, max); } static float ClampX (float x, float min, float max) { if (x < 0) x = 360; if (x > 360) x = 0; return Mathf.Clamp (x, min, max); } }