Unity Mobile Game Development: Enemy AI and Waypoints
In this series of Unity tutorials, we'll demonstrate how to setup our waypoints in order to allow our enemies to seek and destroy the player. Software required: Unity 3.4.
What you'll learn
In this series of Unity tutorials, we'll demonstrate how to setup our waypoints in order to allow our enemies to seek and destroy the player. We'll also discuss how to create a basic AI for our enemies. Having a computer understand our level is one of the most pertinent problems facing any game developer. Waypoints are a quick and powerful way to create a simple model of a level that an AI can understand. Once we have our waypoints set-up and working, we'll create an enemy AI that can find and attack the player. Software required: Unity 3.4.
Table of contents
- Mapping Out the Problem of Entities Moving Around Our Level 5m
- Waypoint Ideas, Concepts and Our Implementation 7m
- Creating an Initial Waypoint Prefab and Setting It Up 8m
- Filtering Nearby Objects to Other Waypoints with a Layer Mask 8m
- Coding a Variable Distance with a While Loop 10m
- Building in Line-of-sight with Layer Masks 6m
- Relaying Information to Neighbors Without Infinite Loops 12m
- Initializing a Generic List of Waypoints to Get to the Player 13m
- Creating an Initialize Function and Removing the Countdown 10m
- Using Conditionals to Check if We Want to Update the List 11m
- Creating a Controller Script That Gathers All the Waypoints 8m
- Locating the Closest Waypoint to Any Point in Space 13m
- Coding a Function to Return a Random Waypoint 11m
- Using a While True Yield Loop to Update Every Few Seconds 9m
- Creating a Spawner Script to Create New Enemies 9m
- Using a Spawn Controller to Randomly Spawn Enemies 5m
- Controlling the Number of Enemies Spawned 9m
- Creating a Basic Enemy Script to Use Our Waypoints 9m
- Moving with the Smooth Damp Method 11m
- Deciding When to Go to the Next Waypoint 8m
- Using Rotate Towards and Look Rotation to Rotate the Enemy 13m
- Refactoring Our Code into a New Function 13m
- Updating the Player Location List so the Enemies Don't Stop 9m
- Refactoring into an Infinite Loop Function to Update Waypoints 8m
- Creating a Basic Finite State Machine to Run Our Enemy AI 9m
- Using a Switch Statement to Run Code Based on the Enemy State 7m
- Creating a Function to Check for Transitions 7m
- Adding a Distance Check Before the Enemy Starts Following 8m
- Transforming the Enemy Forward in the Start State 11m
- Importing Our Final Geometry and Splitting Animations 8m
- Updating Our Enemy Prefab with the New Geometry 11m
- Changing Our Enemy Animation in Code with Animation Play 9m
- Coding an Attack Decision into Our Transition Function 10m
- Telling Our Spawn Controller When an Enemy Has Died 10m
- Changing Our Movement Speed Based on the Current State 8m
- Refactoring Our Code to Be More Readable and User Friendly 6m
- Playtesting Our Enemy AI and Hunting Down Bugs 7m
- Using Random Values to Create More Realistic Movement 8m
- Adding a Collider to Our Prefab for Hit Testing 6m
- Testing if We Have Been Attacked or Hit 9m
- Coding for Other Notifications to Work in Our Game 8m
- Stopping the Animations from Playing While Paused 9m
- Stopping the Logic from Running When the Game Is Over 7m