Giving Random Enemies a Shield

These poor Piranhas are defenseless when it comes to taking fire from the player. My objective for this article is to give random enemies the ability to have a shield that can absorb a single hit. Let’s get going!The first thing I need is an art asset …


This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin

These poor Piranhas are defenseless when it comes to taking fire from the player. My objective for this article is to give random enemies the ability to have a shield that can absorb a single hit. Let’s get going!

The first thing I need is an art asset that can be made into a sprite for the shield visual. I use the same bubble asset I used for the player shield, only re-sizing it for the enemy.

After importing and renaming the enemy shield game object in the Hierarchy, I drag an enemy piranha into the Scene View and make the shield object a child component of the piranha via drag and drop.

The Order In Layer is set to 1 so it appears above the enemy visually.

After placing the shield asset, I un-tick the box to set it to inactive(invisible), and then press Overrides and Apply All changes to the piranha preFab.

Now in the Enemy Class, I need a few variables for the shield. Being that this shield is only taking one hit, I won’t bother with making shield health like I did for the player. All I need is a a random shield variable, a bool to check if the shield is active, and a reference to the shield game object.

I Serialize the Field of the shield game object so that I can assign it to the enemy preFab in the inspector via drag and drop. I need to once again override and accept the preFab changes.

Now that I have a reference for the shield visual, I head back into the enemy class. I assign the randomEnemyShield variable to be a random range between 0 and 4 (5 is exclusive). This gets called once in void start when the enemy is instantiated. I open an if statement to check if the value of the randomEnemyShield is equal to zero. If it is, the shield visual is enabled and the is shield active bool is set to true. This way 1 in 5 enemies will have a shield!

Rather than running these same on death commands for collisions with the player and various projectiles, I make an on death method to be called instead.

I create a another method to check if the shields are active. If they are, the shield visual is set to be inactive and the bool is flipped to false. The return keyword keeps the code from running any further. The else statement calls the enemy on death behavior method upon collision, once the shields have expired.

Now I can head down to the OnTriggerEnter2D method and check collisions for the tusk, player and bubble blaster. All I need to do now is to call the shield checker method at the bottom of each Tag’s collision.

Here is a clip of random enemies having single hit shields. Thanks for reading!


Giving Random Enemies a Shield was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin


Print Share Comment Cite Upload Translate Updates
APA

Jared Amlin | Sciencx (2021-08-03T13:58:08+00:00) Giving Random Enemies a Shield. Retrieved from https://www.scien.cx/2021/08/03/giving-random-enemies-a-shield/

MLA
" » Giving Random Enemies a Shield." Jared Amlin | Sciencx - Tuesday August 3, 2021, https://www.scien.cx/2021/08/03/giving-random-enemies-a-shield/
HARVARD
Jared Amlin | Sciencx Tuesday August 3, 2021 » Giving Random Enemies a Shield., viewed ,<https://www.scien.cx/2021/08/03/giving-random-enemies-a-shield/>
VANCOUVER
Jared Amlin | Sciencx - » Giving Random Enemies a Shield. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/03/giving-random-enemies-a-shield/
CHICAGO
" » Giving Random Enemies a Shield." Jared Amlin | Sciencx - Accessed . https://www.scien.cx/2021/08/03/giving-random-enemies-a-shield/
IEEE
" » Giving Random Enemies a Shield." Jared Amlin | Sciencx [Online]. Available: https://www.scien.cx/2021/08/03/giving-random-enemies-a-shield/. [Accessed: ]
rf:citation
» Giving Random Enemies a Shield | Jared Amlin | Sciencx | https://www.scien.cx/2021/08/03/giving-random-enemies-a-shield/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.