The goal of this project was to make a semi-realistic car physic model with Unity in C#. The model is based on true physic formulas but it’s tune to be fun and easier to drive than a true simulation.
This model was made using mainly this website which explain very well how a car works.
The algorithm include multiple parts and is modular. So it can be used to make a wide variety of vehicles.
The engine is a simple part. Every Update it get a “feedback RPM” from the wheels. This RPM is clamped to not get too low or too high using real world values.
With this value I use a torque Curve to evaluate the engine torque from the RPM. I increase / decrease this value according to the input of the player.
The gear box was a bit tricky to understand for me but I think my result is good.
The gear box can either be manual or automatic. It takes in input the engine torque and applies the current gear ratio and the final drive ratio to get the output torque.
The tricky part was to calculate the RPM ranges for each gear ratio in order to know when the automatic box should shift and also applies an efficiency curve for each gear so that the player can uses the efficiency spike of a gear at his advantage.
The steering wheel use a curve to clamp the steering angle depending of the speed. Because the higher your speed is the less your car can turn.
It also smooth the input for keyboard users so the car can be easier to drive on keyboard.
The drive train is a simple part that link the gear box with all the wheels.
It can be configured to be Front wheel drive, Rear wheel Drive or All wheel drive.
It takes in input the output torque of the gear box and then send it to the wheels according to the transmission type.
It also applies the steering angle to the wheels.
Wheels – Suspensions
This part is quite complex and tricky. It’s the part on which I spent the most of my time. I will try to keep it simple
The system uses a system of custom ray casts to detect the ground around the wheel. It sends ray casts around the wheel from it’s center. For performances issue we can change the number of rays used.
Tires uses frictions presets to simulates different surfaces (Tarmac, Gravel etc) and also have Forward and side Slip / Force Coefficient.
The suspensions uses a Spring + Damper system which can be customized to applies more or less force. The suspensions react to the collisions detected to the ray casts from the wheel and moves accordingly.
The length of the spring can also be customized.