Missile enemies choosing powerups over the player.

addam davis
3 min readAug 6, 2021

Objective: Set the primary target of the missile enemies to Powerups, and if there aren’t any powerups then they target the player.

watching an enemy missile crawl towards your precious powerups before you could reach them will add more pressure to the player to try to shoot the missiles and take the powerups quickly.

I’m not a fan of copy and paste, however, this would be an excellent time to use it. We are going to use the same formula and methods we used to have the missile track and target the player and duplicate it to track and follow powerups. To begin we need to open the missile script and add a reference variable for the powerups.

We can use the exact same formula for having the missile move towards the missile. You calculate the distance and if it is not zero, we will set the Vector2 to move towards the target position. All as we’ve done before.

For the method to locate the powerups you need to change the Game Object in question. After this you are free to reuse the same method as before.

We are almost finished. We need to go back into the method that controls the movement towards the powerup, and we need to add and else statement. If the target is null, then we want to call the method to locate the player. This means when there is no powerup the missile will target the player.

We need to add the powerup tag to the collision so our missile will know what to do when it collides.

Open the powerups script and create a method to handle the destruction of the powerup.

That’s all there is to it. By adjusting already existing code we have given our missile enemies target priorities. The player will now have a harder time attaining their precious powerups. As I always say, don’t be afraid to experiment with your code, and I’ll see you in the next tutorial!