My 90 day journey becoming a Unity game developer: Day-40

Rhett Haynes
5 min readJul 27, 2021

--

Objective: Setting up the enemy fire system.

Final result with the enemies being able to shoot at the player.

We’re going to drag the Enemy prefab into the Hierarchy so we can see the enemy ship on the screen, but place the Enemy prefab onto the Enemy Container since this is where all the enemies are spawned from.

Inside the Enemy Container let’s create an empty game object that will hold the offset position of the enemy lasers and name it Enemy_Laser. Set the position to the Enemy prefab center position, drag the Laser prefab into the Enemy_Laser container and set the Laser to the center position of the Enemy_Laser. Position the laser in front of one laser cannon, then copy the Laser’s component values and duplicate the Laser.

Add the New Component Values to the duplicate laser and change the X-axis value from either negative or positive depending on which laser position was set first. Plus, change the names of the lasers to Laser_Left and Laser_Right.

Positioning the lasers in front of the ship’s cannons.
Enemy Laser overrides applied.

Make sure to add the Overrides to the Enemy Laser so the laser positions are final.

Make the Enemy_Laser a prefab, delete both the Enemy_Laser and Enemy prefabs from the Hierarchy. Now we will make the lasers Instantiate every 3 to 7 seconds through code.

Enemy Laser container turned into a Prefab.

In the Enemy script, create a new variable Game Object name laserPrefab that will handle the enemy’s lasers. In the Update() function, we will take all the movement code and put it in a new method name CalculateMovement() which will make it easier to debug in the future. Then use the CalculateMovement() inside Update().

Let’s create another new variable that will be used to randomize the enemy shooting between 3–7 seconds name fireRate that we will preset it to 3 seconds. Also, create another new variable name canFire which will let us know if we can fire by being compared against how long the game has been running and set it to -1.

Back in Update(), we will check if the time in the game is more than canFire which means when the game starts it’s at zero which is more than -1 so the enemy can fire immediately. Set the variable fireRate to randomly pick numbers between 3 and 7. Set canFire to equal to the actual time plus fireRate. Then Instantiate the laserPrefab on the enemy’s transform position. To see if the lasers come out correctly from the enemy add Debug.Break() after instantiating. Drag the Enemy_Laser Prefab into the laserPrefab variable slot on the Enemy.

Now you will notice that the enemy’s lasers come out in the right position, but the lasers go upward and destroy the enemy itself. The lasers only know one way to move in our code which is up, therefore in the Laser script we need to differentiate between the player and enemy laser.

Enemies being shot by their own lasers.

Create a variable name isEnemyLaser equals False. In Update(), take all the code that moves the laser in an upward direction and put it in a new method call MoveUp().

Back in Update(), we will check if the isEnemyLaser is False to use the MoveUp() method. If not, we will use the new method MoveDown() copying the same code in MoveUp() but changing the Vector3 to move Down and if the laser move below -8 on the screen to destroy it.

Now one thing left is to let the enemy know when isEnemyLaser is True through script communication. Create a new method name AssignEnemyLaser() and add isEnemyLaser equals True.

Going back to the Enemy script, in Update() the laserPrefab that we’re instantiating we need access to 2 components. To do this we need to instantiate the laser in a game object variable or array. In this case we will use a variable name enemyLaser to instantiate the laser. Then we will create a Laser Array variable name lasers and call on the enemyLaser to get the Components in the Children of the Laser parent. There is 2 ways you can assign the lasers with the first being (lasers[0].AssignEnemyLaser()) for each laser. But, this can get out of control if the enemy is shooting multiple lasers which we would have to assign each laser. The other way is to use a FOR loop which will increment through the length of the lasers and AssignEnemyLaser() to each one.

Finally, to make the enemy’s laser hit the player and do damage we will go back to the Laser script. We will create a OnTriggerEnter2D() method and check if the laser hits the player and isEnemyLaser equals True. If both conditions are true, then we need to access the Player’s component. From there we need to do a null check for the player’s component, and if everything is okay we will call on the Damage() method on the player.

Enemies shooting at the player.

If the player is hit by both lasers of the enemy, they will lose 2 lives at this moment which is okay with me.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet