Unity3D模拟魔兽世界第三人称视角旋转/缩放

来源:互联网 发布:数组中删除重复元素 编辑:程序博客网 时间:2024/04/28 22:46

转载自:http://viowan.blog.163.com/blog/static/13762979320134289547567/

using UnityEngine;using System.Collections;public class Script_07_11 : MonoBehaviour { public Transform target; public float distance = 20.0f; float x; float y; float z; float yMinLimit = 10.0f; float yMaxLimit = 80.0f; float xSpeed = 250.0f; float ySpeed = 120.0f;void Start () { Vector3 angles = transform.eulerAngles; x = angles.x; y = angles.y; z = -this.distance; if (this.rigidbody) this.rigidbody.freezeRotation = true; this.goRight();}void LateUpdate () { float temp = Input.GetAxis("Mouse ScrollWheel"); if (this.target && (temp != 0.0f || Input.GetMouseButton(1))) { x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y += Input.GetAxis("Mouse Y") * ySpeed * 0.02f; z += temp * 100.0f * 0.02f; y = ClampAngle(y,yMinLimit,yMaxLimit); z = Mathf.Clamp(z, -20.0f, -5.0f); this.goRight(); }} float ClampAngle(float angle,float min,float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp(angle,min,max); } void goRight(){ Quaternion rotation = Quaternion.Euler(y, x, 0);//鼠标上下移动的时候其实是沿X轴旋转,鼠标左右移动的时候其实是为了沿着Y轴旋转 /*旋转之后不移动位置的话只是相当于转了下摄像头而已,你看到的只是转过去之后摄像机镜头将会显示的东西,比如会看到山树之类的,由于摄像头的位置并没有改动,所以原来看过去线中与摄像头的距离也是有问题的。 * 相乘之后能让相机回归我们“原来”看的视角(即离物体距离为z乘以旋转的(X,Y)角度

* *加上最后一个target.position只是为了参照目标的坐标系因为去掉的话就是基于(0,0,0) * * */ Vector3 position = rotation * new Vector3(0.0f, 0.0f, z) + target.position; this.transform.position = position; this.transform.rotation = rotation; }}


0 0
原创粉丝点击