The distance travelled by my gameObject and behavior depend on my device's performance. If the device is slow for example, it takes longer to accomplish a specific task, thus travels at a smaller speed, smaller distance etc. and doesn't end up where it's supposed to. How can I fix this?

Here's the code:

variable1 -= variable2 * Time.deltaTime;
transform.position += new Vector3 (13, variable1 * Time.deltaTime, 0);
  • \$\begingroup\$ Why do you have a constant value in your transform vector (13)? That definitely makes the whole movement FPS dependant. \$\endgroup\$ – xXliolauXx Oct 11 '15 at 12:32
  • 1
    \$\begingroup\$ Use can also just use FixedUpdate method instead of Update. It doesn't need multiplication by deltaTime because it's invoked every 1/50 second (by default) and not every frame, making it independent of the framerate. \$\endgroup\$ – Igor S. Oct 11 '15 at 21:01
  • \$\begingroup\$ @TrevorPowell may have been a mistake, but that question was recently edited from an existing question, and probably needs to be rolled back. \$\endgroup\$ – Problematic Nov 6 '15 at 21:07
  • \$\begingroup\$ @Problematic Yikes, you're right. I'll do a rollback and see whether folks will vote to reopen with the original question text. Not sure why this was edited into a different question. \$\endgroup\$ – Trevor Powell Nov 9 '15 at 9:40

I don't know if you're fully understanding why delta time is used. So here's a quick recap.

Say you have an object that you want to move at a constant speed, such as 100 units. To update its position, lets say you call a method UpdatePosition();

Assuming that UpdatePosition() is called within a physics engine loop, a separate thread, or a part of just some loop in general, that function is automatically at the mercy of the speed of that loop, or rather its rate is limited to how fast the things before it finish. That's fine and dandy.

This means that the speed of the object that you want to move will vary depending on how busy and how fast the CPU is.

One method of dealing with this, is instead of declaring an amount of space an object should move, you should instead figure out how much you want it to move per unit of time. So if we wanted to move the object 100 units every second, then a timer object should be used to record the time that it takes from the end of the last time you updated the object to the start of the following time you want to update the object. So if for some reason it took 3 seconds to get around to being able to update the object, and you have a rate of 100 units per second, then the value that the object will update by will be:

100 units * 3 seconds = 300 units

So some pseudocode would look like this:

void UpdatePosition()
    float timeOld = getOldTime();
    float timeCurrent = getSystemTime();
    float difference = timeCurrent - timeOld;

    float rate = 100.0f; // 100 units every second, on X Axis
    float desiredAmount = rate * difference;

    Vector3 desiredVector(desiredAmount, 0, 0);

Now looking at the snippet of code you provided us:

variable1 -= variable2 * Time.deltaTime;
transform.position += new Vector3 (13, variable1 * Time.deltaTime, 0);

We can see that you have something similar, you have some sort of a rate defined as variable 2 multiplied by a change in time. You then proceed to take it away from some other var, variable1. That makes sense if you are treating variable 1 as like the momentum of the object or something, and are degrading it over time. However for some reason you are only doing this to the second value of the vector, or rather only its Y-Axis. Further, you're then multiplying it again by the rate. But none of this is done to that constant 13 you have for the X value

  • 1
    \$\begingroup\$ What are you trying to achieve during the example you gave us? Are you trying to make an object jump upwards, move side to side, or what? \$\endgroup\$ – Yattabyte Oct 11 '15 at 16:40
  • 1
    \$\begingroup\$ Well do what you are already doing with variable1 & variable2 for the Y axis, but instead create another 2 for the X axis (eg variable3 & variable4). variable1 -= variable2 * Time.deltaTime; varable3 -= variable4 * Time.deltaTime; transform.position += new Vector3 (variable3, variable1, 0); \$\endgroup\$ – Yattabyte Oct 11 '15 at 16:47
  • 1
    \$\begingroup\$ @Anonymous btw it is very bad practice to call your variables variable1234..., it would be a lot more readable if you used posX or something loke that... \$\endgroup\$ – xXliolauXx Oct 12 '15 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.