An Enemy That Destroys Power-Ups

Today’s core programming challenge will closely follow the logic in the previous topic on how to create a , 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 . Of course, it will have its own dedicated 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 .

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 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 (basic) and (modular). You can also learn more about collision detection

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 (using the animator window), and (Sprite animation).

The Result:

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



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dennisse Pagán Dávila

An ambitious Writer and Unity Developer on a journey to join the video game industry. I publish articles documenting my learning on the go.