2D laggy movement

I wrote this movement script for a 2D project that I’m making, the script is on a capsule and it works but the movement is kind of choppy/laggy, if you follow the capsule with your eye its easy to notice, I’m using a GSYNC monitor and unity is running at 300 fps.

Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] Rigidbody2D playerRb;
    [SerializeField] float moveSpeed = 7.0f;
    [SerializeField] float runningSpeed = 10f;

    void Start()
    {
       
    }

    void Update()
    {
        Movement();
    }  

    void Movement()
    {
        Vector2 PlayerMove = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        PlayerMove.Normalize();

        if (Input.GetKey(KeyCode.LeftShift))
        {
            playerRb.velocity = PlayerMove * runningSpeed;
        }
        else
        {
            playerRb.velocity = PlayerMove * moveSpeed;
        }
    }
}

Is there any way to fix this? Thanks.

Which version of Unity are you using?

I am using 2021.1.10f1

What’s coming through on the raw axis in terms of values?

Can you stick them in an array, and then examine it for variances?

Don’t debug.log each update’s values, this creates garbage that will create even bigger shudders and jitters and shudders when it’s cleaned up. Just examine variances in an array that’s big enough to not run out of space.

1 Like

An interesting general test is to spin a very big stick, from one end of the stick - in the middle of the screen, the other end towards the edge, and make it white if you’ve got a black background (high contrast), and then use a modification of the time difference between frames to set the changes per frame to what should be a consistent rate of rotation around the centre, regardless of fluctuations in the frame rate.

This way you’ll see if smooth movement is at all possible.

This generates some very interesting results in Unity, and reveals ways to get it smoother.

Unfortunately I can’t just say “do it this way”, because on different systems it’s never quite the same solution, and it depends on your acuity to variance, too.

1 Like

I was getting values below 1 and above -1 every time, so I think that’s fine.

The spinning stick is indeed smooth while using FixedUpdate and Update methods, I moved my project to another OS and that seemed to fix the problem, Thanks for your help!! The rotating stick is now my preferred method to test smoothness xD

1 Like