Car wheel script delay.

I am using this script for my car and for some reason that i really cannot figure out, there is a quite substantial delay from the time i hit the arrow buttons to when the car actually steers. Please help.

Script:

var rearWheel1 : WheelCollider;
var rearWheel2 : WheelCollider;
var frontWheel1 : WheelCollider;
var frontWheel2 : WheelCollider;
 
var wheelFL : Transform;
var wheelFR : Transform;
var wheelRL : Transform;
var wheelRR : Transform;
 
var steer_max = 20;
var motor_max = 40;
var brake_max = 100;
var steerSpeed = 20;
 
private var steer = 0;
private var forward = 0;
private var back = 0;
private var brakeRelease = false;
private var motor = 0;
private var brake = 0;
private var reverse = false;
private var speed = 0;
 
function Start() {
  print(steer); //Here is where is put it             *************
rigidbody.centerOfMass = Vector3(0, -0.05, 0);
}
 
function FixedUpdate () {
 
speed = rigidbody.velocity.sqrMagnitude;
steer = Input.GetAxis("Horizontal");
forward = Mathf.Clamp(Input.GetAxis("Vertical"), 0, 1);
back = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0);
 
if(speed == 0 && forward == 0 && back == 0) {
brakeRelease = true;
}
 
if(speed == 0 && brakeRelease) {
if(back > 0) { reverse = true; }
if(forward > 0) { reverse = false; }
}
 
if(reverse) {
motor = -1 * back;
brake = forward;
} else {
motor = forward;
brake = back;
}
if (brake > 0 ) { brakeRelease = false; };
 
rearWheel1.motorTorque = motor_max * motor;
rearWheel2.motorTorque = motor_max * motor;
rearWheel1.brakeTorque = brake_max * brake;
rearWheel2.brakeTorque = brake_max * brake;
 
if ( steer == 0 && frontWheel1.steerAngle != 0) {
if (Mathf.Abs(frontWheel1.steerAngle) <= (steerSpeed * Time.deltaTime)) {
frontWheel1.steerAngle = 0;
} else if (frontWheel1.steerAngle > 0) {
frontWheel1.steerAngle = frontWheel1.steerAngle - (steerSpeed * Time.deltaTime);
} else {
frontWheel1.steerAngle = frontWheel1.steerAngle + (steerSpeed * Time.deltaTime);
}
} else {
frontWheel1.steerAngle = frontWheel1.steerAngle + (steer * steerSpeed * Time.deltaTime);
if (frontWheel1.steerAngle > steer_max) { frontWheel1.steerAngle = steer_max; }
if (frontWheel1.steerAngle < -1 * steer_max) { frontWheel1.steerAngle = -1 * steer_max; }
}
frontWheel2.steerAngle = frontWheel1.steerAngle;
wheelFL.localEulerAngles.y = frontWheel1.steerAngle;
wheelFR.localEulerAngles.y = frontWheel2.steerAngle;
 
wheelFR.Rotate(frontWheel1.rpm * 5 * Time.deltaTime, 0,0 );
wheelFL.Rotate(frontWheel2.rpm * 5 * Time.deltaTime, 0,0 );

It appears so, yes. I just tested it, too. So basically, you have a variable, steer, which is a float that goes from -1 to 1, and seems to lerp back to 0 when you release the key. Then you have another variable, steerSpeed, which is set to 20, and then you have Time.deltaTime, which is always 0.02, which is probably 0.016666… rounded up, because the FixedUpdate runs 60 times per second. Let’s imagine you’re turning left, i.e. steer runs from 0 to -1. The first value I get is -0.02985108, and on the second call to FixedUpdate, it is -0.07792716, and so on.

So, the first time your FixedUpdate runs, it sets frontWheel1.steerAngle to -0.02985108 * 20 * 0.02 = -0.011940432. That’s a very, very small angle.

Then, next time it runs, it subtracts -0.07792716 * 20 * 0.02 = -0.031170864, making the total angle to turn the wheels by equal -0.011940432 + -0.031170864 = -0.043111296. That’s also a small angle, and by now, 2/60 of a second has passed.

I ran this calculation myself and printed stuff to the console while I was at it with these simple few lines of code:

float val = 0;
int frames = 0;

void FixedUpdate()
{
    val += Input.GetAxis("Horizontal") * 20 * Time.deltaTime;
    frames++;
    Debug.Log("Value: " + val + ", Frames: " + frames);
}

It took 34 frames (34 calls to FixedUpdate) just to get the value to -10.3. This means it took a little over half a second just to save up an angle of 10 degrees, and it took 57 calls to FixedUpdate before the angle became 20 (the value of max_steer), which means you have to press and hold the left arrow key for almost a full second, before the wheels are turned as far to the left as they will go.

So, there you have it. The reason you have a delay in turning your wheels is simply that the angle with which you turn the wheels adds up too slowly, particularly in the beginning. To fix it, increase the value of steerSpeed, or use a non-linear function that turns quickly in the beginning and slower towards the end, such a logarithmic function, for instance.