using wheel colliders?

I'm trying to use a wheel collider in a school project and was wondering A)how to script it to work with Input.GetKey() with the up arrow increasing the motorTorque.B)will it react with the terrain object moving the bike forward??

Any answers would be appreciated.

more ▼

asked Mar 15 '10 at 05:19 PM

JDubs212 gravatar image

17 1 1 1

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Wheel colliders aren't easy things to control, when making a bike or motor you'll have to do some tricks to make it stable (not falling) as the wheel collider is infinitely thin. Below I wrote a simple motor script (that doesn't solve the falling over problem, but set y of centerOfMass property to negative values does help).

Because the wheelcolliders shouldn't rotate with the wheels, you need to set up the motor as follows for the script, the motor has two wheel attach gameobjects (frontwheelattach and backwheelattach), you must add the wheelcolliders to these nodes (and attach these to the script properties front/backWheelCollider). The attach nodes must have the actual wheels attached, and these should be connected to the script front/backWheelTransform.

If you need a demo project to see how to setup the script I could make something quick for that as well, just let me know.

// Jaap Kreijkamp
// Ctrl-J (http://ctrl-j.com.au/)
// Demo motor script, do with it whatever you like...

var frontWheelCollider : WheelCollider;
var backWheelCollider : WheelCollider;

var frontWheelTransform : Transform;
var backWheelTransform : Transform;

var motorPower : float = 50;
var maxSteerAngle : float = 10;
var centerOfMass : Vector3;

private var backWheelRotation : float = 0;
private var frontWheelRotation : float = 0;

function Start() {
    rigidbody.centerOfMass = centerOfMass;  

function UpdateWheelHeight(wheelTransform : Transform, collider : WheelCollider) {
    var localPosition : Vector3 = wheelTransform.localPosition;
    var hit : WheelHit;

    // see if we have contact with ground   
    if (collider.GetGroundHit(hit)) {
        // calculate the distance between current wheel position and hit point, correct
        // wheel position
        localPosition.y -= Vector3.Dot(wheelTransform.position - hit.point, transform.up) - collider.radius;
    else {
        // no contact with ground, just extend wheel position with suspension distance
        localPosition = -Vector3.up * collider.suspensionDistance;
    // actually update the position
    wheelTransform.localPosition = localPosition;

function Update() {
    var deltaTime : float = Time.deltaTime;
    var accel = Input.GetAxis("Vertical");
    var steer = Input.GetAxis("Horizontal") * maxSteerAngle;

    // set power
    backWheelCollider.motorTorque = accel * motorPower;

    // set steering
    frontWheelCollider.steerAngle = steer;

    // calculate the rotation of the wheels
    frontWheelRotation = Mathf.Repeat(frontWheelRotation + deltaTime * frontWheelCollider.rpm * 360.0 / 60.0, 360.0);
    backWheelRotation = Mathf.Repeat(backWheelRotation + deltaTime * backWheelCollider.rpm * 360.0 / 60.0, 360.0);

    // set the rotation of the wheels
    frontWheelTransform.localRotation = Quaternion.Euler(frontWheelRotation, steer, 0.0);
    backWheelTransform.localRotation = Quaternion.Euler(backWheelRotation, 0, 0.0);

    // now some more difficult stuff: suspension, we have to manually move the wheels up and down
    // depending on the point of impact. As we have to do it twice (two wheels) we put it in a separate function
    UpdateWheelHeight(frontWheelTransform, frontWheelCollider);
    UpdateWheelHeight(backWheelTransform, backWheelCollider);
more ▼

answered Mar 16 '10 at 01:08 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.9k 21 30 74

thanks a lot, i put it in my project and assigned the wheel colliders for the front and back wheel but everytime i run it the wheels move to the center of the bike. Any suggestions?

Mar 22 '10 at 03:50 PM JDubs212

i've also got a problem with changing the motorPower using keyboard input

Mar 22 '10 at 11:41 PM JDubs212

I had the same problem with the wheels moving to the center of my vehicle. First I found that when my vehicle came to a rest the collider.GetGroundHit function would return false and I also noticed that (localPosition = -Vector3.up * collider.suspensionDistance;) would set the local postion to (0,-suspensionDistance,0) which would be the pivot point of the parent object in most cases the center of the vehicle. Any body know why the GroundHit would return false just because the collider is at rest?

Sep 16 '10 at 02:18 PM Kevin Ambruster
(comments are locked)
10|3000 characters needed characters left

I fixed the problem by myself (I'm so proud now :D) the reason of the wheels sticking in the middle of the vehicle is that in our UpdateeWheelHeight function there is this last line of code to be replaced: wheelTransform.localPosition = localPosition; with this: wheelTransform.localPosition.y = localPosition.y;

it's just that we have to control JUST and ONLY y axis of our wheels, not all 3 of them. so when the vehicle was in the air the code was repositioning the object on all of the axis - moving the whole wheel to (0,-suspensionDistance,0) in local coordinates.

PS: I found another minor mistake here: localPosition = -Vector3.up * collider.suspensionDistance; it would be a better idea to subtract a radius of wheel from suspension distance: localPosition = -Vector3.up * (collider.suspensionDistance - collider.radius);

Kevin: GetGroundHit is intended to produce false when the colider is in the air. that's why we are using the if statement :).

more ▼

answered May 22 '12 at 12:31 AM

taxvi gravatar image

15 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 15 '10 at 05:19 PM

Seen: 7806 times

Last Updated: May 22 '12 at 12:31 AM