This is part of an ongoing mission to explore making a simple controlled vehicle using the Unity Wheel Collider. See the introduction for a chapter list.

The World’s Simplest Car Control Script

In Part 1 I put some wheels on Marc’s truck and let it roll down a slope under the force of gravity. In this post I want to add the simplest of vehicle control scripts to the truck.

So without further ado here is the world’s simplest car control script for Unity:

var rearWheel1 : WheelCollider;
var rearWheel2 : WheelCollider;
var frontWheel1 : WheelCollider;
var frontWheel2 : WheelCollider;

var steer_max = 20;
var motor_max = 10;
var brake_max = 100;

private var steer = 0;
private var motor = 0;
private var brake = 0;

function FixedUpdate () {

steer = Mathf.Clamp(Input.GetAxis("Horizontal"), -1, 1);
motor = Mathf.Clamp(Input.GetAxis("Vertical"), 0, 1);
brake = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0);

rearWheel1.motorTorque = motor_max * motor;
rearWheel2.motorTorque = motor_max * motor;
rearWheel1.brakeTorque = brake_max * brake;
rearWheel2.brakeTorque = brake_max * brake;

frontWheel1.steerAngle = steer_max * steer;
frontWheel2.steerAngle = steer_max * steer;

}

So lets go through it bit by bit.

The first four lines create public variables for the car’s wheel colliders. When a script is added to a game object public variables appear in the script inspector. The plan is to add the script to the truck body game object then hook each of the wheel colliders to these variables using the script inspector.

The next three lines define public variables for use in our script. These are the maximum steer angle, motor torque and brake torque. By making them public I can change them easily in script inspector.

The next three lines are some private variables for use in the script. Specifically they will be used to temporarily store user inputs.

The FixedUpdate function is the heart of the script. The FixedUpdate function is run once every frame that the physics of the scene is updated.

In the function I grab the user input for steering, motor and brakes. These are the ASWD keys by default. Input.GetAxis(“Horizontal”) gets the A and D key presses for left and right. I will use this to steer the truck. Input.GetAxis(“Vertical”) gets W and S key presses. In this case I want W to apply the accelerator and S to apply the brake. So I have clamped, or restricted, the vertical input returns so I know when they are positive (the W key, going forward) and negative (the S key, going backward or braking).

With the user input giving me steering, motor and brake I then apply those to the wheel colliders. The motor and brake torques are applied to both the rear wheels and the steering angle to the front wheel colliders. MotorTorque, brakeTorque and steerAngle are all properties of the Unity wheel collider.

Using the script

The script is applied the the main truck object. Once applied the public settings for it turn up in the inspector. There are four slots for wheel colliders. Drag the wheel collider ojects from the hierarchy to the appropriate slots in the script inspector.

The script in the inspector

The script in the inspector

There were a few other tweaks to be made to the scene before testing. First I leveled the ground plane out again. Then I added a box collider to the truck object. Without the box collider the truck mesh will pass though the ground plane with the truck rolls over… and trust me, it will roll over.

Stomp on the gas and you will find the truck goes into a permanent wheelie. Try and turn a corner at speed and the whole thing is liable to tip over. Obviously there are issues about how much the truck weighs and where its centre of gravity is. Having not changed the default weight of the truck from one kilogram it must have a density much less than styrofoam – no wonder it’s flying about. (EDIT: I have subsequently learned that it is the placement of the truck’s center of gravity, see the next chapter for how to fix it.)

Next Steps

You may notice as you drive around that some things are still off with our truck. The tyres do not turn as the truck moves and the front tyres do not turn with the steering. There is also suspension associated with the wheel colliders which is not being reflected in the tyres. We will fix these cosmetic issues in the next part.

Are you following along and have questions or wisdom to share? Leave a comment below.