- Home /
Odd character dash discrepancies. Time.deltaTime?
Hey guys, so here's the deal. I have a dash counter, dashTimeMax (how long a dash should take. set to 0.5 seconds) and a dash speed, dashSpeed, of 4 mps. So, in theory, I'm expecting my character to move 2 meters after 1 dash execution. Thing is, I'm getting results that go from 1.92 (maybe less) meters to 2.08 meters, and I don't know why?
Here's my code:
On Update:
void UpdateDashStatus()
{
if(grounded && downButtonPressed && jumpButtonPressed && !isDashing)
{
isDashing = true;
jumpButtonPressed = false;
dashTimeCounter = dashTimeMax;
}
if(isDashing)
{
dashTimeCounter = dashTimeCounter - Time.deltaTime;
if(dashTimeCounter <= 0f)
{
//print (Time.deltaTime);
//print (transform.position.x);
isDashing = false;
}
}
}
On FixedUpdate:
void PerformDash()
{
if(isDashing)
{
if(facingRight)
rigidbody2D.MovePosition(rigidbody2D.position + new Vector2(dashSpeed, 0) * Time.deltaTime);
else
rigidbody2D.MovePosition(rigidbody2D.position + new Vector2(-dashSpeed, 0) * Time.deltaTime);
}
}
Shouldn't the uses of deltaTime, and of PerformDash() inside of FixedUpdate be giving me sharp results?
If needed, I'll try to provide further details. Thanks.
Answer by Tehnique · Jul 17, 2014 at 06:44 AM
Well, you do execute your dash OnFixedUpdate, and you update your condition on Update. This can cause certain discrepancies, especially for small values of dashTimeMax, like your case.
For example at Update, you check dashTimeCounter and it is >0, so you keep isDashing as true. Later, on FixedUpdate, you execute your dash, but more time has passed since Update, so maybe your dash should have ended, but your condition was evaluated earlier.
You could try to move your Update code to FixedUpdate to decrease your error.
I've done as you've said, Tehnique, passing just the lines:
dashTimeCounter = dashTimeCounter - Time.deltaTime;
if(dashTimeCounter <= 0f)
{
isDashing = false;
}
to FixedUpdate, since the other part was Input handling, and it worked like a charm. Thank you very much!
On a sidenote: The example presented here, using deltaTime without physics manipulation and inside Update:
http://docs.unity3d.com/ScriptReference/Time-deltaTime.html
It isn't 100% accurate (small differences, though). Is that normal behaviour?
I'm glad it worked. As for your last question, do you mean that in the example, the object does not move 10m/s?
I'm guessing it is normal to have small differences, depending on when you measure the movement (if it is between updates, it's normal).
To clarify a few things: FixedUpdate should run every 0.02 seconds, at least that's how Unity tries to run it. Update, on the other hand runs every frame. So, if you are above 50 FPS, Update runs more often, if you are below, FixedUpdate runs more often. That's jsut a comparison, the 2 functions are not interchangeable, especially because FixedUpdate should be kept very lightweight, and only for Physics stuff.
Yes, I was saying that the example, as presented on that link, always demonstrates a little discrepancy. If I print time.Time and transform.position.x both inside Update, it reaches 10+ (let's say 10.1) at 0.9. I don't see how this is possible, since I'm using deltaTime? Where does the remainder came from? Also, thanks for the information about FixedUpdate and Update.
Time.time is only calculated once/frame and it returns the same value throught the frame. So your update can come later in the frame, but Time.time will return the same value that was calculated before, hence Update moves the transform according to the real time since last frame, but Time.time returns a smallver value of time.
Answer by Guambo1 · Mar 09, 2020 at 09:21 PM
I had a similar issue. I moved my dash related code to fixedupdate and now I get a consistent dash distance. However, now the player stutters when they dash. Have you had a similar issue? @haskell33