Creating a Rare Laser Beam Power-Up in Unity3D
Objective: Create a laser beam power-up that spawns rarely and provides the Player with a laser beam that stays active for 5-seconds.
Step 1: In Unity, drag the Laser_Beam_Power_Up sprite from the Project window and drop it in the Hierarchy window. Add a Box Collider 2D component, with Is Trigger checked, and add a Rigidbody 2D component, with the Gravity Scale set to 0. Then attach the PowerUp script and assign a value of 5 to the Power Up ID.
Step 2: Drag the Laser_Beam_Power_Up GameObject from the Hierarchy window into the Power_Up folder within the Prefabs folder in the Project window.
Step 3: In the SpawnManager script, create a private float variable called _laserBeamPowerUpDelay and assign it a value of 60f. This variable will be used to check against Time.time to spawn the Laser_Beam_Power_Up GameObject after 60-seconds has passed from the start of the game and after every spawn of the Laser_Beam_Power_Up GameObject.
Step 4: Create a private bool variable called _isLaserBeamPowerUpReady and set it to false.
Step 5: Create a new private method called LaserBeamPowerUpActive() and create an if statement stating if Time.time is greater than the value of _laserBeamPowerUpDelay, then _isLaserBeamPowerUpReady will be set to true.
Step 6: In the Update() method, call the LaserBeamPowerUpActive() method. This will check every frame for if Time.time is greater than the value of _laserBeamPowerUpDelay.
Step 7: In the SpawnPowerUpRoutine() method, create an if statement stating that if _isLaserBeamPowerUpReady is equal to true, then instantiate the Laser_Beam_Power_Up GameObject (represented by 5 in the _powerUps array) at the posToSpawn random position, with no rotation, then set the _isLaserBeamPowerUpReady to false, and reassign _laserBeamPowerUpDelay to Time.time plus 60f, which will delay the next Laser_Beam_Power_Up spawn for another 60-seconds.
Next, create an else statement that will then spawn the other power-up GameObjects randomly and at random positions, then save the script.
Step 8: In Unity, drag the Laser_Beam Sprite from the Project window and drop it on the Player GameObject in the Hierarchy window. Add a Box Collider 2D component, with Is Trigger checked, and add a Rigidbody 2D component, with the Gravity Scale set to 0.
Step 9: In the Player script, using [SerializeField], create a private GameObject variable called _laserBeamVisual and save the script.
Step 10: Go back into Unity, select the Player Game Object in the Hierarchy window, and drag the Laser_Beam GameObject into the Laser Beam Visual slot in the Player (Script) component in the Inspector window.
Step 11: In the Player script, create a private bool variable called _isLaserBeamActive and set it to false.
Step 12: In the Start() method, set the active state of the _laserBeamVisual variable to false. This will disable the Laser_Beam GameObject at the start of the game.
Step 13: Create a new Coroutine called LaserBeamPowerDownRoutine() with a yield return of 5-seconds, then set the active state of the _laserBeamVisual to false, and then set the _isLaserBeamActive variable to false. So, when the laser beam is active, it will be active for 5-seconds and then shut off.
Step 14: Create a new public method called LaserBeamActive(), and set _isLaserBeamActive to true, then set the active state of _laserBeamVisual to true, and start the LaserBeamPowerDownRoutine() coroutine to turn the laser beam off after 5-seconds.
Step 15: In the InstantiateLaser() method, add an additional argument that if _isLaserBeamActive is equal to false as well, then instantiate the regular Laser Prefab. So, if _isLaserBeamActive is equal to true, the regular Laser Prefab will not be instantiated and will allow the Laser_Beam GameObject to be active.
Save this script.
Step 16: In the PowerUp script, add a new case, numbered 5, to the switch statement in the OnTriggerEnter2D() method and call the LaserBeamActive() method from the Player script.
Save this script, go back into Unity, and the Laser_Beam_Power_Up GameObject will spawn every 60-seconds and the Laser_Beam GameObject will be active for 5-seconds.