今天帮一个朋友看了个虚拟现实的项目,他希望实现鼠标滚动拉近模型、鼠标右键旋转模型、鼠标中键拖拽模型,部分功能网上已经有答案、但是合在一起的没有找到,哪么我就加以总结把把功能都写在这个demo中吧。
把如下代码直接挂在摄像机对象上。最近心情很down,有点懒了就不写注释了,如果有朋友有疑问欢迎给我留言。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
using UnityEngine; using System.Collections; public class Script_07_11 : 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; private Quaternion rotation = Quaternion.Euler(new Vector3(30f,0f,0f)); private Vector3 CameraTarget; void Start () { CameraTarget = target.position; float z = target.transform.position.z - normalDistance; 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 Update () { if(Input.GetMouseButton(1)) { 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) { 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); 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); Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset; target.transform.position = curPosition; } transform.LookAt(CameraTarget); } 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); } } |
最后是工程下载地址:http://vdisk.weibo.com/s/Dorl2
- 本文固定链接: https://www.xuanyusong.com/archives/2341
- 转载请注明: 雨松MOMO 于 雨松MOMO程序研究院 发表
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!
5.5.1版本的unity不能对博主的包解压Failed to import package with error: Couldn’t decompress package
我采用了你的代码,可是出现了这样的错误,是为什么?UnassignedReferenceException: The variable target of show3D has not been assigned.You probably need to assign the target variable of the show3D script in the inspector.
雨松大大,(功能和你写的一样)我想问下可以通过可以通过改变摄像机的位置实现拖拽物体的效果吗,我写了一个右键旋转和滚轮缩放都实现了,但是拖拽模型正面功能是正常的只要右键旋转角度,拖拽就会横纵向颠倒(我是拿偏移值写的拖拽摄像机实现拖拽物体效果),最后发现是摄像机的位置旋转过去还是按照世界坐标给摄像机赋的值,不知道有什么好的解决方案,谢谢!!!
鼠标滚轮视角会变的情况 应该是物体不在原点 transform.position = normalized * normalDistance;这里改成 transform.position = normalized * normalDistance CameraTarget;应该就没问题了
鼠标滚轮视角会变的情况 应该是物体不在原点 transform.position = normalized * normalDistance;这里改成 transform.position = normalized * normalDistance + CameraTarget;应该就没问题了
Momo大神 如果拖拽时使物体只延着Z轴移动 应该怎么弄呢
让Z轴坐标不变啊
雨松大神,我把你的脚本放进去了,附给了main camera 运行后,鼠标稍微动一下,视角就变了
不是鼠标动一下,是点下鼠标右键或者滚轮
雨松大神,小弟做2d横版跑酷游戏,使用transform.Translate(Vector3(1,0,0)*speed*Time.deltaTime)平移物体时,当speed数值稍微大点儿后,物体的边缘有明显的画面抖动感 ,请问这个问题怎么解决?求指点~
雨松大神,小弟做2d横版跑酷游戏,使用transform.Translate(Vector3(1,0,0)*speed*Time.deltaTime)平移物体时,当speed数值稍微大点儿后,物体的边缘有明显的画面抖动感 ,请问这个问题怎么解决?求指点~
y = ClampAngle(y, yMinLimit, yMaxLimit); MoMo老师,y的值为什么要限制大小呢。