- Home /
Does Time.deltaTime not work in FixedUpdate?
I have a vehicle powered by motorTorque with wheel colliders but when I add Time.deltaTime to my script the vehicle doesn't move:
void FixedUpdate(){
gas = Input.GetAxis("Horizontal");
frontLeftWheel.motorTorque = enginePower * gas * Time.deltaTime;
frontRightWheel.motorTorque = enginePower * gas * Time.deltaTime;
rearLeftWheel.motorTorque = enginePower * gas * Time.deltaTime;
rearRightWheel.motorTorque = enginePower * gas * Time.deltaTime;
Does Time.deltaTime not function with FixedUpdate or is there something else I'm not getting?
Answer by s_guy · Sep 27, 2013 at 12:54 AM
It works. That is, it gives the correct amount of time for that FixedUpdate(). An elaboration...
Time.fixedDeltaTime is meant for use in FixedUpdate(). However, per docs for Time.deltaTime "When called from inside MonoBehaviour's FixedUpdate, returns the fixed framerate delta time."
http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html
So, you can just use Time.deltaTime for both Update() and FixedUpdate().
In your example, when you multiply by Time.deltaTime (which will be a small number less than 1.0), you're probably reducing torque to the point where it can't overcome friction or some other force. You'll need to use more enginePower to achieve the same resultant torque in game.
Alternatively, you could remove the Time.deltaTime factor from your torque equation, but you'd have to retune your power values if you ever changed the FixedUpdate() frame rate.
That makes sense and clears some things up for me. Thanks!