Game Development Reference
In-Depth Information
First of all we have two arrays of type
WheelCollider
, in order to reference the colliders
of the front and the back wheels of our vehicle. We need references to all colliders, since
they are the place where we control vehicle movement. Separating front and back wheels
is necessary as the steering is going to be applied to front wheels only. The next variable is
centerOfMass
, which is going to store a reference to
CenterOfMass
empty object we have
created earlier. The variables
maxMotorTorque
and
brakesTorque
represent the magnitude of
the force used to accelerate and decelerate the wheels. To control steering, we use
maxSteer-
ingAngle
and
steeringSpeed
. These two variables are going to be applied to front wheels only,
since we do not usually steer the back wheels. The last two public variables of the script are
maxSpeed
and
maxReverseSpeed
, which set the speed limits of our vehicle when driving
forward or backwards.
In addition to the public variables, we have
currentSteering
, which is used to store the
current steering angle of the front wheels. We have also
maxRPM
and
maxReverseRPM
,
and we are going to use these two variables to represent
maxSpeed
and
maxReverseSpeed
in terms of rotations per minute. Having the speed in such unit is necessary, since wheel
collider uses this unit to express the speed. Finally, we have a set of flags that store the cur-
rent control state of the vehicle. If there is a command from the controller (player or AI)
to accelerate forward, then
accelerateForward
variable is set to
true
, otherwise it is going
to be
false
. Similarly, the other four flags represent the states of their relative commands.
The next part of the script is shown in Listing 42, which contains
Start()
and
FixedUpdate()
functions.