Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] movement juddering bug?

Discussion in 'Editor & General Support' started by Nyyus, May 27, 2019.

  1. Nyyus

    Nyyus

    Joined:
    Feb 27, 2019
    Posts:
    8
    Im trying to do a spaceship game with a camera following the player with a delay.
    it worked fine but the spaceship stops and moves repeatedly in a short time what created a weird looking effect.
    I started to do some testing and every test showed me that it looks like every movement is handeled in unity like this even a simple cube with a constant speed had the same behavior.


    this is my following script:

    public class mainKamera : MonoBehaviour
    {
    public GameObject target;
    public float distanceDamp = 10f;
    public float rotationDamp = 10f;
    public Vector3 defaltDistance = new Vector3(0f, 2f, -10f);
    private Vector3 AimPos;
    private Quaternion AimRot;

    private void Update()
    {
    AimPos = target.transform.position + (target.transform.rotation * defaltDistance);
    transform.position = Vector3.Slerp(transform.position, AimPos, Time.deltaTime*distanceDamp);

    AimRot = Quaternion.LookRotation(target.transform.position - transform.position, target.transform.up);
    transform.rotation = Quaternion.Lerp(transform.rotation, AimRot, rotationDamp * Time.deltaTime);
    }
    }
     
    Last edited: May 27, 2019
  2. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    709
    Could you please make the title more specific than: "Is this a bug?".
     
  3. IronLionZion

    IronLionZion

    Joined:
    Dec 15, 2015
    Posts:
    78
    Try using Vector3.Lerp instead of Slerp.
    Also try putting your camera movement code inside LateUpdate instead of Update.
     
  4. Nyyus

    Nyyus

    Joined:
    Feb 27, 2019
    Posts:
    8
    Thx for the suggestion but I tried both already.
     
  5. IronLionZion

    IronLionZion

    Joined:
    Dec 15, 2015
    Posts:
    78
    Judging from the two videos, the problem doesn't seem to be with the camera, but with the ship. Post the code that moves the ship.
     
  6. Nyyus

    Nyyus

    Joined:
    Feb 27, 2019
    Posts:
    8
    private void Start()
    {
    rb = GetComponent<Rigidbody>();
    }
    private void FixedUpdate()
    {
    rb.AddForce(transform.forward * forwardForce);
    }

    I have the drag setting in my rigidbody on 5.
     
  7. IronLionZion

    IronLionZion

    Joined:
    Dec 15, 2015
    Posts:
    78
    Ah, your ship is using physics to move. I also get this jittering when I have a camera that follows a rigidbody.
    Two solutions:
    1) Remove physics/rigidbody from the ship movement. Move the ship in Update by setting transform.position.
    2) Keep the ship code as it is but place the camera code inside FixedUpdate.

    This always fixes the problem for me. If it doesn't work for you, then I'm sorry but I don't have any other suggestions.
     
  8. Nyyus

    Nyyus

    Joined:
    Feb 27, 2019
    Posts:
    8
    I found a very easy soulution.
    You just have to put the settings in the Rigidbody on Interpolate and its smooth.
     
    Last edited: Dec 8, 2019
    CloudyVR likes this.