Damage Everything
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 52 of 64.
Challenge
MediumLet the sword hurt anything that can be hurt, through an interface.
- In
IDamageable.cs, declarevoid TakeDamage(int amount);. EnemyimplementsIDamageable:TakeDamagetakesamountoffhealthand destroys the enemy at 0 or below.- In
Hero.Swing(), replace the tag test andDestroywithhit.TryGetComponent(out IDamageable target)andtarget.TakeDamage(swingDamage);.
Press Play and swing at a grunt: it takes two hits. Then Check.
Try it yourself
// A later lesson fills this in: anything that can be hurt.
public interface IDamageable
{
}
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular TurretRecap: Twin Cannon4Serialized Data
Serializable ClassesLists in the InspectorEnums in the InspectorRecap: Loot TableRecap: Roll the Loot7State Machines
States as ClassesThe Alert StateA State MachineTransitionsEnemy BrainsThe Knight AttacksRecap: Door States10Project: Arena Survivor
The HeroEnemy Types2Interfaces in Unity
IDamageableInterfaces and TryGetComponentGetComponentsRecap: Interact KeyRecap: Nearest in ReachPractice on your own: Unity playground