unity3d 摄像机跟随角色时 画面抽搐问题

//摄像机update频率更新情况下

positionrotation结果
不插值插值摄像机高速移动抽搐
插值插值角色跑步抽搐
不插值不插值摄像机高速移动抽搐
插值不插值角色跑步抽搐 且 摄像机高速移动抽搐(不明显)

//摄像机fixedupdate频率更新情况下

positionrotation结果
不插值插值摄像机高速移动抽搐(不明显)
插值插值正常
不插值不插值摄像机高速移动抽搐
插值不插值摄像机高速移动抽搐

总结
rotation和position未插值 导致摄像机高速移动导致画面抽搐(推测由于鼠标不是匀速运动引起的)
角色刚体更新频率和摄像机更新频率不一致(摄像机update更新,角色刚体fixedupdate更新,频率不一致,角色更新速度一次,摄像机可能更新位置一次或者多次) 导致角色跑步画面抽搐

附测试代码

摄像机

public class CameraController : MonoBehaviour
{
    public GameObject _lookAt;
    public float _distance;
    public float _mouseSensitivity;
    public float _height;
    public bool isNeedPosDamping;
    public bool isNeedRotateDamping;
    public float _moveDamping = 2.5f;
    public float _rotateDamping = 2.5f;
    private const float MINRX=-2f;
    private const float MAXRX=89f;

    private Vector3 _camForward
    {
        get => transform.forward;
    }
    public Vector3 _camUp{ get; private set; }
    public Vector3 _camRight { get; private set; }
    public Vector3 _castForward;
    private Camera _camera;
    private float _mouseRx, _mouseRy;


    private void Start()
    {
        Application.targetFrameRate = 60;
        _camUp = Vector3.up;
        ReCalCastForward();
        Cursor.visible = false;
    }

    void FixedUpdate()
    {
        _camera = GetComponent<Camera>();
        _mouseRy += Input.GetAxis("Mouse X") * _mouseSensitivity * 0.1f;
        _mouseRx -= Input.GetAxis("Mouse Y") * _mouseSensitivity * 0.1f;
        Debug.Log(_mouseRy);
        _mouseRx = math.clamp(_mouseRx,MINRX, MAXRX);

        Quaternion newCamRotation = Quaternion.Euler(_mouseRx, _mouseRy, 0);
        Vector3 newCamPos = _lookAt.transform.position - _camForward * _distance + _height * Vector3.up;
        if (isNeedPosDamping)
        {
            transform.position = Vector3.Lerp(transform.position, newCamPos, Time.fixedDeltaTime * _moveDamping);
        }
        else
        {
            transform.position =newCamPos;
        }
        if (isNeedRotateDamping)
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, newCamRotation, Time.fixedDeltaTime * _rotateDamping);
        }
        else
        {
            transform.rotation = newCamRotation;
        }
        ReCalCastForward();
    }

    private void OnDestroy()
    {
        Cursor.visible = true;
    }

    private void ReCalCastForward()
    {
        var left = Vector3.Cross(_camForward, _camUp).normalized;
        _camRight = -left;
        var castForward = Vector3.Cross(_camUp, left).normalized;
        _castForward=castForward;
    }
}

角色移动代码

using UnityEngine;

public class PlayerController : MonoBehaviour
{
   private Animator _animator;
   private Rigidbody _rb;
   public CameraController _cameraController;
   public float _speed;

   private void Start()
   {
      _animator = GetComponent<Animator>();
      _rb = GetComponent<Rigidbody>();
   }

   private void Update()
   {
      var x=Input.GetAxisRaw("Horizontal");
      var y = Input.GetAxisRaw("Vertical");

      if(InRange(x,-0.01f,0.01f)&&InRange(y,-0.01f,0.01f))
      {
         _animator.SetBool("IsRunning",false);

      }
      else
      {
         var forward = _cameraController._castForward;
         var right = _cameraController._camRight;
         _rb.velocity = (x * right+y * forward)*_speed;
         _animator.SetBool("IsRunning",true);
         var rY =90-Mathf.Atan2(_rb.velocity.z,_rb.velocity.x)*Mathf.Rad2Deg;
         Quaternion newPlRotation=Quaternion.Euler(0,rY,0);
         transform.rotation = newPlRotation;
      }
   }

   private bool InRange(float x, float min, float max)
   {
      return x < max && x > min;
   }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值