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:
- 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!