Day 45|Destroying Pickups: 2D Space Shooter Series

Ted Lim
2 min readJun 15, 2021

Continuing our theme of adding more behaviors to our enemies, we’ll give our enemies the ability to shoot and destroy power-up collectibles if they are within a given range.

Destroying Powerups

To make our enemies capable of destroying powerups, we need the enemy to detect the powerup and then sequentially shoot lasers at it.

Detecting Powerups

To detect the powerup, we will make a child Trigger Collider for our enemy prefab. For this child, we’ll attach a Rigidbody2D component and make it Kinematic. Otherwise, the parent enemy object will consider the child collider as part of its own collider.

Shooting Lasers at the Powerups

With the collider created, we want the enemy to shoot any powerup that enters the collider space. Therefore, we’ll create a PowerupDetectorScript and attach it to the child Trigger Collider object. The script will control when the enemy lasers are shot based on the OnTriggerEnter2D callback function.

Lastly, we’ll create a tag for the collider called PowerupDetector, and add the tag to the powerup script to destroy the powerup when it comes into contact with the enemy lasers.

And we’re done! Now, the enemy ships can prevent the player from collecting the powerups!

--

--