Hey and welcome!
I’ve got a quick one for you here. Our last powerup to integrate into our game is the shields powerup and we’re going to be using a mixture of what we’ve learned in order to implement it.
So the first step is to do what you’ve done before with the triple shot and speed powerup and create something that the player can collect complete with animation and appropriate spawning.
Before we get into the code let’s add in the shield itself into our game view making sure that it’s position and the player’s position are both at 0, 0, 0 and then adjust the shield so that it fits nicely around our player.
If you’ve got any animation for your shield be sure to add that in now and then prefab your shield once you’re happy with how it fits around your player.
Now we get into our code, the way we’re going to be doing this is we’re going to instantiate our shield when the powerup is picked up and then assign the shield’s parent as the player so that it follows the player’s movement.
public bool collectedShield = false[SerializeField]
private GameObject _shieldPrefab;
private GameObject _shield;public void EngageShieldPowerup()
_shield = Instantiate(_shieldPrefab, transform.position, Quaternion.identity);
_shield.transform.parent = this.transform;
collectedShield = true;
Take special note that we’re setting our collectedShield variable as a public one as that will come important later.
This code should all be looking very familiar to you, the shield.transform.parent, you’ll recognise from when we were assigning our enemies to the enemy container and this is what lets our shield that we’ve just instantiated follow around the player once we assign it to the player’s transform using this.transform.
If we were to move around our enemy container object while the game was running you would find that the enemies would move along with it too.
The idea of this shield is that it will last indefinitely until the player gets hit by and enemy, so we’ll need to alter our Damage() code a little:
public void Damage()
if (collectedShield == true)
collectedShield = false;
_playerLives -= 1;
Since we’ve set _shield as a global variable we can make a call to it in our code here in order to destroy the instance of the shield we instantiated as Unity would come across issues if we tried to destroy the _shieldPrefab object itself. Then we simply change our collectedShield bool to false and we’re all good in case we pick up another.
Well almost all good, there’s one last thing to do here and is the reason why I’ve set collectedShield as a public variable. It’s time to make your way over to the Powerups script and add this into case 2 in our switch statement:
if (_player.collectedShield == false)
The reason I have this check in here is to stop the player from collecting several instances of the shield on top of the one we have, we only want one shield object in our game at a time and this is what helps us to do that.
With that all done you should have something like this now where your player lives don’t get affected either:
Next article we’ll be getting into the majesty of some of the UI capabilities in Unity.