2D Mobile Game: Enemy Waypoint System


In this article, we will cover adding the properties needed for waypoint system in the enemy class and how to implement this in an individual enemy script to create the enemy AI movement.

Enemy Class Setup

All of the enemies are going to be using the waypoints system for their AI movement. For this reason we are going to declare a few variables that individual enemy scripts will inherit. The first is an array of Transforms, which can be populated with the different positions the designer wants the enemy wants to move between. The second is an int variable that will keep track of which waypoint they enemy should be moving towards in the array.

Another change to the Enemy Class script will be to make it an abstract class. Making it an abstract class will allow us to force inherited classes to use certain methods, so they are all being built the same way. Think of the Enemy Class as the template that all other enemy scripts will be built from. They share common properties and can still override the template to create unique behaviors. Using Abstract Class will allow the development of cleaner, more modulate code.

We are going to be using an Update method with all of our Enemy scripts, so we create a public abstract method and we must make it empty. This will force the inherited scripts to call the method.

The Moss Giant (inherited script) gets an error now since the update method must be called.

Now that the update is being called, we error has been resolved.

Waypoints Setup

We create two empty game objects (Point A & Point B) and position them to where we want the enemy to and from.

The Moss Giant inherited an empty array of the Transforms from the Enemy Class script. We can set the the amount of waypoints we have (2) and drag in the two waypoints we created (Point A & Point B).

The Script

On Start, we are going to set the targetID to to current ID (0) plus 1 divided by the length of the array (2). The % will return the remainder of this equation, which will be 1.

In the Update method, we set the current position of the Moss Giant and have it move towards the waypoint 1. We use Vector3.MoveTowards method, which requires…

The current position is the current position, target position is the waypoint 1 position and the maxDistanceDelta is the the speed of the Moss Giant.

When the Moss Giants reaches the position of the targeted waypoint, we then want to cycle to the next waypoint using the same equation in the OnStart. The targetID is set to the current targetID (1) + 1 divided by 2. It again returns the remainder which is 0, because 2/2 = 1 with non remainder. This can be used with any number if of waypoints, which is ideal since we want this to be modular with a number of enemies we are going to create.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store