Journal 35 — Add New Enemy Movement Pattern with Waypoints in Unity

Chris Nielsen
3 min readMay 31, 2021

The basic movement pattern I started with in this practice game is Vector3.down, and this keeps repeating endlessly. I updated the movement pattern to allow for use of a set of waypoints that the enemies can follow. Here’s the basic concept:

  1. Create waypoints

Waypoints are simply a series of empty gameobjects placed in the editor, that are typically referenced by other gameobjects to simulate a movement pattern on screen. As a test case, I made 4 points in a bowtie pattern, which would simulate the enemies moving across the screen diagonally. We can use the Unity reference documents OnDrawGizmos to visualize these points. I put all the waypoints in an empty gameobject container and created a C# script.

Unity Editor, add empty gameobjects for waypoints
Unity C# script for waypoints, add to Container
Unity Editor, visualize waypoints

2. Use GameManager Singleton for waypoints

After testing multiple approaches, I found a way to use a Singleton method to create a GameManager instance that can be used to store the waypoints in a List, so the Enemy prefabs can access these points when instantiated. In the GameManager script I added:

Unity C# script for GameManager, create GameManager Instance

In the Start method, we find the Transform of each child of the container and add to a waypoints List.

Unity C# script for GameManager, add waypoints

3. Assign waypoints to enemy instantiated prefabs

Now that we have successfully referenced the waypoints, we need to add these waypoints to each enemy we want to change when they are instantiated. To start I made a quick change to one of the enemy colors to visualize their differences, added a different tag in Unity, and do a comparison of their tags to add the waypoints to a list in the Enemy script.

Unity C# script for Enemy, waypoints variables
Unity C# script for Enemy, assign waypoints

4. Apply the different move pattern to enemies

Now that certain enemies have a set of waypoints to follow, we can switch this movement in the enemy Update method.

Unity C# script for Enemy Update method, change movement
Unity C# script for Enemy, movement pattern follows waypoints sequentially

Finally, here are the results.

Unity Playmode, see movement patterns in Editor

Thank you for stopping by!

--

--

Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.