I am not a huge fan of driving games, though I have played my fair share of Gran Tourismo. However the first model Marc sent me was of a little blue truck. When I got it I remembered seeing the Wheel Collider mentioned in the Unity documentation. So my thought was, “hey I can just slap some wheel colliders on Marc’s model and we will have Grand Theft Auto in no time”.
Yeah, well, not so much.
After several attempts at just slapping something together I finally decided to go find a tutorial on the subject. Unfortunately I could not find one. Nor could I find much in the way of sample code. With the few scraps I could find I have started to piece things together.
Colliders are part of Unity’s physics engine. As the name suggests they allow game objects to collide and to use those collisions as triggers. The Wheel Collider is a special class of collider that mimics the physical properties of a wheel and suspension. In this case the specific properties it mimics are:
- A wheel keeps its axle a set distance (the radius of the wheel) from the ground.
- A (free spinning) wheel has a low resistance to movement in one direction and a high resistance in all others.
These, amongst a few others, are the properties that the Wheel Collider mimics. It is also important to note what a Wheel Collider does not do.
- A wheel collider does not spin.
- A Wheel Collider does not transmit force to the ground (as far as I know). (Edit – actually it does through the motorTorque and brakeTorgue parameters)
The Unity documentation recommends that you keep the wheel mesh and the Wheel Collider for a wheel separate. This makes sense when you realise that the with them separate you can make the mesh rotate so it appears to be driving the vehicle forward. You do not want to rotate the Wheel Collider as Bad Things™ will happen. Obviously you would want to connect the two somehow through code so that the appearance of the mesh follows the physics of the collider.
So the mission I am setting myself is to get Marc’s little blue truck driving around under user control with a camera following it. I will try and document as much of what I learn as I can as I go along.
- Part 1: Adding wheels to the truck
- Part 2: Adding a motor and steering
- Part 3: Turning the wheels and adding a reverse gear
- Conclusion: Drive the truck around in Unity Player for Web
- Redux: Drive an improved truck in Unity Player (with new script in full)
I would love to hear your comments on this series of posts either here or in the comments of the other posts.