I’m developing a 2.5D mobile game with Unity. In order to move the character back and forward I’m using a piece of code inside the update function:
void Update ()
transform.Translate(Vector3.forward * 3 * Time.deltaTime);
So that code works pretty well when the game is running at 60 fps, but when the fps go down to 30 or less, the character starts to vibrate while moving. I’ve tried to test the same code with a plane terrain and it worked well, so maybe the problem is the collision between the character’s and the terrain’s colliders. However, I don’t understand why if the fps are high it works well. I’ve tried both capsule collider and a mesh collider but no one has worked. What do you think? Should I try to use another code?
Do you have physics taking place in FixedUpdate elsewhere which affects the position of the character?
While googling this, I came across this question with an answer by duck which explains the difference and notes that because of the fixed frame rate of FixedUpdate, multiple physics simulations may take place in between drawn frames when the framerate is low. You mention that the vibration only occurs at low frame rate, so perhaps it’s being moved by physics simulations in between visible frames at 30FPS?
If there is collision involved, you should use FixedUpdate to move anything, because FixedUpdate is framerate independent and is used for physics calculation.
If your game is run ad 30 fps, you have major desynchronization between Update(when your character moves) and FixedUpdate(when collision is calculated) calls, thats why your characters move a little between frames or something like that.