IDamagable Interface in Unity
Objective: Set up an IDamagable Interface
Currently our Player can pass through the Skeleton and hit it with the sword and nothing really happens. It does register a collision but there is no logic for damage or effect. Let’s fix that. The Skeleton Hit animation was set up following the same splice and transition process as the other animations.
To set up the Interface, create a script called IDamagable(naming structure being I for interface, the name of the script, able at the name), change class to interface and remove the inherits from MonoBehaviour. Next we will use a Health property and Damage method. That’s it for now, nice and simple but we may enhance it later.
Next to use it in the Skeleton we implement the IDamagable interface. We need to set the Health property and add the Damage method or we’ll get an error from not implementing the interface traits. Once this is set up, we are now ready to apply damage to the Skeleton.