Game Development Reference
In-Depth Information
Let's consider the Patrol state. In the Patrol state, the Enemy should appear to be moving around
and to be “up to something.” In practice, this means the Enemy class should internally generate a
random location inside the level , and then move there. And when the destination is reached, the
Enemy should generate a new location elsewhere, and then move there, and so on for as long
as the Patrol state is active. Now, to achieve this, we'll need to make use of pathfinding and
navigation . Before considering this, let's add a NavMeshAgent component to the Enemy character.
Doing this signifies that our Enemy will be the kind of thing that can move intelligently around the
level. To add a NavMeshAgent component, select the Enemy_Drone in the scene, and choose
Component Navigation Nav Mesh Agent from the application menu (see Figure 7-12 ).
The Component settings can be left at their defaults for now .
Figure 7-12. Adding a NavMeshAgent component to the enemy Drone
The NavMeshAgent component will be necessary because, for the Enemy to move around the level,
pathfinding and navigation will be used. Why is this? Consider Figure 7-13 . Suppose the Enemy
(standing in position A), in a Patrol state, decides to move to a new destination (position B). The
blue arrows in Figure 7-13 indicate the route he should take to travel from A to B. We, as humans,
can clearly see and plan the route as shown, because we know certain things about the world: we
know we can't walk through walls, for example. To accommodate this, we therefore create a route
that conforms to the environment, ensuring we only travel along possible routes. But the problem is
that the enemy Drone doesn't know any of this! He would quite happily take the direct route, passing
ethereally through all walls and obstacles to reach the destination. And Unity will do absolutely
nothing to stop him. Thus, we need the Enemy to act smart and consider obstacle avoidance when
traveling. To solve this, we'll use the Unity NavMesh system.
 
Search WWH ::




Custom Search