An Enemy That Destroys Power-Ups

Dennisse Pagán Dávila
3 min readApr 4, 2022

--

Today’s core programming challenge will closely follow the logic in the previous topic on how to create a Smart Enemy, as well as adding to/using code previously established in prior articles(all of which will be cited).

Objective: Create an enemy that can destroy a power-up after detecting it within a given range.

Detecting the Power-Up and Firing at it

The power-up detection function will be following the same structure as the Player detection. Of course, it will have its own dedicated LayerMask and tag comparison labeled accordingly. This block of code is part of my Enemy Script since the instantiated needs to spawn from the Enemy position to which the script is attached. To fire the projectile, you can simply instantiate the object or use a method for the instantiation and call it here.

Programming the Projectile Behavior

The projectile that will instantiate once the Power-up is detected needs to move towards its target. In order to achieve this, we can implement a simple “up” movement to it. This code will go on a script attached to the object.

  1. Add a speed variable. If you wish to tweak this from the Inspector for testing, you can make it into a SerializeField variable

Note: The direction of the movement will be subjective to your own code’s utility, meaning that the direction and movement methods can vary. In my case, a simple movement is used because the Enemy detects only Power-ups that are directly in front of it in its 2D enviroment.

2. Create a void method for the move behavior. This will be called from the update so that it can execute per frame. You learn more about movement logic here.

Destroying the Power-Up

To destroy the object, we will be using the script attached to the power-ups to detect the collision from the projectile.

  1. Use an OnTrigger2D function to detect collisions and compare their tags to the specific ones that is being searched for.
  2. When the right tag is found(in my case the puDestroyerLaser), the object holding the script will destroy itself, and the “other” object, which refers to the projectile, will be destroyed as well. The .20f delay has been added to allow an explosion animation to play s the power-up is destroyed.

Note: The following code is part of my power-up script, which has all the power-up activations within it. However, for the purpose of this article, they have been left out to focus on the current objective. You can view the Power-Up script here(basic) and here(modular). You can also learn more about collision detection here.

3. This is more of a bonus, but you can instantiate an animation or particle effects to accompany the object’s destruction. You can learn more about adding animations here(using the animator window), and here(Sprite animation).

The Result:

In the next article, I will be covering how to create an enemy wave system.

--

--

Dennisse Pagán Dávila
Dennisse Pagán Dávila

Written by Dennisse Pagán Dávila

Software Engineer that specialize in Game Development. Currently looking for new opportunities. Portfolio: dennissepagan.com

No responses yet