Car handling

I’m making a racing game, and I’m not happy with the handling of my car . .What can I do??

var steer_max=20;
var steerSpeed=20;
private var steer=0;
function FixedUpdate() {
steer=Input.GetAxis("Horizontal");

if ( steer == 0  FrontLeftWheel.steerAngle != 0) {
if (Mathf.Abs(FrontLeftWheel.steerAngle) <= (steerSpeed * Time.deltaTime)) {
FrontLeftWheel.steerAngle = 0;
} else if (FrontLeftWheel.steerAngle > 0) {
FrontLeftWheel.steerAngle = FrontLeftWheel.steerAngle - (steerSpeed * Time.deltaTime);
} else {
FrontLeftWheel.steerAngle = FrontLeftWheel.steerAngle + (steerSpeed * Time.deltaTime);
}
} else {
FrontLeftWheel.steerAngle = FrontLeftWheel.steerAngle + (steer * steerSpeed * Time.deltaTime);
if (FrontLeftWheel.steerAngle > steer_max) { FrontLeftWheel.steerAngle = steer_max; }
if (FrontLeftWheel.steerAngle < -1 * steer_max) { FrontLeftWheel.steerAngle = -1 * steer_max; }
}
FrontRightWheel.steerAngle = FrontLeftWheel.steerAngle;
WheelFL.localEulerAngles.y = FrontLeftWheel.steerAngle;
WheelFR.localEulerAngles.y = FrontRightWheel.steerAngle;}

Help me guys, I can’t understand, how to get good handling . . . . . .also, please give me the best available wheelfrictioncurve setting .

Thanks in advance . . .

Please help guys . . .

Can you give some more information about the problem with the car’s handling? Saying you’re not happy with it, doesn’t really give much to work with :wink:

Try this, but without the project, it’s very hard.
I utilize c#, so i don’t know if it is correctly.

	var steer_max = 20;
	var steerSpeed = 20;
	var changed = false;
	private var steer = 0;
	function FixedUpdate() {
		steer = Input.GetAxis("Horizontal");

		if (steer != 0 ) {
			FrontLeftWheel.steerAngle += (steer * steerSpeed * Time.deltaTime);
			changed = true;
		} else if (FrontLeftWheel.steerAngle != 0) {
			if (Mathf.Abs(FrontLeftWheel.steerAngle) <= (steerSpeed * Time.deltaTime)) {
				FrontLeftWheel.steerAngle = 0;
			} else {
				FrontLeftWheel.steerAngle += (Mathf.Sign(FrontLeftWheel.steerAngle) * (steerSpeed * Time.deltaTime));
			}
			changed = true;
		} else {
			changed = false;
		}

		if (changed) {
			FrontLeftWheel.steerAngle = Mathf.Clamp(FrontLeftWheel.steerAngle, -steer_max, steer_max);

			FrontRightWheel.steerAngle = FrontLeftWheel.steerAngle;
			WheelFL.localEulerAngles.y = FrontLeftWheel.steerAngle;
			WheelFR.localEulerAngles.y = FrontRightWheel.steerAngle;
		}
	}

@andee :

Actually, its not handling like, the way,it does in any professional racing game. What I think, really is the problem, is car’s turning fine at slow speeds, upto 50 mph, but afterwards, a little input,causes it to be steered more and this happens very quickly and it’s out of control at this time.

@andrea85cs :

Thanks mate, but, I think, the code has some problems, car ,once steered, stays steered, even after release of input, although , we can steer it to opposite direction .But it stays there,even after the release of input.

After a detailed search, I collected a few points, like center of mass needs to be tweaked, so,to get , the right feel on the car. If center of gravity is left shifted a little on high speed right hand cornering n’ like that, this way, we can control the weight distribution .On brakes weight needs to on front end, and on acceleration, it should behind ,the on rest, center of gravity(ie., in rear end) .I hope this is how it needs to be done. I am about to try it.Wish me luck

I’m sorry but I don’t try the code. Infact i see the error :

FrontLeftWheel.steerAngle += (Mathf.Sign(FrontLeftWheel.steerAngle) * (steerSpeed * Time.deltaTime));

The right code is:

FrontLeftWheel.steerAngle += -(Mathf.Sign(FrontLeftWheel.steerAngle) * (steerSpeed * Time.deltaTime));

Good, it steers now. But, at high speeds, cars turning too much. what should I do, to lower the steerangle according to the velocity . . .

Have a high speed steering angle and a low speed steering angle. Then, use Mathf.Lerp to vary between them depending on the car’s speed:-

var highSpeedSteer = 20.0;
var lowSpeedSteer = 45.0;
var highSpeedLevel = 50;

function Update() {
    var fwdSpeed = rigidbody.velocity.magnitude;
    var highSpeedFraction = fwdSpeed / highSpeedLevel;
    var actualSteerAngle = Mathf.Lerp(lowSpeedSteer, highSpeedSteer, highSpeedFraction);

    ... steering code based on actualSteerAngle ...
}
1 Like

Ya, I used this, but, it still gives very quick turns on high velocities, which is soooo not real. :frowning:

When you drive a real car and go really fast, you turn the steering wheel up to a few degrees. When going slowly, up to 900 degrees can be used. The hard part is to let a driver of your game turn the wheels pressing keyboard keys for certain amounts of time. There is no universal solution to this but andeeees solution should work very well, but then it is not possible to turn the wheels a lot in high speed, no matter how long you keep the button pressed. I recommend that you change the sensitivity and gravity of the Input, based on vehicle speed.

Another thing… You say that the vehicle turns unnaturally in high speeds. Maybe your tires should have less lateral traction.

1 Like

This is my version…

// 50 is the max velocity 
private var highSpeedFactor : float = 1.0 / 50.0;

function FixedUpdate() {
    var fwdSpeed = transform.InverseTransformDirection(rigidbody.velocity);
    // I'm presuming that z is car forward.
    var highSpeedFwd = fwdSpeed.z * highSpeedFactor;
    var steerVel = 1 - Mathf.Clamp01(highSpeedFwd);
	
   ... (steerVel * steer * steerSpeed * Time.deltaTime)...

    ....

Sorry about the late reply, but . . . . .I tried all those techniques…but nothing seems to be so good .Can you tell me, any tutorial with perfect handling, with/without wheel colliders ,something as good as HelloRacer .

Unity Studios is working for a vehicle tutorial for Unity3D. You can see it in the end of this video:

I don’t know when it will be released, though.

Also, I googled “unity3d car tutorial” and found those:

http://www.gotow.net/andrew/blog/?page_id=78
http://learnunity3d.com/coding/awesome-unity-car-tutorial
http://forum.unity3d.com/viewtopic.php?t=12740

…among others.

Hope this helps. Good luck!

/Ricky

Your code works great except you have the high low speeds backwards in the Lerp