‘But I want to fire too!’ said the Enemy
In this article, we will give the enemies the ability to fire lasers also. It is not too hard to implement since we have already done it for the Player.
Create an Empty GameObject on the Enemy Prefab, just like with the Player in a previous article, and once in place, make sure to apply the changes to the Prefab.
Next, create an Empty GameObject named “Enemy Laser” and put two Laser Prefabs as children, enable Is Trigger on both of them, and make it a Prefab just like with the Triple Shot in a previous article.
Create a new Tag named “Enemy Laser” and change the Lasers in Enemy Laser to that Tag. That will prevent the enemies from blowing themselves up.
In the Laser script, add a private bool named _isEnemyLaser set to be false; this will let us tell the Laser which way to move.
Next, take the movement code for the Laser from Update and make two new methods with it named “MoveUp” and “MoveDown”. MoveUp is the normal Laser Movement fired by the Player, so it doesn’t need to be changed. MoveDown will be the Enemy Laser movement, so change the Translate to Vector3.down and the if statement to check if the y-axis position is less than -8. That will have it move down the screen and destroy itself.
In Update, create an if statement that checks if _isEnemyLaser is false; if so, call MoveUp, else call MoveDown. That will control which way the Laser moves based on _isEnemyLaser.
Create an OnTriggerEnter2D method. Inside it, create an if statement that checks if the other Collider’s Tag is “Player” and if _isEnemyLaser is true. If true, get a reference to the Player; null check it. If not null, call the ChangeLives method on the Player and pass in -1. Then Destroy the gameObject.
Finally, create a public method named “AssignEnemyLaser”, inside it set _isEnemyLaser to true; this is how the Laser will be set to a Laser fired by the Enemy.
In the Enemy script, add a private GameObject named “_laserPrefab”, a private Transform named “_laserSpawn”, a private AudioClip named “_laserClip” with a null value, two private ints named “_minFireDelay” with a value of 3 and “_maxFireDelay” with a value of 8, and a private float named “_canFire”. GameObject is a reference to the Enemy Laser Prefab. Transform is the point where the Prefab will spawn. AudioClip is the sound that will play when the Enemy fires. The two ints are the random fire delay range for the Enemy. The float is how the Enemy will know if the fire delay is up.
Create a new method named “PlayClip” that takes in an AudioClip, and inside it, call PlayOneShot on the enemy audio source and pass in the clip. It is just like the Player from a previous article, except it is not public.
Create a new FireLaser method that is going to be based on the Player script’s FireLaser. Inside the method, make a local float named “fireRate” equal to a random number between _minFireDelay and _maxFireDelay. Then set _canFire to equal Time.time plus fireRate, to make the cool-down. Next, call PlayClip and pass in _laserClip. Create a local GameObject named “enemyLaser” and equal the instantiated _laserPrefab that is spawned at the _laserSpawn position. Make a local array of type Laser called “lasers” and set it to the Laser script components of the children of enemyLaser. Finally, create a for loop that will loop till i is greater than lasers.Length and have call the AssignEnemyLaser method on each element. That will turn the Laser Prefabs into enemy lasers that will move down the screen and hit the Player.
In Update, create an if statement that checks if Time.time is greater than _canFire and that _isDead is false, then call the FireLaser method.
In Unity, hook up the Spawn Point, Enemy Laser Prefab, and Laser sound clip to the Enemy Prefab.
Now the enemies are firing back at the Player.