温馨提示×

温馨提示×

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

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

[Unity3d]鼠标旋转缩放拖动模型

发布时间:2020-07-20 18:48:33 来源:网络 阅读:505 作者:蓬莱仙羽 栏目:游戏开发

实现鼠标旋转拖动缩放模型,非常不错的代码:

using UnityEngine; using System.Collections;  public class MouseControl : MonoBehaviour {      //目标物体     public Transform target;     private int MouseWheelSensitivity = 1; //放大倍数的快慢     private int MouseZoomMin = 1; //最小倍数     private int MouseZoomMax = 5; //最大倍数     //默认距离     private float normalDistance = 3;       private Vector3 normalized;      //拖拽的移动速度     private float xSpeed = 250.0f;     private float ySpeed = 120.0f;      //拖拽的高度限制     private int yMinLimit = -20;     private int yMaxLimit = 80;      //角度     private float x = 0.0f;     private float y = 0.0f;          //记录目标物体的坐标     private Vector3 screenPoint;     private Vector3 offset;      //围绕x旋转30°     private Quaternion rotation = Quaternion.Euler(new Vector3(30f, 0f, 0f));      //目标的3D坐标     private Vector3 CameraTarget;      //打印欧拉角:绕各个轴旋转的角度,顺时针为正方向     public void Awake()     {         print(transform.eulerAngles.x);         print(transform.eulerAngles.y);         print(transform.eulerAngles.z);     }      void Start()     {         //找到目标飞机的3d坐标         CameraTarget = target.position;          //目标飞机的z-3,距离摄像机更近了         float z = target.transform.position.z - normalDistance;         //给当前相机给定位,现在的3D坐标乘以30°         transform.position = rotation * new Vector3(transform.position.x, transform.position.y, z);          //将视角转向物体         transform.LookAt(target);          //记录各个轴偏离的角度         var angles = transform.eulerAngles;         x = angles.y;         y = angles.x;     }      void OnGUI()     {         GUI.Label(new Rect(10,10,300,30),"左击:旋转;滚轮:缩放;中击:拖拽");     }      void Update()     {         //如果左击了,旋转         if (Input.GetMouseButton(0))         {             x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;              y = ClampAngle(y, yMinLimit, yMaxLimit);              var rotation = Quaternion.Euler(y, x, 0);             var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;              transform.rotation = rotation;             transform.position = position;          }         //滚轮缩放         else if (Input.GetAxis("Mouse ScrollWheel") != 0)         {             //摄像机3d坐标-物体的3d坐标             normalized = (transform.position - CameraTarget).normalized;              if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)             {                 normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;             }             if (normalDistance < MouseZoomMin)             {                 normalDistance = MouseZoomMin;             }             if (normalDistance > MouseZoomMax)             {                 normalDistance = MouseZoomMax;             }             //改变摄像机的远近             transform.position = normalized * normalDistance;         }         //案件按下 记录鼠标的         else if (Input.GetMouseButtonDown(2))         {             //将目标物体的坐标转化成平面坐标             screenPoint = Camera.main.WorldToScreenPoint(target.transform.position);             //计算鼠标的3维坐标跟物体的3维坐标的差值             offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));         }          //中键拖拽,改变飞机的坐标,每帧调用         if (Input.GetMouseButton(2))         {             //鼠标的平面坐标             Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);             //鼠标转移的3d空间坐标值             Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;             //改变鼠标的3D空间坐标             target.transform.position = curPosition;         }         //朝向,每动一帧都要改变朝向         transform.LookAt(CameraTarget);     }      //控制旋转的角度,如果旋转的角度大于360或者小于360都要加上或者减去对应的角度     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);     } }


[Unity3d]鼠标旋转缩放拖动模型

向AI问一下细节

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

AI