2D Galaxy Shooter P1: Rare Secondary Weapon Added!
On today's challenge, we had to implement a rare secondary weapon power-up!
Let me introduce you to “God's Wish!”
I had a hard time implementing this on my own. I had to look for help in how to have the projectiles shoot in a 360-degree radius. It requires Math smart, and that is something that I am working towards.
It’s all about trigonometry and how to calculate angles. Sin and Cos would get us the angles needed to shoot around with a bit of PI involvement.
A lot of the implementation is the same as the one I did for the Tripleshot power-up. I will go through the different parts of the implementation.
Since it’s a rare power-up, I had to give a simple probability system that will do the trick. In the SpawnManager script, within the spawn power-up logic, had to alter the code into this:
private IEnumerator SpawnPowerUpRoutine()
{
if (_gameManager.IsNewScene)
{
yield return new WaitForSeconds(_waitToSpawn);
} while (!_stopSpawning)
{
float randomX = Random.Range(-11, 11);
Vector3 randonXposition = new Vector3(randomX, 8, 0); int randomPowerUp = Random.Range(0, _powerupPrefabs.Length);
float randomValue = Random.value; if (randomValue < 0.95f) //Instantiate Common power-ups
Instantiate(_powerupPrefabs[randomPowerUp], randonXposition, Quaternion.identity); int randomRare = Random.Range(0, _rarePowerupPrefabs.Length);
if (randomValue >= 0.95f) //Instantiate Rare power-ups
{
Instantiate(_rarePowerupPrefabs[randomRare], randonXposition, Quaternion.identity);
} float randomTime = Random.Range(6, 20);
yield return new WaitForSeconds(randomTime);
}
}
Perhaps, it’s not the best way to implement a probability system. It is simple enough for it to work in the current state of the game.
If the random value is less than 95%, instantiate a common power-up. If the random value is greater or equal to 95%, instantiate a rare power-up. It leaves a 5% chance for God’s Wish to spawn. It’s too overpowering!
In the Projectile script, I had it so there could only be two directions when shooting, up or down.
private void Update()
{
if (!_isEnemyLaser)
MoveUp();
else
MoveDown();
}
With the addition of the new rare power-up, I had to create a new type of projectile so it had its own behavior when out of bounds.
private void Update()
{
if(_type == ProjectileType.Laser)
{
if (!_isEnemyLaser)
MoveUp();
else
MoveDown();
}if (_type == ProjectileType.GodsWish)
DestroyGodsWishProjectile();
}
In bold, if the type of projectile is God’s Wish, then call the function to destroy all projectiles out of bounds! Using an enum to identify the projectiles, we can have each type not be affected by other behavior.
And finally, in the shooting logic, I added a new condition to check if God’s Wish is activated. If it is, then we need to make sure it shoots in the behavior we want. And the behavior we want is for it to fire many projectiles in a radius.
else if (_isGodsWishActive)
{
int numberOfProjectiles = 20;
float radius = 5f; float angleStep = 360f / numberOfProjectiles;
float angle = 0f; for (int i = 0; i <= numberOfProjectiles; i++)
{
float projectileDirXposition = transform.position.x + Mathf.Sin((angle * Mathf.PI) / 180) * radius;
float projectileDirYposition = transform.position.y + Mathf.Cos((angle * Mathf.PI) / 180) * radius; Vector3 projectileVector = new Vector3(projectileDirXposition, projectileDirYposition, 0);
Vector3 projectileMoveDir = (projectileVector - transform.position).normalized * 5f; var projectile = Instantiate(_godsWishPrefab, transform.position, Quaternion.identity);
projectile.GetComponent<Rigidbody2D>().velocity = new Vector3(projectileMoveDir.x, projectileMoveDir.y, 0); angle += angleStep;
} _audioManager.PlayGodsWishSound();
}
We want to know how many projectiles to instantiate in the starting position. We divide 360-degrees by the number of projectiles to instantiate. We then want to iterate through all of the projectiles so they can have the logic to move in their respective direction.
With this challenge out of the way, we can now move on to the next!
The next challenge will be to implement a camera shake when the player takes damage!
Thank you for your time!