- Home /
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.
Answer by Jaap Kreijkamp · Mar 16, 2010 at 01:08 AM
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);
}
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?
i've also got a problem with changing the motorPower using keyboard input
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?
Answer by taxvi · May 22, 2012 at 12:31 AM
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 :).
Answer by UnityUnitedUK · May 01, 2014 at 03:19 PM
I know its a old post but could you make a demo project that works. thanks.
oh, unfortunately my laptop on which I was building that project died long ago :/
this is the only part of the project that's still alive, but no source code, sorry: http://www.kongregate.com/games/taxvi/didgori
Answer by malviyanpawan · Aug 13, 2014 at 03:17 PM
hi, I used ur code but there is little bit problem with it that when bike jump from any objects that point it look little bit buggy. So i replace with this code it work fine with my car demo project but look litter bit buggy with bike my code is :::
void WheelPosition() { Vector3 _wheelPos; RaycastHit _hit; if(Physics.Raycast(_rWheelCollider.transform.position, - _rWheelCollider.transform.up, out _hit, _rWheelCollider.radius + _rWheelCollider.suspensionDistance)) { _wheelPos = _hit.point + _rWheelCollider.transform.up _rWheelCollider.radius; } else { _wheelPos = _rWheelCollider.transform.position - _rWheelCollider.transform.up _rWheelCollider.suspensionDistance; } _wheelRTrans.position = _wheelPos; if(Physics.Raycast(_fWheelCollider.transform.position, - _fWheelCollider.transform.up, out _hit, _fWheelCollider.radius + _fWheelCollider.suspensionDistance)) { _wheelPos = _hit.point + _fWheelCollider.transform.up _fWheelCollider.radius; } else {
_wheelPos = _fWheelCollider.transform.position - _fWheelCollider.transform.up _fWheelCollider.suspensionDistance; } _wheelFTrans.position = _wheelPos; }
Your answer
![](https://koobas.hobune.stream/wayback/20220612111845im_/https://answers.unity.com/themes/thub/images/avi.jpg)