Unity Guide
Implementing space shooter game features - Enemy shields
A quick review of new features added to a space shooter game in Unity
Objective: Implement shields for the enemies in a space shooter game with Unity.
In the previous post I implemented a new type of enemy with a laser beam shot in my space shooter game with Unity. Now it’s time to implement shield in some of the enemies that are spawned per wave.
Shield prefab
In order to implement shield for the enemies, we’ll need to change the way we handle the player shield and make it an individual object that handles almost all of the collisions with other gameobjects and shots.
In the next image we can see the shield of the player, which is a disabled gameobject that is enabled and disabled again when the player receives damage:
So, in order to make the shield an independent object, let’s add a 2D collider that fits well with the sprite and let’s enable the Is Trigger property:
Then, let’s modify the collider in the scene view to cover the shield sprite:
Also, to handle the collisions, let’s add a 2D rigidbody and set the gravity scale to 0:
Now, let’s make a prefab of it by dragging it into the respective prefab folder. As we already have one for the player, (and we only need to modify some values in the enemy shield) we can choose to make a new prefab variant instead of a new prefab:
In order to distinguish the shield prefabs we can use a tag or a bool value for each one:
Shield class
Now, let’s open the Shield script in order to handle the game logic. In the script we’ll need a new variable:
- Is enemy shield
This new variable indicates if the shield prefab is going to be instantiated to cover an enemy. By using [SerializeField] we can make sure to modify its value through the inspector.
If you want to know what the other variables do, you can check the shield implementation for the player in this older post:
Now, let’s change the bool method (which indicates that the shield receives damage and returns if the shield needs to remain active) into a void method that disables the shield when there’s no resistance left:
Then, in order to handle the collisions with other gameobjects, let’s use the OnTriggerEnter2D method:
First, we’ll make sure that the shield is of the player, so that we can handle the collisions between the shield and:
- Other enemy shields
- Enemy shots
- Enemies
Then, if the shield is from an enemy, we’ll need to check the collisions between the shield and:
- The player’s shield
- Shots from the player
- The player itself
Spawning shields
And now, in order to spawn the enemies with a shield (without having to add it in their respective prefabs) we’ll need to create 2 new variables in the spawn manager script:
- Enemy shield
This variable will store a reference to the shield prefab. We can drag the prefab into the inspector by using [SerializeField].
- Shield probability
This variable will store a decimal value that will indicate the probability to spawn a new enemy with a shield.
Finally, let’s add a couple of lines in the function that spawns the enemies from each wave. We’ll compare the value of Random.value (which returns a value between 0 and 1 inclusively) with the shield probability and if it is within the range we’ll instantiate the shield prefab as a child of the new enemy to be spawned:
And now, if we run the game in Unity, we’ll be able to see that some enemies have shields and the shields work within their own class:
And that’s it, we implemented shields for the enemies! :D. I’ll see you in the next post, where I’ll be showing more features added to my space shooter game in Unity.