New Enemy Type with Shield

How to create shielded enemies

To create this behavior we create a new variation of the Enemy Damage script. We created a child game object shield that is set active and when the enemy gets hit it doesn’t take damage and the shield is deactivated. We use the following code:

private void OnTriggerEnter2D(Collider2D other)
{
//Debug.Log(other);
if (_isShieldActive == true)
{
_shield.SetActive(false);
_isShieldActive = false;
if (other.tag == "Player")
{
//dmg player
_player.Damage();
}
else if (other.tag == "Laser")
{
//destroy laser
Destroy(other.gameObject);
}
}
else {
//if other is player
if (other.tag == "Player")
{
//dmg player
_player.Damage();
//add 10 to score
_player.AddScore(10);
//destroy us
StartCoroutine(EnemyDeath());
}
//if other is laser
else if (other.tag == "Laser")
{
//destroy laser
Destroy(other.gameObject);
//add score
_player.AddScore(20);
//destroy us
StartCoroutine(EnemyDeath());
}
}
}

--

--

--

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

PL/SQL : Everything You Need To Know About PL/SQL

Releasable APK can’t be installed? — Strange App launcher behaviors (part 3)

Critical controls for a DevSecOps practice — Part 1

Coding guidelines and best practices

Git Hooks — The Missing Tutorial

Running Python as a Scheduled Task (Windows)

Using Rust for Gamedev

Interactive CLI (Command Line Interface) applications with Ruby Gems.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tristan Engel

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

More from Medium

2.5D Platformer

Run Klipper/OctoPrint In Void Linux

Piano Concerto in C minor Rachmaninoff

CS373 Spring 2022: Ruchi Bhalani