using UnityEngine; using System.Collections; public class mouseRotateAndScale : MonoBehaviour { GameObject car; //public GameObject light; Vector2 p1, p2;//用來記錄滑鼠的位置,以便計算旋轉幅度 // 滑鼠中間鍵 int MouseWheelSensitivity = 5; int MouseZoomMin = 18; int MouseZoomMax = 90; float normalDistance = 60; // Use this for initialization void Start () { car = GameObject.Find("benchi"); } // Update is called once per frame void Update () { //旋轉 if (Input.GetMouseButtonDown(0)) { p1 = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//滑鼠左鍵按下時記錄滑鼠位置p1 } if (Input.GetMouseButton(0)) { p2 = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//滑鼠左鍵拖動時記錄滑鼠位置p2 if (transform.position.y >= 20 && transform.position.y <= 130) //控制在20-130之内 { float dx = p2.x - p1.x; float dy = (float)0.6 * (p2.y - p1.y); //滑鼠上下移動 transform.Translate(-dy * Vector3.up * Time.deltaTime); transform.RotateAround(car.transform.position, Vector3.up, dx * Time.deltaTime); } else if (transform.position.y < 20 && p2.y < p1.y) { float dx = p2.x - p1.x; float dy = (float)0.6 * (p2.y - p1.y); //滑鼠上下移動 transform.Translate(-dy * Vector3.up * Time.deltaTime); transform.RotateAround(car.transform.position, Vector3.up, dx * Time.deltaTime); } else if (transform.position.y > 130 && p2.y > p1.y) { float dx = p2.x - p1.x; float dy = (float)0.6 * (p2.y - p1.y); //滑鼠上下移動 transform.Translate(-dy * Vector3.up * Time.deltaTime); transform.RotateAround(car.transform.position, Vector3.up, dx * Time.deltaTime); } //下面開始旋轉,僅在水準方向上進行旋轉 //else if (transform.position.y < 20) //{ // transform.position.y = 20.1f; //} //else if (transform.position.y > 130) //{ // transform.position.y = 129.9f; //} //light.transform.RotateAround(car.transform.position, Vector3.up, dx * Time.deltaTime); } //滑鼠滾輪控制場景大小 // 如果按住滑輪 if (Input.GetAxis("Mouse ScrollWheel") > 0) { Debug.Log(1); Debug.Log(Input.GetAxis("Mouse ScrollWheel")); if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax) { normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity; } if (normalDistance < MouseZoomMin) { normalDistance = MouseZoomMin; } if (normalDistance > MouseZoomMax) { normalDistance = MouseZoomMax; } // transform.Translate(transform.forward * normalDistance); transform.camera.fieldOfView = normalDistance; } //後滾 else if (Input.GetAxis("Mouse ScrollWheel") < 0) { Debug.Log(-1); if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax) { normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity; } if (normalDistance < MouseZoomMin) { normalDistance = MouseZoomMin; } if (normalDistance > MouseZoomMax) { normalDistance = MouseZoomMax; } // transform.Translate(-transform.forward * normalDistance); transform.camera.fieldOfView = normalDistance; } } }
本文轉蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366218,如需轉載請自行聯系原作者