Unity相机抖动/播放器传送

时间:2019-01-13 02:58:29

标签: unity3d camera game-physics

我使用了body.MovePosition来移动我的角色,并在其后面放置一个摄像头。问题是当我突然切换方向时,播放器将稍微移动一点,而不是沿相反的运动方向平稳地移动。播放器和Camera的脚本设置为FixedUpdate,如果我尝试将Camera移至LateUpdate,则整个过程会抖动很多。

玩家脚本:

private void Start()
{
    m_Rb = GetComponent<Rigidbody>();
    m_InputAxisName = "Vertical" + m_playerNumber;
    m_StrafeAxisName = "Horizontal" + m_playerNumber;


}
private void FixedUpdate()
{
    m_InputAxisValue = Input.GetAxis(m_InputAxisName);
    m_StrafeAxisValue = Input.GetAxis(m_StrafeAxisName);

    //Movement
    Vector3 movement = (transform.forward * m_InputAxisValue * m_Speed * Time.deltaTime) + (transform.right * m_StrafeAxisValue * m_Speed * Time.deltaTime);

    m_Rb.MovePosition(m_Rb.position + movement);

}

相机脚本

 void FixedUpdate () {

          m_NewPos = m_player.transform.position + m_offset;

    if (m_Rotate)
    {
        Quaternion newRot = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * m_rotSpeed, Vector3.up);
        m_offset = newRot * m_offset;
    }


    transform.position = Vector3.SmoothDamp(transform.position, m_NewPos, ref m_MoveSpeed, m_DampTime); ;

    if (m_Rotate)
        transform.LookAt(m_player);


}

2 个答案:

答案 0 :(得分:0)

问题在于,您的运动逻辑是在FixedUpdate中实现的,但是您需要使用time.deltaTime。您必须使用fixedDeltaTime版本。 另外,请勿尝试在FixedUpdate中获取输入,这也可能导致对象的抖动和卡顿/跳动。

取而代之的是从Update获取输入,并使用变量将其传递到FixedUpdate内部。

最后一个,根据您的“场景设置”和“相机”,查看您的RigidBody进行的内插/外推,这也可能会有所帮助。

答案 1 :(得分:0)

我终于弄清楚了问题所在,关于固定更新中相机的旋转。我将其移动以进行更新,现在可以正常使用

相关问题