温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

[unity3d]鼠标拖动and旋转缩放

发布时间:2020-02-26 12:03:43 来源:网络 阅读:433 作者:蓬莱仙羽 栏目:游戏开发
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;         } 	} } 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI