Chapter 8. Positions, Movement and Navigation for Character GameObjects

In this chapter, we will cover:

  • Player control of a 2D GameObject (and limiting the movement within a rectangle)
  • Player control of a 3D GameObject (and limiting the movement within a rectangle)
  • Choosing destinations – find the nearest (or a random) spawn point
  • Choosing destinations – respawn to the most recently passed checkpoint
  • NPC NavMeshAgent to seek or flee destination while avoiding obstacles
  • NPC NavMeshAgent to follow waypoints in sequence
  • Controlling the object group movement through flocking

Introduction

Many GameObjects in games move! Movement can be controlled by the player, by the (simulated) laws of physics in the environment, or by the Non-Player Character (NPC) logic; for example, objects that follow a path of waypoints, or seek (move towards) or flee (away) from the current position of a character. Unity provides several controllers, for first and third-person characters, and for vehicles such as cars and airplanes. GameObject movement can also be controlled through the state machines of the Unity Mecanim animation system.

However, there maybe times when you wish to tweak the Player character controllers from Unity, or write your own. You might wish to write directional logic—simple or sophisticated Artificial Intelligence (AI) to control the game's NPC and enemy characters. Such AI might involve your computer program making objects orient and move towards or away from characters or other game objects.

This chapter presents a range of such directional recipes, from which many games can benefit in terms of a richer and more exciting user experience.

Unity provides sophisticated classes and components including the Vector3 class and rigid body physics for modeling realistic movements, forces, and collisions in games. We make use of these game engine features to implement some sophisticated NPC and enemy character movements in the recipes of this chapter.

The big picture

For 3D games (and to some extent, 2D games as well), a fundamental class of object is the Vector3 class—objects that store and manipulate (x,y,z) values representing locations in 3D space. If we draw an imaginary arrow from the origin (0,0,0) to a point on space, then the direction and length of this arrow (vector) can represent a velocity or force (that is, a certain amount of magnitude in a certain direction).

If we ignore all the character controller components, colliders, and the physics system in Unity, we can write code that teleports objects directly to a particular (x, y, z) location in our scene. And sometimes this is just what we want to do; for example, we may wish to spawn an object at a location. However, in most cases, if we want objects to move in more physically realistic ways, then we either apply a force to the object, or change its velocity component. Or if it has a Character Controller component, then we can send it a Move() message. With the introduction of Unity NavMeshAgents (and associated Navigation Meshes), we can now set a destination for an object with a NavMeshAgent, and then the built-in pathfinding logic will do the work of moving our NPC object on a path towards the given (x, y, z) destination location.

As well as deciding which technique will be used to move an object, our game must also do the work of deciding how to choose the destination locations, or the direction and magnitude of changes to movement. This can involve logic to tell an NPC or enemy object the destination of the Player's character (to be moved towards, and then perhaps attacked when close enough). Or perhaps shy NPC objects will be given the direction to the Player's character, so that they can flee in the opposite direction, until they are a safe distance away.

Other core concepts in the NPC object movement and creation (instantiation) include:

  • Spawn points
    • Specific locations in the scene where objects are to be created, or moved to
  • Waypoints
    • The sequence of locations to define a path for NPCs or perhaps, the Player's character to follow
  • Checkpoints
    • Locations (or colliders) that, once passed through, change what happens in the game (for example, extra time, or if a Player's character gets killed, they respawn to the last crossed checkpoint, and so on)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.54.255