Game Development Reference
In-Depth Information
UpdateSteering()
function takes a float value in degrees, and adds it to
currentSteering
. If the
passed value is zero, the function returns the steering to the straight position by using
steeringSpeed
. The steering dead zone is set between -3 and 3 degrees, so if the steering
value is within these limits, it is going to be set instantly to zero (straight). After setting the
new value of
currentSteering
, the function checks the limits by comparing
currentSteering
with
maxSteeringAngle
and
-maxSteeringAngle
. Finally, the value of
cuurentSteering
is stored
in
steerAngle
member of all wheel colliders in
frontWheels
.
The last function we are going to discuss in this script is
KmphToRPM()
. This function takes
two parameters: a wheel collider and a speed value expressed in km/h. The first step is to
convert
speed
from km/h to meter/minute and store the result in
mpm
variable. The second
step is to convert the speed from meter/minute to RPM. However, this conversion depends
on the circumference of the wheel. The circumference tells us how many meters the wheel
moves during one complete rotation. Therefore, we divide
mpm
by the circumference to get
the speed in RPM and return it. After adding this script to the root object of the vehicle, we
are ready to move to our next script.
Now we need another script that enables the player to provide his input to control the
vehicle. This script is going to read input from the keyboard and invoke the corresponding
functions from
PhysicsCarDriver
. Listing 44 shows
KeyboardCarController
script, which
handles player input.