0
\$\begingroup\$

I just started to make this game, it runs fine on my computer. 60fps, but when I tested it on another computer, with lower fps, the player just started to jitter and glitch around a lot. The game is basically unplayable like that, and I want to make sure that everyone who plays my game will be able to enjoy the full experience. I have pasted the specific part of the code for the movement below.

void Update () {

    rb.AddForce (Vector3.forward * ForwardSpeed);

    if (Input.GetKeyDown (KeyCode.A) && side > maxSideLeft) {
        rb.AddForce (-Vector3.right * Speed);
        side -= 1;
    } else if (Input.GetKeyDown (KeyCode.D) && side < maxSideRight) {
        rb.AddForce (Vector3.right * Speed);
        side += 1;
    }

    if (Input.GetKeyDown (KeyCode.W) && level < maxLevelHeight) {
        rb.AddForce (Vector3.up * Speed);
        level += 1;
    } else if (Input.GetKeyDown (KeyCode.S) && level > minLevelHeight) {
        rb.AddForce (-Vector3.up * Speed);
        level -= 1;
    }

    if (Input.GetKeyDown (KeyCode.R) || Input.GetKeyDown(KeyCode.Space)) {
        SceneManager.LoadScene ("Scene1");
        Time.timeScale = 1;
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You appear to be moving the player in the Update() method, applying direction and speed. For smooth movement, you also need to accommodate for the time in between updates.

Consider this: your telling your player to move by a set distance every time you perform an Update(). We will say your hardware is fast enough to perform Update() 60 times per second. If you play your game using hardware that is only fast enough to perform Update() 30 times per second, you will only get to tell the player to move half as many times as before. Using only speed, your player would only be able to move half the distance in the same amount of time.

The solution is quite simple. You use the time since the last Update() as a multiplier, to ensure smooth movement, regardless of hardware. Unity provides this value as Time.deltaTime. Simply replacing Speed with Speed * Time.deltaTime should ensure smooth movement.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you. I have no idea why I did not do that. I had a different movement script that implemented transform.translate and use Time.deltatime for that, but for some reason I just didn't do it here. \$\endgroup\$
    – alexo1001
    Nov 11, 2016 at 2:00
  • 1
    \$\begingroup\$ This wrongly indicates that the OP should continue to use Update for physics movement. You should be using FixedUpdate. \$\endgroup\$
    – zcabjro
    Nov 11, 2016 at 17:16
  • \$\begingroup\$ @Jack, than make the changes for review. I won't make such edits from phone. \$\endgroup\$
    – Gnemlock
    Nov 11, 2016 at 20:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .