Now they move how?!
In the last article, I implemented a camera shake when the player takes damage and wrapped up the first phase of the GameDevHQ 2D Game Development Certification track. Today, I’m going to be starting the second, and final, phase of that Certification track. In this article, I’ll be adding a new type of enemy movement! The requirement reads as follows:
- Enable the Enemies to move in a new way, either from side to side, circling, or coming into the play field at an angle.
Making Enemies that follow a path
To accomplish this requirement I decided to make some of my enemies follow a path of points. This would allow me to define new paths for new types of movement in the future. The first thing I felt like I needed was a path of points for the enemies to follow. I created a set of empty GameObjects and changed their display icon to make them visible while I was working on them inside the editor. Then I prefabbed Them as a path object.
With this first path setup, I created a new script that would control the enemies movement towards each point in the new path prefab. In the script I grab a list of the transforms of each point in the passed in prefab path and set the first point in the list as the starting point. Then in the update loop I check to see if the player has reached the current waypoint that it is moving towards or if it has reached the end of the path. If the enemy hasn’t yet reached the end of the path I do the calculations to move the enemy towards the current waypoint then, if it has reached the current endpoint, I update the current endpoint to the next one in the list. Otherwise, If the enemy has reached the end of the path I destroy the enemy game object.
A new enemy type
Now that I have the new enemy movement I want to apply it to enemies but I don’t want to apply it to all of my enemies. I still wanted to keep the top to bottom movement that my enemies currently have on some of my enemies. For this I created a copy of my enemy prefab called Pathed Enemy and attached my new EnemyPathing script to that new type of enemy. To disable the movement of the original enemy script for my new pathed enemies, I set the movement speed of the original script to 0.
Now, to spawn enemies with the new prefab, I changed the SpawnManager to take an array of enemy prefabs and a delay time between spawning a random selection from the array of enemy prefabs.
With that I have pathed enemies being randomly inserted into the spawn order and then following their path until destroyed by the player or reaching the end of their path!
In this article, I added a new type of enemy movement. Next time, I'll be changing the players ammo display to include the max amount of ammo. If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.