Having an Enemy fire at a Player pick up.

Jason Schneider
2 min readJan 24, 2022

In order for our Enemy to fire at a Powerup, it needs to know that the Powerup exists. To accomplish this we can use similar code to that of finding the closest Enemy; with a little tweak; that makes sure it is also in front of our Enemy in our Enemy script..

In order to use this code we need to add the following to the Update method of the Enemy script:

We then; of course; need to add a variable to our Enemy script:

Now when a Powerup is in front of our Enemy _powerup will not be null, so we can use the following in the Movement method of our Enemy script:

We need to add a bool variable _powerUpDestroyActive to our Enemy script.

Saving the script, jumping back into Unity, pressing play and then pause. Then setting the spawned Enemies, Power Up Destroy Active to true and dragging a Powerup prefab in front of our Enemy and pressing pause again, will give:

Fantastic, now we just need to add code that will allow the Enemy laser to destroy the Powerup, this, I will leave to you.

--

--