We've almost arrived at the end of our journey. We learned all the essential tools to implement fun AI in our Unity game. We stressed on this throughout the course of the book, but it's important to drive the point home: the concepts and patterns we learned throughout the book are individual concepts, but they can, and often should, be used in harmony to achieve the desired behavior from our AI. Before we say our goodbyes, we'll look at a simple tank-defense game that implements some of the concepts that we learned to achieve a cohesive "game", and I only say "game" because this is more of a blueprint for you to expand upon and play with. In this chapter, we will:
NavMeshAgent
tankOur "game" is quite simple. While the actual game logic, such as health, damage, and win conditions, are left completely up to you, our example focuses on setting you up to implement your own tank-defense game.
When deciding on what kind of logic and behavior you'll need from your agent, it's important to have the rules of the game fleshed out beyond a simple idea. Of course, as you implement different features, those rules can change, but having a set of concepts nailed down early on will help you pick the best tools for the job.
It's a bit of a twist on the traditional tower-defense genre. You don't build towers to stop an oncoming enemy; you rather use your abilities to help your tank get through a gauntlet of towers. As your tank traverses the maze, towers along the path will attempt to destroy your tank by shooting explosive projectiles at it. To help your tank get to the other side, you can use two abilities:
For our example, we'll implement the towers using a finite state machine since they have a limited number of states and don't require the extra complexity of a behavior tree. The towers will also need to be able to be aware of their surroundings, or more specifically, whether the tank is nearby so that they can shoot at it, so we'll use a sphere trigger to model the towers' field of vision and sensing. The tank needs to be able to navigate the environment on its own, so we use a NavMesh and NavMeshAgent
to achieve this.