# How can I adjust steer angle based on speed in Unity?

I'm following this (https://www.youtube.com/watch?v=kAEpLX3rfms) video on how to make a car in Unity. But my car doesn't lower the steering angle based on the speed of the video. The idea was that you can steer the car at a max of 50 degrees at the slowest and then 10 degrees at the fastest. But it's having no effect on the car. Could someone show me how to fix this?

Code:

public WheelCollider wheelFL;
public WheelCollider wheelFR;
public WheelCollider wheelRL;
public WheelCollider wheelRR;

public Transform wheelFL_Trans;
public Transform wheelFR_Trans;
public Transform wheelRL_Trans;
public Transform wheelRR_Trans;

public float maxSpeed;
public float lowSpeedSteerAngle;
public float highSpeedSteerAngle;
public float Torque;
public float centerOfMassHeight;

private float currentSteerAngle;
private Vector3 com;
private Rigidbody rigidBody;

void Start () {
rigidBody = GetComponent<Rigidbody>();
com = rigidBody.centerOfMass;
com.y = centerOfMassHeight;
rigidBody.centerOfMass = com;
}

void FixedUpdate () {
wheelRL.motorTorque = Torque * Input.GetAxis("Vertical");
wheelRR.motorTorque = Torque * Input.GetAxis("Vertical");

//Here is the part with the issues

float speedFactor = GetComponent<Rigidbody>().velocity.magnitude / maxSpeed;
currentSteerAngle = Mathf.Lerp(highSpeedSteerAngle, lowSpeedSteerAngle, speedFactor) * Input.GetAxis("Horizontal");
wheelFL.steerAngle = currentSteerAngle;
wheelFR.steerAngle = currentSteerAngle;
}

void Update () {
wheelFL_Trans.Rotate(0, 0, wheelFL.rpm / 60 * 360 * Time.deltaTime);
wheelFR_Trans.Rotate(0, 0, wheelFR.rpm / 60 * 360 * Time.deltaTime);
wheelRL_Trans.Rotate(0, 0, wheelRL.rpm / 60 * 360 * Time.deltaTime);
wheelRR_Trans.Rotate(0, 0, wheelRR.rpm / 60 * 360 * Time.deltaTime);

Vector3 wheelSteerAngle = wheelFL_Trans.localEulerAngles;
wheelSteerAngle.y = currentSteerAngle + 90;
wheelFL_Trans.localEulerAngles = wheelSteerAngle;
wheelFR_Trans.localEulerAngles = wheelSteerAngle;

if(Input.GetKeyDown(KeyCode.P)){
Vector3 resetPosition = new Vector3(transform.position.x, 1.5f, transform.position.z);
Quaternion resetRotation = Quaternion.Euler(0f, 0f, 0f);

//Reseting Velocity
GetComponent<Rigidbody>().velocity = new Vector3(0f, 0f, 0f);

//Reseting Rotation
transform.rotation = resetRotation;

//Reseting Position
transform.position = resetPosition;

//Reseting Motor Torque and Steering Angle
wheelFL.motorTorque = 0f;
wheelFR.motorTorque = 0f;
wheelRL.motorTorque = 0f;
wheelRR.motorTorque = 0f;

wheelFL.steerAngle = 0f;
wheelFR.steerAngle = 0f;
wheelRL.steerAngle = 0f;
wheelRR.steerAngle = 0f;
}

}

• I've answered this question of yours on Stackoverflow: stackoverflow.com/a/32804724/4038191 – EvilTak Sep 27 '15 at 5:48
• No effect, the car still turns the same amount at high speed as it does at low speed. – mr-matt Sep 27 '15 at 6:09

Try adapting this code to the area that you marked with the issues. This algorithm should give the amount that you should be able to turn given the speed. You will just have to apply the turnAmount to the car.

float maxTurn = 50, minTurn = 10;

float speedFactor = 1 - (GetComponent<Rigidbody>().velocity.magnitude / maxSpeed);
float currentMaxTurnAngle = minTurn + ((maxTurn - minTurn) * speedFactor);
float turnAmount = currentMaxTurnAngle * Input.GetAxis("Horizontal");


This should yield 10 at max speed and 50 when not moving.

I suggest increasing the turn values, and then multiplying the turnAmount by Time.deltaTime so that the turning will work more consistently whatever the framerate.

• That is what the OP is doing. And the second part of your answer where you recommend to multiplying the turnAmount by Time.deltaTime will not work. deltaTime for a game is usually around 0.016 (@ 60 FPS) so that way you'd have to keep your maxTurn * 60 and minTurn * 60. Which is useless, since the 1 / 60 (deltaTime) and the 60 would cancel out each other. – EvilTak Sep 27 '15 at 5:47
• No effect, the car still turns the same amount at high speed as it does at low speed. – mr-matt Sep 27 '15 at 6:09
• @EvilTak Yeah I guess you're right. He marked that area as having the issue so I just assumed it didn't work. I literally just did the exact same thing in a different way. I'm guessing the problem is somewhere else then? – Daniel Sep 27 '15 at 7:29
• Yeah, I guess. @MatthewInglis hasn't replied to my comments on my answer to his Stackoverflow question. – EvilTak Sep 27 '15 at 7:34