Game Development Reference
Fig 4.10 Adding and positioning
Step 8. Attach the script waypoints.js from the Project onto Player2.
Select Player2 in the Hierarchy and locate the waypoint script in the
Inspector. Add each sphere to the script under the Waypoints variable,
as shown in Figure 4.11 . Ensure that the spheres are added in the
order you would like the Player2 car to move around the track, with
the first sphere in front of the car being assigned to Element 0.
Step 9. Play. The NPC car will start moving off following the waypoints.
In the Inspector you can change the speed, rotation speed, and
accuracy values of the waypoint script to modify the car's behavior.
When following a path and moving, sometimes objects don't land
in the exact location of a waypoint. We need to give a close enough is
good enough value. This is the value called accuracy . Rotation speed
will cause the car to make tighter turns the higher the value.
Step 10. At this point, Player2 may appear to sink into the road when
moving. This is because the collider does not include the wheels. The
player's car does not sink because of the Car script. As Player2 no
longer has this script, select the chassis collider element under Player2
in the Hierarchy and modify the size and position of the Box Collider
to include the wheels. Now Player2 will not sink.
Step 11. Now experiment with the speed of Player2 by racing against it.
Step 12. To create a finish line, add a cube to the scene. Resize it to
stretch across the road. Remove its Mesh Filter and Mesh Renderer
components. Leave the Box Collider and tick the Is Trigger box. The
remaining box collider for the cube should be big enough to trigger
an event when the NPC or player drives through it.