- Home /
GameObject trembles when moving with setPosition
Hi! to everyone,
I'm making a prototype of a shoot'em up game.
I just have in the scene a sphere and I made a script to move it just in the x axis. this is the code for moving the sphere:
using UnityEngine;
using System.Collections;
public class EnemyScript : MonoBehaviour{
public void Update ()
{
transform.position+= new Vector3(5,0,0)*Time.deltaTime;
}
}
The sphere moves, but it does like little jumps and trembles as it's moving
Anyone can helo?
Thank you and kind regards
First, I would move this under FixedUpdate as Update is called every frame. So if your computer FPS spikes, the would move faster. FixedUpdate is called every fixed framerate frame. Otherwise, I do not see any trembling when i run your script.
Probably not a solution but you shouldn't set the position to move the object, use transform.Translate ins$$anonymous$$d.
I also did that, and still the same, if I have 2 gameobjects both of them "jump" at the same frame...even they move at different speeds
Thanks!!!
Do they have rigidbodies attached to them? If so, are they set to $$anonymous$$inematic objects?
One objects is with rigidbody and no kinematic, the other object is moved with translate and has no rigidbody
Answer by supernat · Mar 08, 2014 at 04:29 PM
If you are not limiting the frame rate, you could have very small frame times, down in the floating point error. Using FixedUpdate and removing the time.deltaTime from your equation (replace it with fixed time delta) is one way to correct it. Under normal circumstances though, you can safely use time.deltaTime which compensates for frame time variation, so technically I don't think your code is wrong in general, only if you expect high frame rates does it become problematic.
Add this to limit frame rate to see if indeed that is the cause. Application.targetFrameRate = 60
I coded the movement in FixedUpdate, modify the framerate to 60 and even used forces to move the rigid body (impulse) and still got this nasty jumps...
Anyone?
Answer by IBAI23 · Mar 09, 2014 at 03:18 PM
Thank you so much guys,
I changed it to FixedUpdate and still got that "mini jumps", I coded the frame rate limit to 60 and still got the jumos too. I don't know what to do :(
Thank you for the quick and good answers!!!! :-)
Your answer
![](https://koobas.hobune.stream/wayback/20220613135712im_/https://answers.unity.com/themes/thub/images/avi.jpg)