实现鼠标旋转拖动缩放模型,非常不错的代码:
创新互联建站是专业的合川网站建设公司,合川接单;提供成都做网站、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行合川网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!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); } }
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。