#Jittery movement using Cinemachine 2D

1 messages · Page 1 of 1 (latest)

thorny pewter
#
public class Move : MonoBehaviour
{
    Vector3 mousePosition;
    public float moveSpeed = 0.1f;
    Rigidbody2D rb;
    Vector2 targetPosition = new Vector2(0, 0);

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        mousePosition = Input.mousePosition;
        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

        Vector2 playerMouseVect = mousePosition - transform.position;
        transform.up = playerMouseVect.normalized;

        if (Input.GetMouseButton(1))
        {
            targetPosition = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
            rb.MovePosition(targetPosition);
        }

    }
}
#

this is the movement script

craggy canopy
#

This is most likely because you're movement is happening in Unity's FixedUpdate method and your Camera is most likely updated in Unity's Update method. This in most cases makes it look like the player is jittering. I haven't used Cinemachine before, but this is probably the case.

#

Just looked into it. Cinemachine updates their cameras positions in the LateUpdate method. Late update is called multiple random times per frame while fixed update is called at fixed intervals and not dependent on frame rate. This is why you are getting jittery movement.

thorny pewter
#

so should i change my camera script to fixedUpdate. "the look script"

#

@craggy canopy

craggy canopy
#

@thorny pewter Yeah, or use Vector3.SmoothDamp on the Camera. That fixes it.