How to create framerate independent movement in Unity

Next Story

How to load a new level/scene in Unity

Due to the fact that scenes might run at different frame rates due to complexity of the level, you don’t want your game to work differently during different scenes.

One way to handle movements in Unity game engine is by taking into account the time of change as movement is taken into account.

void Update(){
  var movement = direction * speed;

  movement *= Time.deltaTime;

  this.transform.Translate(movement);
}

By using the Time library’s deltaTime variable and multiplying it by the movement variable, it turns the movement variable into a movement per unit time value.

Thus, when the transform function is called to translate the object, the amount of movement will take into account the amount of time that have passed between the frames.

Writing our movement code this way in the update method will cause the character object to move at different rates depending on the framerate but it will make it seem like the character is moving at a steady pace to the user.

Leave a Reply

Your email address will not be published. Required fields are marked *

https://www.googletagmanager.com/gtag/js?id=UA-63695651-4