//摄像机update频率更新情况下
position | rotation | 结果 |
---|---|---|
不插值 | 插值 | 摄像机高速移动抽搐 |
插值 | 插值 | 角色跑步抽搐 |
不插值 | 不插值 | 摄像机高速移动抽搐 |
插值 | 不插值 | 角色跑步抽搐 且 摄像机高速移动抽搐(不明显) |
//摄像机fixedupdate频率更新情况下
position | rotation | 结果 |
---|---|---|
不插值 | 插值 | 摄像机高速移动抽搐(不明显) |
插值 | 插值 | 正常 |
不插值 | 不插值 | 摄像机高速移动抽搐 |
插值 | 不插值 | 摄像机高速移动抽搐 |
总结
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;
}
}