Day 41 — Improvements: updated spawning and enemy shields

Hey and welcome!

In this article we’ll look at rebalancing our current spawning system and also add shields to our new enemy type.

First let’s have a look at our spawning system and we’ll add in some new code for our new beam enemy that we created last time:

private _beamEnemyCount = 1;
[SerializeField]
private GameObject _beamEnemy;
if (_beamEnemyCount < 1)
{
Instantiate(_beamEnemy, new Vector3(0, 6, 0), Quaternion.identity);
_beamEnemyCount += 1;
}

We’re shoehorning this code into our switch statement for the wave system and it’s just after we check for the regular enemy count. Nothing fancy happening beyond this but make sure you remember to increase the value of _enemiesToDestroy by 1 before running this since we’re adding in an extra enemy.

With that sorted let’s tackle a bigger fish and update our powerup spawning code so that ammo spawns more frequently than regular powerups and the health spawn is pretty rare.

while (_playerAlive == true)
{
float randomValue = Random.value;
if (randomValue <= 0.1f)
{
int multishotChance = Random.Range(0, 7);
Instantiate(_powerups[multishotChance], new Vector3(Random.Range(-10.7f, 10.7f), 6.2f, 0), Quaternion.identity);
yield return new WaitForSeconds(3.0f);
}
else if (randomValue > 0.1f && randomValue < 0.5f)
{
float secondValue = Random.value;
if (secondValue < 0.4f)
{
int randomPowerUp = Random.Range(0, 6);
Instantiate(_powerups[randomPowerUp], new Vector3(Random.Range(-10.7f, 10.7f), 6.2f, 0), Quaternion.identity);
yield return new WaitForSeconds(Random.Range(3.0f, 7.0f));
}
else
{
Debug.Log("Chance for health spawn");
int randomPowerUp = Random.Range(0, 5);
Instantiate(_powerups[randomPowerUp], new Vector3(Random.Range(-10.7f, 10.7f), 6.2f, 0), Quaternion.identity);
yield return new WaitForSeconds(Random.Range(3.0f, 7.0f));
}
}
else if (randomValue > 0.5f)
{
Debug.Log("Ammo Spawn");
Instantiate(_powerups[3], new Vector3(Random.Range(-10.7f, 10.7f), 6.2f, 0), Quaternion.identity);
yield return new WaitForSeconds(Random.Range(3.0f, 7.0f));
}
}

At first there looks to be quite a lot going on here but I’ve only tweaked the if statements for our randomValue variable. We still have the one for our multishot that spawns if the value is less than 0.1f but I’ve added a new one for just spawning ammo whenever the value is more than 0.5f and then everything else spawns when the value is greater than 0.1f or less than 0.5f.

To further reduce the chances of the health spawning I’ve added in a second random value when the first value is between 0.1f and 0.5f that will give a chance for the health to spawn if that second value is below 0.4f. There’s a lot of playing about that can be done with this but with the code in place you’ll be finding that there’s quite a lot of ammo now and everything else becomes a bit of a rare commodity whenever they spawn.

Enemy Shields

With that in place let’s go ahead and add some shields to our beam enemies. Conveniently we already have a shield prefab so let’s go ahead and parent this shield prefab to our new beam enemy and then change the colour to something different than the player’s shield:

We’ll have all the necessary components already on the shield so now it’s just the case of adding in some logic into our collider in the beam enemy script whenever the player laser comes into contact:

private bool _shieldActive = true;
[SerializeField]
private GameObject _shield;
if (_shieldActive == true)
{
_shieldActive = false;
_shield.SetActive(false);
Destroy(other.gameObject);
}

That’s all it takes! With that code in place the enemy will take one hit from the laser before the shield goes down and is left vulnerable to the follow up.

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.