DaniDevy / FPS_Movement_Rigidbody

A physics based movement system
MIT License
492 stars 642 forks source link

Incorrect usage of Time.deltaTime in movement. #1

Closed Epicguru closed 4 years ago

Epicguru commented 4 years ago

In the character movement script, all of the forces are added by multiplying by Time.deltaTime. Instead, Time.fixedDeltaTime should be used because the FixedUpdate method runs at a constant frequency unlike Update.

The current code makes the character accelerate much slower when you have high FPS, and much faster when you have high FPS.

Dani if you see this you may want to also fix it in your main game because it will give players with potato PC an advantage because they run at lower framerate.

TheCoder5550 commented 4 years ago

"accelerate much slower when you have high FPS, and much faster when you have high FPS." Shouldn't one be lower FPS?

DaniDevy commented 4 years ago

I'm not sure if this is a common misconception, or if it actually holds some truth. But I've already tested this extensively, both with Time.deltaTime & Time.fixedDeltaTime, and I've seen the best results / accruacy over all FPS ranges, by using deltaTime.

According to the official Unity documentation, when comparing fixedDeltaTime & deltaTime used in FixedUpdate() :

For reading the delta time it is recommended to use Time.deltaTime instead because it automatically returns the right delta time if you are inside a FixedUpdate function Source

So that's my reasoning for using Time.deltaTime; it is automatically adjusted to work appropriately for both Update and FixedUpdate. But if you have any evidence or proof against this, I'd like to see it, as I want the game to be as accurate as possible.

Thanks!

Epicguru commented 4 years ago

Oh you're completely right. I simply assumed that deltaTime did not work correctly because all Unity documentation examples use fixedDeltaTime.

I suppose this issue can be closed then. Thanks for the prompt response!

Bamboy commented 4 years ago

Anytime Unity refers to "Fixed", it means the updating of the physics engine.

Time.fixedDeltaTime returns the time since the last loop of the physics engine and FixedUpdate() Time.deltaTime is the time since the last Update() loop, which also happens to be the time since the last frame was rendered.

I haven't looked into the script much other than a quick glance, but since it is using a rigidbody, you probably want to use fixedDeltaTime when forces are actually applied. For input you want to use normal Update() for responsive controls.

DaniDevy commented 4 years ago

I'm using Update() for Input.

I've looked more into the matter, and found that when Time.deltaTime is used within FixedUpdate(), it will return the fixedDeltaTime value. So it apparently doesn't matter which one is used, thus I'm not sure why the Unity documentation states that you should use Time.deltaTime.

Bamboy commented 4 years ago

Unity documentation is ass in terms of "best practices". Best example I can give is them telling you to use rigidbody everywhere, where in my experience it's nigh impossible to tame the physics engine to do what you want. I've gotten to the point where I just tell my clients to only use rigidbody if they want physics behaviour in the purest sense, otherwise avoid it like the plague.