Enemy destroys power-up if in front

Day 50- Making 2D Space Shooter Game

Objective: Make enemy detect when the power-up is in front of them and shoots laser which destroys the power-up

We are going to add this behavior to the same enemy which could avoid laser and the concept is similar. It will detect gameobjects with tag power-up and fire laser towards it.

add this inside the OnTriggerEnter function so that after the enemy detects the power-up, it will fire laser at it and set the default canFire to -1 so that it will wait for default amount of time before it can shoot another laser.

Here set the powerupdetected variable to true only when the laser is moving down else the laser may destroy power-up even when it detects player behind it and shoots upwards. It is done inside of Laser script.

Finally destroy the power-up and itself when the power-up enters the collider of the laser.

--

--

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