New enemy movement

Russell Smith
4 min readOct 6, 2022

--

Objective: Make the enemies spawn and move diagonally.

Previously, I had the enemies spawn at the top of the screen and simply come down the page. I wanted to have the enemy come in at a different angle, I also wanted them to wrap like the Player, so if it goes off the side of the screen so it would appear on the other side.

Firstly, I took my enemy prefab and dragged it in to my Hierarchy View, I then duplicated to make more copies, renaming one enemy left and the other enemy right. From here I changed the rotation of each , by simply changing the Z axis of each to 35 or -35, so it would be in position for the path I wanted it to follow when it spawned.

As you can see below I have the standard enemy in the center next to the asteroid and the two new ones on either side.

I take the two new created enemies and drag them down to my Project View to prefab them.

Next, in my Spawn Manager Script I needed to alter the Spawn Routine for the enemy. I wanted to make an array as I now technically had 3 enemy types, as the alternated enemies were like new enemies. I changed my enemy variable as you can see.

Now in Unity I could assign each of the enemy types to its corresponding element in the array. Also making sure in each enemy is assigned their ID number.

Once that’s set, I want to change my Spawn Enemy Routine back in my Spawn Manager script. I need to add an int variable to spawn a random enemy from the array. Then I want it to instantiate the corresponding enemy, and when it does it’s important it keeps its same rotation, so those enemies come in at that angle.

With the Spawn Manager updated, time to jump into the Enemy script to fix their movement and firing ability.

In my Enemy Script I wanted to make a Switch for the different enemy ID’s. For this I needed an ID system.

My Calculate Movement method I changed to Enemy Movement. In here I made my Switch, for case 0 I kept what I already had, as this was for the standard enemy.

For case 1 & case 2, I copied the movement but also added the wrap feature I had on the Player, so if these enemies went off the side of the screen they would appear on the opposite side. Now I didn’t need all of it as the Left enemy would never go off the left side as it’s travelling towards the right, and vice versa. So, I only used the code that was applicable in the script.

The problem I was now facing that the new enemies weren’t keeping their rotation when they spawned, so what I used to make this work was this line of code.

Putting this in case 1 and case 2 preventing this issue. So my completed Switch statement looked like this:

The last issue I had to over come was the lasers. When the enemy spawned, and a laser instantiated it would automatically destroy my enemy. To overcome this I simply needed to change the rotation in the line of code for the laser instantiation in the Enemy Script.

I simply changed Quaterion.identiy to this.transform.rotation and below you can see how the new line of code looks.

Saving my changes and updates, I then ran my game and you’ll see we now have working enemies coming in at different angles.

--

--