Cannot get consistent movement speeds even using Time.deltaTime! (849756)

Ok, so I have never run into such a massive issue with such a basic thing. I have been running some tests in an effort to get the movement of various objects to be consistent even during frame rate drops. But to no avail. Yes, I have seen all the threads about using fixedupdate if altering transform values to move and if a rigidbody2d is attached to instead changed the values on the rigidbody2d itself. BUT NEITHER WORKS ACCURATELY!

So I setup some simple code that runs during a 50 frame animation (sample speed of 60; animation speed of 1). At the end of the animation the object is disabled via the script set in an animation event on the animation timeline. When I click the box to reenable it the y position is reset to 0.

I first tried using a rigidbody2d component (no scripting involved). I set mass and both drags to 0, then set the gravity to -20. During the frame rate drop: the object moves y wise from 0 to 88.5 consistently. However, when the frame rate returns to its smooth normal rate, the object moves from 0 to either 70.8 OR 74.2 instead!? Which makes no sense as it should be ending in the same position each time. SMH.

So I tried the alternative: moving by altering the y value in the transform instead! Removing the rigidbody2d component (or setting it to kinematic; same results either way) and then using code in fixedupdate or update to move the object.

  • With time.deltatime in fixed update: during the framerate drop it goes from 0 to 18.8. When the frame rate returns to normal, from 0 to either 16.8 OR 17.2. Still no good.

  • With time.deltatime in update: during the framerate drop it goes from 0 to 18.8. When the frame rate returns to normal, from 0 to between 17 thru 17.2 (So 17.01, 17.1674, etc.). So I get a small range it can land on instead of the same values over and over. Still no good as with a decent size speed this can cause an extra few pixels movement.

Now one last test: taking out time.deltatime:

  • In FixedUpdate: during the framerate drop it goes from 0 to 940. When the frame rate returns to normal the y value goes from 0 to either 840 or 860. Not good.

  • In Update: resulting y values are of course all over the place. Expected.

I am at a total loss here. How can I remedy this to ensure that during an animation the movement speed is consistent? For Example: if the player is jumping he should always move 50 pixels up during his jump animation.

I hope I am being clear and detailed enough here. Thanks in advance.

//SIMPLEST CODE EVER

void FixedUpdate()
{
   transform.position += new Vector3(0, speed.y * Time.deltaTime, 0);
}

void OnEnable()
{
   transform.position = Vector3.zero;
}

You’ve got that completely backwards.

Time.deltaTime in FixedUpdate always returns the value you’ve specified in Project Settings → Time → Fixed Timestep. In Update it returns the time it took to process the previous frame.

50 frames is not a consistent amount of time. Depending on your framerate that might take half a second or it might take 2 seconds. The better test is to use a consistent amount of real life time.

Anyway I don’t really understand your test. You’re talking about an animation, which seems like your motion is controlled by an Animator. But then you’re also talking about scripting and Rigidbodies. So what are you actually doing?

This code should result in a visually smooth motion:

public Vector3 speed = Vector3.up;

void Update() {
  transform.position += speed * Time.deltaTime;
}

Corrected that little bit of my post. TY.

I had forgotten this existed. TYVM. This helps to improve my results to be more consistent regardless of frame rate. But there is still some variance.

The thing I need to make clear: this object has a 52 frame animation. At which point it is disabled. With the fixed and max fixed settings under time set to the same value that means that the object should be moved 52 times IF I am setting the transform.position value via fixed update.

But for some reason it sometimes runs 51 times. Which is very odd.

I never said it was. :slight_smile: But using real life time would not make sense here.

Let’s say we have a player object who jumps. I know I want him to move 50 pixels upward EXACTLY during his jump animation. If I used real life time and the animation slowed down due to the game’s framerate decreasing, his vertical motion would end BEFORE the jump animation ended. Which could cause problems and would NOT look right. The idea is to sync the speed and the animation so he cover those 50 pixels during the animation. Get it?

I never said anything about motion being controller by the animator.

That is literally the same code I have above. Except written in different syntax and you used update. Which I also said I tried before and gave the results in detail above. But the issue remains even with the fixed timestep and max allowed timestep correct: sometimes the position is getting incremented 1 less time. This will also happen if I use addforce or change the velocity on the rigidbody2d. Which is very odd. So I’m guessing the culprit is actually mecanim.