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.

First I add a Line Renderer component to my Enemy prefab:

Add the Line Renderer component
Set its Width and Color to be like my laser, and set Use World Space to true

Then, I make some new variables in my Spawn Manager script:

  • 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.

The Laser-Targetting Process in the Enemy Gun script:

  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.

Get and sort the list of Powerups:

Get the closest Powerup:

Determine which gun, maintain the Line Renderer, and Destroy the Powerup:

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

Also make a public function to start the coroutine:

Lastly, we need to determine when these functions will be called:

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:

And the result:

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. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

14-Player Damaging and Lives — Script Communication using “GetComponent”

Using UUID in Laravel

Using LiveData & Flow in MVVM — Part II

SQS Polling using Spring Web Flux

Robust Java Frameworks for Your Web App Development Projects!

Successful Cloud Migrations Made Easy

Getting the weather — a full stack ReactJS/Python Flask API tutorial — Part 1

The Kudex Blast-Off Update

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
Vincent Taylor

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.

More from Medium

“Game Feel”: The Impression Of Limited Time

How to Switch Virtual Cameras Using Only Cinemachine and Scripting in Unity

My journey becoming a Unity game developer: 2.5D

Adding a Main Menu to our UI