# How do I correct the turning effects on my car in order for it to turn properly?

In my game, I'm currently working on a Physics-based car driving game in Unity from scratch. My problem is that whenever I press the input controls used for turning, instead of turning, my car does barrel rolls in the air. I'm using a document written about Car Physics by Marco Monster to aid some of my calculations in this post. I've looked at my code and suspect that the problem lies within this segment of my code. Please provide any feedback as it would be greatly appreciated.

//This section relates to low-speed cornering, not really useful for drifting
//This will determine the circle radius related to turning, in effect, calculating how much we turn
R = L / Mathf.Sin(userRight);

//Here, we calculate the vehicle's angular velocity in radians per second
//Omega (fancy w) represents angular velocity
// v represents velocity
rb.angularVelocity = rb.velocity / R;

//This section covers high-speed turning
//This relates to the car
//sideSlipAngle = Vector3.Angle(carTransform.forward, rb.velocity);
sideSlipAngle = Mathf.Atan(rb.velocity.z / rb.velocity.x);

//This relates to the wheels
for(int i = 0; i < 4; i++)
{
//alpha represents the slip angle for each wheel
alpha = Vector3.Angle(wheels[i].forward, rb.velocity);

//this equation is an approxiamtion of values below the peak shown in the graph
lateralForce = new Vector3(0f, 0f, corneringStiffness * alpha);

//These values are supposed to represent the magnitudes of these velocities
longVelocity = Mathf.Cos(alpha) * rb.velocity;
latVelocity = Mathf.Sin(alpha) * rb.velocity;

/*
Remember that these forces are supposed to be acting upon the wheels themselves,
so add rigidbodies accordingly
*/

//The lateral velocity causes the cornering force to counteract it
//This relates to only the front wheels
frontSlipAngle = Mathf.Atan((latVelocity.magnitude + (rb.angularVelocity.magnitude * b)) / longVelocity.magnitude) - (userRight * Mathf.Sign(longVelocity.magnitude));
for (i = 0; i < 2; i++)
{
//Calculates the lateral force of the wheels
latFrontForce = lateralForce.normalized * (frontWeight.magnitude / 2f);
latFrontForce += latFrontForce;

corneringForce = latRearForce + Mathf.Cos(userRight) * latFrontForce;
lateralForce += latFrontForce;

frontTorque = Mathf.Cos(userRight) * latFrontForce * b;
//rb_wheels[i].AddForce(lateralForce);
}

centripedalForce = corneringForce;
steeringRadius = centripedalForce.magnitude / (rb.mass * Mathf.Pow(rb.velocity.magnitude, 2f));

//This relates to only the rear wheels
rearSlipAngle = Mathf.Atan((latVelocity.magnitude - (rb.angularVelocity.magnitude * c)) / longVelocity.magnitude);
for (i = 2; i < 4; i++)
{
//Calculates the lateral force of the wheels
latRearForce = lateralForce.normalized * (backWeight.magnitude / 2f);

latRearForce += latRearForce;
lateralForce += latRearForce;

rearTorque = -latRearForce * c;
//rb_wheels[i].AddForce(lateralForce);
}

}

totalTorque = frontTorque + rearTorque;
rb.AddTorque(totalTorque);
$$$$

• This looks horrifying: rb.angularVelocity = rb.velocity / R;` "The faster I'm moving in a straight line, the faster I should spin around that line" isn't really how cars work. Are you sure you've transcribed this correctly? (Including the relevant excerpt from your source would help others check - is it this one?) – DMGregory Apr 9 at 13:08
• You may also want to consider using Unity's built-in WheelCollider component, which implements a lot of standard driving physics for you. – DMGregory Apr 9 at 13:12
• @DMGregory As for the source, here is the source: asawicki.info/Mirror/Car%20Physics%20for%20Games/… However, can you tell me if the WheelCollider is capable of allowing the car to drift and turn accordingly? – CobraCoder Apr 9 at 13:39
• – DMGregory Apr 9 at 17:27