Player movement input lag. Is it because of my code?

Yo! I’m making a 2d scroller sort of game that has the player move along 3 horizontal tracks. The player sort of snap vertically from track to track, but still have free horizontal movement. What’s happening is that it’s moving the way I want it too but some of my key presses are not registering.

void FixedUpdate ()
    {
        float y = 0;
        float moveHorizontal = Input.GetAxis ("Horizontal"); //horizontal movement
        if (Input.GetButtonDown ("verticalUp") == true) { //determines what direction to move in
            transform.Translate(0.0f, y + 1, 0.0f); //transforms in that direction
        }
        if (Input.GetButtonDown ("verticalDown") == true) {
                transform.Translate(0.0f, y - 1, 0.0f);
        }
        Vector2 movement = new Vector2 (moveHorizontal, 0.0f);
        GetComponent<Rigidbody2D>().velocity = movement * speed;
        GetComponent<Rigidbody2D>().position = new Vector2 //this sets up the player boundery
            (
                Mathf.Clamp (GetComponent<Rigidbody2D>().position.x, boundary.xMin, boundary.xMax),
                Mathf.Clamp (GetComponent<Rigidbody2D>().position.y, boundary.yMin, boundary.yMax)
                );
    }

Is this code based and if so how do I fix it?

You’re using FixedUpdate to read input. You should only read input in Update, because FixedUpdate can be called several times per frame or only once between several frames. Input is read every frame, especially Input.GetButtonDown and similar functions that only return true during one frame are easily missed by FixedUpdate.

A bit more detail about what ThermalFusion is saying: The update cycle basically goes:

Any number of FixedUpdates
Read input from keyboard
Update
Render frame
Any number of FixedUpdates
Read input from keyboard
Update
Render frame

So not only will you get unpredictable numbers of FixedUpdate’s being executed, it will also be guaranteed to be at least one frame behind where it would be had you used Update.

The reason there may be 0, 1, or more FixedUpdate’s between each Update has to do with timing. Let’s say your fixed timestep is 0.02 seconds (the default). If frame 10 hits at t=2.0 seconds and frame 11 hits at t=2.01 seconds, there won’t have been enough time in between those frames for the fixed timestep to have happened. On the other hand, if frame 11 hits at t=2.1 seconds, the game will have to run five FixedUpdate’s before the next normal Update. And all five of those FixedUpdate’s will use last frame’s input data!

Second note: You repeatedly call GetComponent(). This is inefficient. Call that once (generally in Start() or Awake(), store it in a variable of type Rigidbody2D, and then use that variable instead. (It’s not so important for this one thing, but getting in good habits is how this one thing turns into these 10,000 things, and that’s a problem.)

3 Likes

Thankyou guys! I moved the movement code to Update() and now it moves just fine. Now the thing is, when I’m moving vertically, my player object likes glitching past the bounderies I set for it in line 13 and teleporting back into place. When I move horizonally into the boundary, it pushes past a bit and when I let go of the key the player object glides back into place inside the boundary. How can I fix this?