Nerd For Tech
Published in

Nerd For Tech

Unity Guide

IDamageable interface | Unity

A quick guide about how to implement an IDamageable interface in Unity

Objective: Implement an IDamageable interface in the enemies of a 2D game to indicate that they have health and are damageable.

In the last post I covered how to use abstract classes to design enemies in Unity. Now, it’s time to work with interfaces in order to let the player know that the enemies are damageable.

Current enemies

To start, let’s take a look at the enemies in our 2D game. Currently, we have a moss giant, a spider and a skeleton that patrol on certain areas of the level:

IDamageable interface

Now, in order to indicate that certain gameobjects are damageable (like our enemies), let’s create a new C# script named IDamageable:

Once created, let’s open the script and modify the IDamageable class to be of type interface:

Note: An IDamageable interface will allow us to force certain classes to implement properties and/or methods that are needed to handle damage.

Then, let’s declare a new property and a method to handle the damage in certain classes like the enemies:

  • Health

This property will determine the amount of health that each class will have.

  • Damage

This method will take care of the damage application to the health within the class.

Once we save the interface, let’s open the class of an enemy and make sure to declare that it’s damageable by implementing the IDamageable interface:

You’ll notice that the class displays an error as soon as we declare that the class implements the interface:

This is due to the requirements of implementing the IDamageable interface. To fix it, we need to declare the respective health property and damage method within the class:

Once declared, we’ll see that the error disappears from the script. Then, let’s do the same to the other enemy classes:

And now, we’re able to declare what to do in each one of our enemies. For example, we can subtract a unit from the Health property in the Damage method:

This interface implementation allows us to call the Damage method without restrictions on what we want to damage in an attack. We could implement this interface in a wood box, an enemy or even the player. To use it, for example, we can get the IDamageable component from the collider on a collision and call the Damage method:

And that’s it, now we can use an interface to indicate that our enemies are damageable in Unity! :D. I’ll see you in the next post, where I’ll be showing a way to implement the hitbox detection within melee combat in Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Integrations with Apache Camel

My KubeCon & CloudNativeCon Europe Experience 2019

What Is Mulesoft And Why It Is Important For Your Digital Enterprise Model

Generating tools, Energy and Durability

Webinar: Increasing Product Release Velocity by Debugging and Testing in Production

Does Hadoop really use the concept of parallelism while uploading files to the cluster?

Fatal flaw of Ethereum 2.0

Spctrm (Google chrome extension)

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
Fernando Alcantara Santana

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).

More from Medium

How to Switch Virtual Cameras Using Only Cinemachine and Scripting in Unity

Cross platform input controller | Unity

Adding Animations to Point and Click Controller

Creating a UI Shield Strength indicator