Making the Enemy Fire

Set Up the Prefabs

I added an Enemy to the Scene

I Added an enemy Laser prefab to the Scene

Mad Sure that the Moveable Component of the Enemy Laser Prefab was set to move in the correct direction.

I added a new Game Object to the screen for the Enemy Laser and added the Destroy No Children Game Component.

I moved the Enemy Laser Prefab onto the new Enemy Laser Game Object. I positioned it so it fired from the correct location. I duplicated it and moved the duplicated to the other correct position.

I mad the New Enemy Laser Game Object a Prefab.

I edited The Enemy Prefab to use the laser prefab with an offset of 0 on all axis and to spawn in the correct container

I added a sound effect to the enemy prefab so that it plays a sound when firing the laser.

I edited the Enemy Asteroid Prefab and removed the Laser Prefab from it, this ensures that the asteroid does not fire a laser.

Update Enemy Should Fire

I made the Should Fire Laser of the Enemy Behavior return the opposite value of the Fire Delay Timer Is Active. If the Fire Delay Timer is active then the Enemy has fired a laser and should not fire again until the Fire Delay Timer is not Active.

The Enemy Now Fires a laser.

Fix the Enemy Laser Prefab

The laser will never damage the Player, it is set to destroy enemies.

Edit the Enemy Laser Prefab, Change the Tag to Enemy Laser and on the Damageable set it to be damaged by the Player.

Add Some Variation to the Enemy Firing its laser

Since the Enemy is using the Cool Down Timer to determin when it should fire the laser I added a Min and Max time for the timer to pick from. I set the cool down time to be a Random vale between the min and max

Made sure to make the Player and Enemy Prefabs to use the new min and max Times. I set the Player to have a Min time of 5 and a Max Time of 5 this keeps the Player at the 5 seconds that I had set up earlier. I set the Enemy to have a min time of 3 and Max time of 7.

Now the Enemy will fire its laser randomly between 3 and 7 seconds.

Make the Enemy Not Fire a laser when it First appears

The Enemy will fire its laser as soon as it is in the game, This may be a behavior that you want. I want The enemy to be visible to the player before it fires its laser. In the Enemy Behavior I overrode the Ships Awake method and started the Fire Delay Timer. This sets the Fire Delay Timer to Active.

The Enemy can now shoot the Player.



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
James Lafritz

James Lafritz

Excited about changing my hobby into a new carer with GameDevHQ course.