Game Development Reference
FIG 5.11 Setting the first person
controller as the target for an NPC.
top of the code. Before playing, locate the patrol script attached to the
robot model in the Hierarchy and set the target variable to the First Person
Controller as shown in Figure 5.11 .
When the NPC is pursuing and attacking the player, its position is made
relative to the player. Because the model sizes of the player and the robot
mesh are different sizes and the center points differ with respect to their y
position, without some kind of height offset for the robot, it will either sink
into the ground or float above it. This is the same issue the NPC has when
following the waypoints and the reason why spheres are lifted above the
ground. To fix this, the heightOffset variable has been added.
To get the best offset value usually the game needs to be played a number
of times and the NPC observed. If you know the exact height of the model's
mesh and its center position, it could be calculated.
Step 16. Play. The guard will chase and shoot at the player, but move up
the stairs into another room and see what happens. The guard will follow
the player but take shortcuts through the walls of the warehouse.
Step 17. This presents another problem to solve. We could make the NPC
stick to the waypoints and paths while trying to follow the player, but
what if the player goes somewhere there are no waypoints? You could