Creating a diagonal moving enemy.

Today we’re getting closer to wrapping up the Space Shooter game. In this article, we create a new enemy type that will move diagonally and shoot a triple shot laser. You will need to create a new enemy prefab as well as a new prefab of the triple shot. You will need to reposition the middle laser in the prefab so it doesn’t instantiate inside the player. We will also create a small offset to help prevent that from happening. I won’t walk through the creating of the game objects as this is something we have been doing for a while now.

In the ‘Enemy’ script we will need a new bool and a new GameObject.

When getting to the meat and potatoes of this enemy, we will start with the movement. I chose to create a coroutine since I wanted the enemy to spawn and move downwards first, then take off moving to the right. The idea behind this was to trick the player into thinking the enemy would be moving in a straight line, then it starts cruising diagonally.

Next, we need to create a new function that will start this coroutine, but also instantiate the triple shot laser. I pretty much used the same logic from the original enemy laser, and replaced the prefab, and added a buffer for the laser to not collide with the player collision box.

I now just call the function we created in ‘Update’ then made sure to check the box for ‘Triple Shot Enemy’ so that the enemy will act with this behavior.

I did have to place the original enemy laser logic into its own function to get this to work and call it in each enemy type. The reason was due to this new enemy was shooting a normal laser, as well as the triple laser.