unity相机旋转带阻尼效果

unity相机旋转带阻尼效果using System Collections using System Collections Generic using UnityEngine using UnityEngine InputSystem public class SmoothRotate 1 MonoBehaviou Header

大家好,我是讯享网,很高兴认识大家。

在这里插入图片描述
讯享网

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class SmoothRotate_1 : MonoBehaviour { 
    [Header("旋转的类型")] public RoatationType _RoatationType; [Header("围绕旋转的目标物体")] public Transform target; [Header("设置旋转角度")] public float x = 0f, y = 0f, z = 0f; [Header("旋转速度值")] public float xSpeed = 10f, ySpeed = 10f, mSpeed = 5f; [Header(" y轴角度限制,设置成一样则该轴不旋转")] public float yMinLimit = -50, yMaxLimit = 80; [Header(" x轴角度限制")] public float leftMax = -365, rightMax = 365; [Header("距离限制")] public float distance = 6f, minDistance = 0.5f, maxDistance = 10f; [Header("阻尼设置")] public bool needDamping = true; [Header("阻尼大小")] public float damping = 3f; public float posdamping = 10; [Header("初始角度")] public float initX; public float initY; // 改变中心目标物体 public void SetTarget(GameObject go) { 
    target = go.transform; } void Start() { 
    Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; //pers(); } void LateUpdate() { 
    if (target) { 
    if (Input.GetMouseButton(0)) { 
    // 判断是否需要反向旋转 if ((y > 90f && y < 270f) || (y < -90 && y > -270f)) { 
    x -= Input.GetAxis("Mouse X") * xSpeed * 0.02f; } else { 
    x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; } y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; // x = ClampAngle(x, leftMax, rightMax); y = ClampAngle(y, yMinLimit, yMaxLimit); } distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed; distance = Mathf.Clamp(distance, minDistance, maxDistance); //Vector3 disVector = new Vector3(0.0f, 0.0f, -distance); Vector3 disVector = new Vector3(0.0f, 0.0f, -distance); Debug.Log(distance); //旋转角度 Quaternion rotation = Quaternion.Euler(y, x, z); //位置 Vector3 position = rotation * disVector + target.position; // Vector3 position = rotation * disVector + target.position; // 阻尼感 if (needDamping) { 
    transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping); if(_RoatationType==RoatationType.user) { 
    transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * posdamping); } } else { 
    transform.rotation = rotation; if (_RoatationType == RoatationType.user) { 
    transform.position = position; } } } } // 对数值进行限制; 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); } // 初始 public void pers() { 
    this.x = initX; this.y = initY; } // 正视图 public void front() { 
    this.x = 0f; this.y = 0f; } // 后视图 public void back() { 
    this.x = 180f; this.y = 0f; } // 左视图 public void left() { 
    this.x = 90f; this.y = 0f; } // 右视图 public void right() { 
    this.x = 270f; this.y = 0f; } // 俯视图 public void top() { 
    this.x = 0f; this.y = 90f; } // 仰视图 public void bottom() { 
    this.x = 0f; this.y = -90f; } } public enum RoatationType { 
    self, user, } 

讯享网
小讯
上一篇 2025-01-24 12:01
下一篇 2025-01-15 23:23

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/83004.html