Player Advantage? Enemies Destroy The Power-Ups! (Part 2)

Shooting at Powerups in front of them is good, but what if they could destroy any powerup, anywhere? Following on from Part 1, we’ll make the enemies able to shoot any Powerup at a lower Y position than them.

Today’s Objective: Make the enemies shoot at and destroy any power-ups that are in front of them, stopping the player from collecting them. The Powerups do not need to be directly in front of them with this version.

Add the Line Renderer component
Set its Width and Color to be like my laser, and set Use World Space to true
  • A public static List of Transforms for all currently existing Items.
  • A public static Boolean variable to return if any Items currently exist.
  • A public static Int variable to return the Item count.
  • And a public static function to update the List.

The function is called when an Item is spawned, adding it to the list, and when an Item is destroyed, removing it.

  1. Get a list of all Powerups in the scene.
  2. Remove any Powerups behind the Enemy (plus a buffer distance) from the list.
  3. Find the closest Powerup to the Enemy in that list, and set as the target.
  4. Optional: I have 2 ship guns, so I will use distance to choose which gun to shoot from.
  5. Activate the Line Renderer and target the targeted Powerup for the duration of the shot.
  6. Destroy the Powerup and deactivate the Line Renderer.
Create a new IEnumerator coroutine

Inside the coroutine, add the following:

Delay before starting, get the target, and a null-check
Determine the gun to use, and enable the Line Renderer
Start a while loop until [duration] passes, updating the Line Renderer point positions every frame
Disable the Line Renderer, and if the target still exists, destroy it

I want it to happen every time the enemy exits the bottom of the screen and repositions to the top. So I will go to my code that checks if it has reached the bottom of the screen:

A dynamic Anti-Powerup Laser!

I think it turned out great, and maybe with a low chance of activating, would be a good additional difficulty for players. I think I’ll set it to a 5% or 10% chance an enemy will target a powerup every time they reposition to the top of the screen.

Unity game developer / C# Programmer / Gamer