# Car handling

Hello

I am trying to create a car simulation game, and have some problems with the handling of the car. The main problem being that if i speed up the car to about 200 rpm, and just slightly turn, the car flips. Now this is not exactly the behavior i was looking for, so i am trying to either limit the steering angle, based on rpm, or make the car slip when the steering angle becomes too high, i just can’t seem to figure out how to do it, anyone have any suggestions?

This is my car handling source code so far:

float multiplier = 10.0f * Time.deltaTime;
float steer = FrontLeftWheel.steerAngle + (Input.GetAxis("Horizontal") * multiplier);

if (horizontalSteering != 0)
{
if (steer < 20  steer > -20)
{
FrontLeftWheel.steerAngle = steer;
}
}
else
{
if (FrontLeftWheel.steerAngle > 1)
FrontLeftWheel.steerAngle -= multiplier * 2;
else if (FrontLeftWheel.steerAngle < -1)
FrontLeftWheel.steerAngle += multiplier * 2;
else if (FrontLeftWheel.steerAngle < 1  FrontLeftWheel.steerAngle > -1)
FrontLeftWheel.steerAngle = 0;
}

Hi, welcome to the forum!

A common way to control the steering angle is to set a large angle (say 45º) for low speed and a small angle (say 15º) for high speed. Then, it is simply a matter of interpolating between the two angles depending on the speed the car is travelling. If you divide the car’s current speed by a nominal top speed value, you get a fraction between 0 and 1 that you can pass to Mathf.Lerp:-

var topSpeed: float;
var lowSpeedAngle: float;
var highSpeedAngle: float;

function FixedUpdate() {
...
var fracTopSpeed: float = rigidbody.velocity.magnitude / topSpeed;
var steerAngle: float = Mathf.Lerp(lowSpeedAngle, highSpeedAngle, fracTopSpeed);
steerAngle *= Input.GetAxis("Horizontal");
// Set steering angle for WheelColliders...
}