0
votes

I use rigidbody.MovePosition to move around my character , and have a camera following it. The problem is when i switch directions suddenly the player will teleport a bit instead of smoothly moving in the opposite direction of motion. The scripts for the player and Camera are set to FixedUpdate , if I try moving camera to a LateUpdate then the whole thing jitters a lot.

Player Script :

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);

}

Camera Script

 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
Your player may not be teleporting but the camera to have problem with switching to opposite direction. In the past a had problems with SmoothDamp not receiving "consistent" input, i.e. smoothing causing problems when switching from one type of smoothing to another. I assume your problem may be similar. - Nikaas

2 Answers

0
votes

the problem is that your motion logic is implemented within FixedUpdate but you take the time.deltaTime. You have to use the fixedDeltaTime version. Also do not try to get the Input within the FixedUpdate that can also cause jitter and stutter/jumping of objects.

Instead get the Input from Update and use variables to pass it inside the FixedUpdate.

A last one, look at your RigidBody for the Interpolation/Extrapolation depending on your Scene Setup and Camera this could also help.

0
votes

I finally figured out what was wrong with it, something about the rotation of the camera in the fixed update. I moved it to update and it works fine now