This article will show how to create an omnidirectional shot to kill enemies all around the Player. Since a lot of this is very similar to implementing the Triple Shot Power-Up from a previous article, I will gloss over the identical parts.
In the Player script, add three variables, a GameObject for the Prefab, a bool to know if to use the Omni-Shot, and a float for the duration of the Omni-Shot.
In Start, set the Omni-Shot duration to the Power-Up duration.
In the FireLaser method, add an else if statement checking for if the Omni-Shot is active and instantiate the Omni-Shot Prefab at the Player’s position.
Create a new Power Down Coroutine for the Omni-Shot. Inside it, make a while loop that will run as long as _omniShotDuration is greater than 0, have it yield for 1 second, and then decrease _omniShotDuration by 1; after the while loop has been completed, set _omniShotDuration back to _powerUpDuration and _isOmniShotActive to false.
Create a public method to activate the Omni-Shot and inside of it, create an if and else statement that checks if _isOmniShotActive is false and if so set it to true, and start the Omni-Shot Coroutine, else increase _omniShotDuration by _powerUpDuration. Also, add an if statement checking if Triple Shot is active, and if so, set it to false and the Triple Shot duration to 0.
In the TripleShotActivate method, add an if statement checking if Omni-Shot is active, and if so, set it to false and the Omni-Shot duration to 0.
Now, create a new script for the Omni-Shot, and add three float variables. The first is for the maximum scale amount that the Omni-Shot will increase in size to before stopping. The second is for the rate at which the Omni-Shot will expand from the Player. The last float is what the Omni-Shot’s fade will be multiplied by once it has reached the maximum scale. Also, we need a reference to the Sprite Renderer to fade the sprite out and a bool to know if the sprite is fading.
In Start, set the _renderer to the SpriteRenderer component and null check it.
Create a Coroutine that will fade out the sprite once it has reached the maximum scale. In the Coroutine, we need a local float which will be the alpha or opacity of the Sprite Renderer’s Color. Now add a while loop that will run as long as opacity is greater than 0.1. In the loop, subtract the time since the last frame multiplied by _fadeMultiplier then set the Sprite Renderer’s Color to a new Color with the RGB values of the Renderer’s Color and the alpha to opacity, and yield till the next frame. After the while loop is complete, destroy the Omni-Shot gameObject.
In Update, add an if statement that checks if the scale of the Omni-Shot’s x-axis is less than the maximum scale. Then increase the scale of Omni-Shot by 1 on all axes multiplied by _rateOfExpasion and time since the last frame. Then add an else if that makes sure the sprite is not already fading. If so, then start the SpriteFadeRoutine and set _isFading to true.
In the Enemy script, add an if statement checking for the “Omni Shot” Tag in the OnTriggerEnter2D method. Inside of the if, null check the Player reference and call the AddScore method on it. Next, check if the Enemy is not dead; if so, play the explosion sound and _isDead to true. Then set the trigger on the animator to play the death animation, set the speed to 0, disable the Enemy’s Collider and destroy the Enemy after the delay.
In the PowerUp script, we need to add an OmniShot element to the PowerUpType Enum for the Omni-Shot Power-Up.
Then add the PowerUpType.OmniShot case to the switch statement and have it call the OmniShotActivate method on the Player.
Now to create an Omni-Shot Power Up to put the script on.
Then create the Omni-Shot Prefab, Tag it as Omni Shot, and hook it up to the Player.
When the Player collects the Omni-Shot Power Up, they can fire an omnidirectional blast.