Game Development Reference
In-Depth Information
We have another custom function in the lines 94 through 105, which is AdjustSteering() .
We are going to use this function to implement turning right and left. When we call this
function, we must provide it with a turn speed and a turn axis. AdjustSteering() rotates the
car around rotationAxis using the value of speed variable. After that it checks the current
speed of the car. If the current speed is greater than 30 km/h, it is reduced by turnDecel-
eration . Notice that AdjustSteering() calls AdjustSpeed() in order to apply deceleration. This is
perfectly legal in programming languages; to have a function call another function.
Back to Update() function, and specifically to lines 42 through 52, where we check the
state of the up arrow, and increment the current car speed by the value of acceleration if
the player is holding this arrow. Notice that we change the speed by calling AdjustSpeed()
function and providing it with acceleration multiplied by CONVERSION_FACTOR to
convert km/h to m/s. Calling AdjustSpeed() without performing this conversion of units
will result in a wrong value. If the player is not pressing up arrow key, we decrease the
speed with the negative value of deceleration. We use the negative value here in order to
have AdjustSpeed() subtract deceleration value from the current speed. In lines 54 through
59 we check the state of the down arrow, and activate the brakes if the player is pressing
it. Activating the breaks means decreasing the speed with the value of braking . This means
that the car will need less time to stop completely when the brakes are active.
Turning is performed in lines 62 through 71, were we check the state of the right arrow as
well as the current car speed. We do not turn the car if its current speed is less than 5 km/
h, since steering must not be able to move the car if it is completely stopped. Notice in line
64 that we use rightAxis as the rotation axis, along with the positive value of steeringSpeed .
This is because turning right needs a positive (clockwise) rotation around the vertical axis
(line 97). On the other hand, in line 70, we use the negative value of steeringSpeed , to ach-
ieve a counter-clockwise rotation around leftAxis .
After computing the speed of the car and performing the necessary rotation for turning, we
need to move the car forward along its positive z axis. Weuse currentSpeed multiplied by
the time. Remember that currentSpeed is expressed in m/s, so it is safe to multiply it directly
by Time.deltaTime as in line 74. You can now test your car control script since it is ready,
then you can move to the next step, which is writing the camera script.
Search WWH ::




Custom Search