您好,登錄后才能下訂單哦!
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; } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。