4
votes

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 () {

   if (shouldMove==true){

     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?

Edit 1: I'm using a capsule collider and a rigidbody. Should I use a Character Controller?

2
Are you saying that the shouldMove boolean is being continually flipped on and off thus the character only moves on certain frames and that the way you interface with the physics engine is what drives the shouldMove value?Chris Sinclair
No. When the user touches a button to move the character, should move is true. And when the user ends touching it, shouldMove is said to false.Marti Serra Vivancos
Well, you might need more code or a better description of your physics engine usage. You may also want to consider posting this to the official Unity3D answers or support forums. Much more likely someone there has dealt with the same issue as you.Chris Sinclair
Look at edit 1. I've already posted the question in Unity.Marti Serra Vivancos

2 Answers

3
votes

Sam Bauwens is absolutely right in his response, however, this issue normally is caused due to an excess of objects (specially those which are animated). This can worsen the performance a lot.

You should try to delete some of the objects and try if your character still vibrates. If it doesn't, that means that I'm right. Of course, you won't want to delete objects of your scene, so, you can add an asset such as SmartLOD that deletes the geometry of those objects that are not shown on screen and thus enhances your game's performance.

Hope it helps.

3
votes

I had a similar problem with a ball that vibrates on the ground. It was caused by gravity which is pulling the gameobject towards the ground, then it collides on the ground and bounces. If your problem is the same as me, you have to either tune the Fixed Timestep (Edit => Project settings => time) and/or Bounce Threshold (Edit => Project Settings => Physics).

By increasing Bounce Threshold, you're going to increase the minimum speed below which an object won't bounce, so that the force of gravity won't be big enough to make the ball's velocity exceed the bounce threshold.

By reducing Physics time step, you reduce the impact of gravity for each time step because the time steps are smaller and therefore the amount of velocity added to the gameobject for each timestep is smaller.

EDIT: you can also look at sleep velocity (Edit => Project Settings => Physics), because if it is higher that the gravity velocity, the object shouldn't vibrate.