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;
}