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

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.

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:

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

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.

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.

Finally, here are the results.

Thank you for stopping by!



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
Chris Nielsen

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