Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wheel Collider Unity: after adding torque the vehicle starts moving sideways

Tags:

unity3d

I really killed few hours to try to fix this, Googling for a solution, but I could not.

I've got a vehicle, it's a go-cart, so there are no suspensions (technically there are, but the values are close to 0, to simulate the tires). The vehicle has a rigid body attached and the child object contains the 4 wheel colliders (and the model itself), as can be seen here:

https://dl.dropboxusercontent.com/u/76599014/sxc/Clipboard01.jpg

For testing, I added a short script to make the vehicle move. It's in the GameObject called "gokart":

public class carControler : MonoBehaviour {

public WheelCollider fr, fl, rr, rl;
public float performance = 50f;

void FixedUpdate () {
    throttle();
}

public void throttle() {

    this.rl.motorTorque = this.performance;
    this.rr.motorTorque = this.performance;
} }

What happens is: the rear wheels start to rotate, as intended, but the vehicle starts moving sideways slowly. The movement speed depends on the torque amount (the wheel rotation in this case). There is no movement forward, so this is not the bug, where when you are standing on a flat surface you are drifting on the sides.

Any ideas? If you need a video or a GIF (I have to figure out how to make one) of the movement, I'll be glad to provide one.

like image 524
Erik Putz Avatar asked May 20 '15 22:05

Erik Putz


1 Answers

I think you should try and apply 0 brake torque to the front wheels while applying motor torque to the rear wheels.

public void throttle() {

this.rl.motorTorque = this.performance;
this.rr.motorTorque = this.performance;
this.fr.brakeTorque = 0f;
this.fl.brakeTorque = 0f;
} 

That being said, anything could go wrong if the Rigidbody/wheelcolliders aren't set up correctly. Unity's wheel colliders can be difficult to set up and work with. Unity changed the physics in Unity 5 so most documentations are outdated.

I found this very good short document that was made by unity: http://unity3d.com/profiles/unity3d/themes/unity/resources/downloads/beta/unity-5.0-user-guide.pdf

It highlights the changes that was made to unity 5 and at the end of page 5 you can find a section that explains how to set up a car. I have tried it about a month ago in a new unity project and it worked. the instructions are clear so try this tutorial out and I hope it will help.

Regards

like image 165
mnaa Avatar answered Dec 03 '22 11:12

mnaa