Tuesday, December 07, 2010

Unity3D WheelCollider and motorTorque.

So, you've created a car prefab using WheelCollider components, and now you can apply a motorTorque to make the whole thing move along.

But how do you calculate vehicles for the motorTorque which make the vehicle reflect reality?

Here are my current ideas on the best way to do this.

1. Create a "torque curve" for your engine. It should run from 0 - 7000 (which reflects your engine RPM) and the height should peak at the maximum power output (in Newton Metres) of the engine. . The below image shows a torque curve which peaks at around 450 newton metres and then sharply drops off near 6000 rpm, just like a real engine. Use an AnimationCurve for this.

2. Set up your masses. The car should weigh between 1000-3000 kg. Set this in the mass property of your rigidbody component. It is also very important to set the mass of your wheels (WheelCollider.mass) to something reasonable. Most car wheels weigh between 15-30 kilograms.

3. Create another AnimationCurve variable, which we will use for gear ratios. Gear 0 should be neutral, and gear -1 should be negative to enable a reverse gear. A typical(?) set of gear ratios are below. Add another float variable which is the "final drive ratio" and set it somewhere between 3 and 4.

4. Every frame, you need to calculate the RPM of the motor. To do this use this formula:
motorRPM = minRPM + (wheelRPM * finalDriveRatio * gearRatios.Evaluate(gearIndex));

minRPM is usually set to 700-1000, depending on the idle RPM of the engine. You can get wheelRPM from your WheelCollider components.

5. Finally, calculate the motorTorque.
totalMotorTorque = torqueCurve.Evaluate(motorRPM) * gearRatios.Evaluate(gearIndex) * finalDriveRatio * accel;

If you have four drive wheels, set each wheel's motorTorque to totalMotorTorque / 4. If you have only two drive wheels (front or rear wheel drive), set each wheels motorTorque to totalMotorTorque / 2. accel is a float variable which specifies how far the accelerator cable is depressed, and should be between 0 and 1.

6. Fill in the gaps. You need to add a mechanism to change the gearIndex value (which specifies which gear the car is in) and a mechanism to modify the accel value.

7. Tweak the curves. Just like a real car, minor adjustments to weight, torque curves or gear ratios can have a dramatic impact on the car's performance. Don't be surprised if you get crazy behavior from seemingly minor modifications!


wearereasonablepeople said...

Thank you so much for this brilliant post!

penisteeth said...

1st off, excellent blog, just stumbled onto it...

Concerning the animation curves...how exactly are you setting these up? are you creating a new game object and putting them on that, or are you putting this directly on the car? i've never worked with animation curves before in Unity so it is kinda throwing me for a loop.

Anyhow thx for any info/clarification

penisteeth said...

Also, how were you able to get the animation window to display a time of -1 seconds within your 'gears' pic?

penisteeth said...

"Create another AnimationCurve variable" ... dur!

Mukah said...

Thank you so much! Helped me a lot!

Mukah said...

Thank you so much, helped me a lot!

Unknown said...
This comment has been removed by the author.
ViFFeX said...


I know this has been posted about 5 years ago, but I had a question which I hope you can help me with

When going from the first gear to the second, the acceleration stalls.

So the first gear has a gear ratio of 4.25 and the second has a gear ratio of 3.10

When you plug those numbers in the calculation of totalMotorTorque

You get totalMotorTorque = torqueCurve.Evaluate(motorRPM) * 4.25 * finalDriveRatio * accel;

totalMotorTorque = torqueCurve.Evaluate(motorRPM) * 3.10 * finalDriveRatio * accel;

Now, I'm guessing that this is correct, since when you're shifting up, your torque will be lower (at least, that's how I understand what happens)
However, if I have the totalmotortorque applied to my wheelcolliders, I get a speed of ~50 km/h in first gear, but once I go to second gear, my speed doesn't go up. I stay around the 50 km/h speed, and my RPM doesn't go up either.

Do you have any insight on this?

Anonymous said...

Excellent post

but one and important question is that in first Gear the torque or acceleration is limited to specific Speed e.g 10km/h put the power of vehicle is more, so how can we do it in Unity3d?

if we increase torque it move very fast and if we decrease torque then there will be no power. So the main question is that how can we control RPM on high motorTorque in unity.

Popular Posts