unity 相机控制

您所在的位置:网站首页 unity相机控制脚本 unity 相机控制

unity 相机控制

2023-11-17 03:10| 来源: 网络整理| 查看: 265

 转载于:https://blog.csdn.net/weixin_42399500/article/details/84871444?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=1328680.8103.16161168717678889&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

public class Test : MonoBehaviour { public Transform target;//相机的目标     private float xSpeed = 200;//x轴的旋转速度     private float ySpeed = 150;//x轴的旋转速度     public float yMin = -20;//y最小角度     public float yMax = 20;//y最大角度     public bool needDamping = true; //是否需要的阻尼     private float damping = 5;//阻尼      private float x = 0; private float y = 0; void Start() { Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; } void LateUpdate() { if (target) {             //使用鼠标光按钮来控制相机,调整照相机位置             if (Input.GetMouseButton(0)) { x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y = ClampAngle(y, yMin, yMax); } Quaternion rotation = Quaternion.Euler(y, x, 0.0f); Vector3 disVector = new Vector3(0.0f, 0.0f, 0); Vector3 position = rotation * disVector + target.position; if (needDamping) { transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping); transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping); } else { transform.rotation = rotation; transform.position = position; } } }     //限制旋转角度     private float ClampAngle(float angle, float min, float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp(angle, min, max); } private void Update() {         //视野放大缩小         if (Input.GetAxis("Mouse ScrollWheel") > 0) { if (GetComponent().fieldOfView >= 30) { GetComponent().fieldOfView--; } } if (Input.GetAxis("Mouse ScrollWheel") < 0) { if (GetComponent().fieldOfView


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3