Sign in to follow this  
Wolken

Need help with a simpe movement bug

Recommended Posts

Hello guys.

 

I am making a simple 2D game in Unity for Android. In that game the movement of a couple of objects is controlled by the following line:

 

transform.position = new Vector3(transform.position.x + speed * time.deltaTime, transform.position.y, transform.position.x);

 

But when I test the game on different mobile phones the resulting speed is different. For instance, on one phone it moves faster and on the other one slower. I think the better the phone the slower it moves. Would you know what could be the cause of this and how should I fix it?

 

Thanks in advance.

Share this post


Link to post
Share on other sites

Are you using Unity's FixedUpdate() function, or Update() function?

 

Update is used for non-physics objects and user input. It is called irregularly, and deltaTime may be nearly any value.  Use it if you need something to happen every frame, no matter when that frame actually hits.  Be careful about doing motion here.  While it is more smooth (the function happens each frame) it is less regular with different results on every device.

 

Any time you are working with physics manipulation you should be using FixedUpdate(). While the results still won't be identical between machines (Unity does not make that guarantee) at least with FixedUpdate() you'll have more consistent results between machines. You'll also want to use Time.fixedDeltaTime for the step. The built-in physics system triggers immediately after FixedUpate(), so it is especially important to make any physics-based changes here.  These are more regular and more dependable, but different from each frame's update.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this