Hi everybody ! I had problems with Unity 5 and cars for a long time but I managed to get a working car pretty good with 6 gears and a top speed of 260 km/h.
If any of you want to achieve this, you should :
- Use a real-scale car model (if it’s too little or too big it works badly, this is very IMPORTANT)
- Apply around 1000-1500 kg of mass to the car rigidbody
- Apply around 60 kg of mass to each wheel
- For all wheels, default WheelCollider values for forward friction except stiffness to 2.
- For all wheels, default WheeCollider values for sideways friction.
Once you have this setup, you are ready to apply your equations and make the input control the car.
The basics of using RPM and gears need a pair of animation curves and some equations to work.
The RPM curve should look like this (for a high-end car) :
The Gear curve should look like this :
You can slightly modify those to match your desired settings.
Once you have all this ready, in your equations you should take in mind those curves and evaluate the value depending on the current RPM and the current Gear.
float WheelsRPM = (backRightWheel.rpm + backLeftWheel.rpm) / 2f;
float currentRPM = MinRPM + (WheelsRPM * FinalDriveRatio * GearCurve.Evaluate(currentGear));
On the first part you want to get the wheel RPMs as accurate as possible, so I will take both rpm from the back wheels and make the arithmetic mean.
Then, to calculate the current RPM we will need to sum up the minimum RPM for our car (in my case I have 1000 as minimum) to the formula.
This formula takes the Wheels RPM we calculated before and multiplies it by the Final Drive Ratio (in my case I have 4) and multiplies it by value depending on the gear (calculated from the gear curve).
Once you have the RPM formulas set up, we need the last formula.
float currentTorque = RPMCurve.Evaluate(currentRPM) * GearCurve.Evaluate(currentGear) * FinalDriveRatio * Input.GetAxis("Vertical");
So, in this formula, we’ll take in it the current RPM value we calculated before and evaluate it on the RPM curve. This value will be multiplied again by the evaluation of the current gear in the gear curve and by the final drive ratio.
Lastly, we’ll multiply it by the Input so it will only accelerate if you are pressing the input.
With all this calculated, we will now need to apply the torque to the back wheels. We’ll do that diving the torque by 2 and then applying it to the back left and right wheels. We divide it by 2 beacuse each wheel will get half of the torque. If we do an AWD car, we will divide it by 4 and apply the torque to all wheels.
With this all set, you have the basics ready. Now for the last part you need to implement the braking system, which should be relatively easy (when moving, instead of applying inverse motorTorque, you apply 0 motorTorque and some amount of brakeTorque to each wheel).
You’ll also need to add the gear system which is relatively easy and I’ll explain someday here too, but basically when your RPMs are above x value (for me 8400) I switch to a bigger gear. The gear switch has a delay of 0.6 and some sound effects.
I hope you get it working guys, I know it’s really frustrating because I’ve been there, but once you get it to work, you get a moral boost!