Creating a smart enemy with a laser beam in Unity

Austin Young
5 min readSep 1, 2023

--

Goal: Create a new enemy that knows when it’s underneath the player and fires a laser beam upwards while moving towards the player.

Creating the enemy and laser beam

To start I dragged in the sprite for my new enemy to my scene, renamed, set layer and added all the necessary components: box collider2D, rigidbody 2D, animator, audio source, and a new script I created for it.

Next I created an empty object and manipulated multiple assets to design a laser beam which is all parented by the empty object named laserbeam. I then made the laser beam a child of the new enemy. I also created a new script for the laser beam and attached it while also attaching a box collider2D and rigidbody 2D. Set as inactive.

I also made a shield as a child of the new enemy as all my enemies will have a 50% chance to start with a shield when spawned.

Now that my enemy is ready to be programmed, I dragged it into my Prefabs folder and opened visual studio. The scripts I need to code for are the new enemy script, new laser beam script, and editing existing spawn manager script to add the new enemy to the spawn routine on level 3.

Programming the enemy

Inside my new enemy script, I copied the collision logic, explosion audio and animation, and shield logic from my other enemies. I needed to create new movement and attack logic.

I created the following new variables:

  1. A SerializedField GameObject to assign the enemy’s laser beam object.
  2. A GameObject to find and assign the player object in Start().
  3. A Vector3 to track the player’s position which will update in Update()
  4. A Bool to define if the laser beam is firing or not.
  5. A Bool to define if the enemy has used the laser beam once per rotation.
  6. A SerializedField AudioClip to assign and play a sound effect for the laser beam.

After assigning variables in the inspector and in Start() and Update(), I created new logic for the enemy movement, utilizing the player’s position and the bool variables.

I want the enemy to move down the screen until it’s 2 meters underneath the player at which it will activate it’s laser beam and move towards the player. After one second or collision it will stop, move down again and then repeat once recycled at the top.

My enemy is rotated 180 degrees so the directions may seem wrong but I had to reverse them because of rotation. The enemy will move down, if 2 meters below player position and hasn’t fired laser, it will start a fire laser coroutine. If the laser beam is being fired, it will move towards the player depending on which side it’s on. Lastly, once off screen below the enemy will randomly appear above and the “has used laser beam” bool variable will be set to false again.

So for the IEnumerator coroutine to activate the laser, I set all variables and objects to true and played the sound effect, waited for 1 second, then set the firing bool and object to false.

In Update(), I made sure the player was alive before trying to define it’s position and put the enemy movement under the check as well so that it doesn’t keep trying to laser the player’s position on game over.

I also created a public method to set the firing laser bool to false that the laser beam script can call to stop movement if colliding with player early.

Programming the laser beam

Inside the laser beam script, I needed variables to communicate with the player and enemy scripts to call damage and stop laser beam methods respectively.

After getting handles to the scripts, I created an OnTriggerEnter2D collision method and when colliding with player, the player gets damaged, and the enemy stops the laser.

Programming the spawn manager

To add my new enemy to my existing spawning routine, I increased the array size for my enemy GameObject array variable and dragged the new enemy prefab into the slot inside the Unity inspector.

Inside the script, I created a new if statement underneath my level 1 and level 2 logic, this time checking if it’s level 3.

For level 3, I want all 3 enemies to have an equal chance to spawn. I created a new random range integer and instantiated the random range giving each enemy an equal chance.

removed level 1 and level 2 logic for screen shot cleanliness

I now have 3 unique enemies that get introduced as the game progresses.

--

--

Austin Young

o/ I'm a game dev documenting my journey. Mostly technical tutorials. Currently focusing on the Unity engine. My Portfolio: ajamesgames.wixsite.com/website-83