- Home /
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 );
No one is willing to read a mad jumble of code. Format your code properly using the "101010"-button, or no one will help you.
Do not post the same question again (with worse formatting), but hit the 'edit' button at the lower left of your post, then select all your code and hit the 101010-button as christian is telling you.
Sorry guys, i couldent see the edit button cause i kept on getting logged out. Anyways figured it out. Please help.
It looks to me like it really should begin steering at once, since already at the first call to your FixedUpdate, it begins incrementing the steerAngle of both wheels by the product of the steer variable and the steer speed.
Not knowing the relative values of steer and steerAngle, it's hard to deter$$anonymous$$e just from looking at the code what it is that goes wrong, but I have a couple of "shot in the dark"-suggestions. First, print out steer to the console along with the steerAngle after the frontWheel1 has been incremented. Do they acquire values that you expect? Secondly, might some values be integers, when you actually expect them to be floats? Perhaps the delay you're seeing is happening because the steerAngle gets calculated to some value between 0 and 1 for a while to begin with, but gets truncated to 0 because the system thinks it's an integer? If that is the case, then the steering would seem "jerky" because the angle always has discrete, integer values assigned to it.
All in all:
Try to explain in greater detail how the steering behaves. What happens if you press and hold one arrow key for a while? How does it behave when you release the key? Does it correct itself properly?
Thank you very much, ill be looking into what you said. and i have put it on a server for you guys to play so you can get a first can see what the problem is:
You will of course need the unity plug in raws.adc.rmit.edu.au/~s3286249
Answer by CHPedersen · Sep 02, 2011 at 11:15 AM
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.
Thank you very much Christian. Though incresing the steer speed does nothing to the delay. And i dont trust my self to write this logarithmic function. it sounds like hell. Sorry if im being a pain. But could you explain how i would go about writing it?
Forget about that for now. Ins$$anonymous$$d, try to write a simple if-sentence, that sets steer to -1 or 1 immediately, depending on whether it is positive or negative to begin with, ins$$anonymous$$d of waiting for consecutive calls to FixedUpdate to wind it up to -1/1.
Sure, that's what I said. :P But you don't want to set it to -1 all the time, since -1 is only when you're turning left. You want it to be +1 when you're turning right. So, in a frame where it is positive, set it to 1, and in a frame where it is negative, set it to -1.
So say in one of the else statements to do with the steering angel?
Your answer
Follow this Question
Related Questions
car script / powering wheel colliders 1 Answer
Simple steering wheel rotation in a car 1 Answer
WheelCollider - real friction values 0 Answers
in Unity 3.5.7 car starts to shake 0 Answers
can't slow my car down 2 Answers