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.
asked Mar 15, 2010 at 05:19 PM
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.
answered Mar 16, 2010 at 01:08 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 :).