Power-Ups
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 45 of 50.
Challenge
MediumPower-ups. Keep everything from the earlier lessons. A heart and a shield lie in the arena, and the Heart and Shield prefabs are ready for the waves to drop. Everything the hero can pick up implements one interface, so the hero never needs to know the kinds.
IPickupdeclares one method,void Collect(Hero hero);.Heartimplements it, withamount(2) from the Inspector: it heals the hero byamountand destroys itself.Shieldimplements it, withduration(5) from the Inspector: it keeps the hero safe from every hit fordurationseconds and destroys itself.Herogetspublic void Heal(int amount), which adds to the health but never abovemaxHealth, and logs the health as a hit does; a fallen hero is not healed. It getspublic void Protect(float seconds), which keeps it safe from hits untilsecondsfrom now, as a hit'ssafeTimedoes. It also getsOnTriggerEnter: when what it touched has anIPickup, it callsCollect(this)on it. A CharacterController gets trigger messages without a Rigidbody.- Each
Wavegets apublic GameObject reward. The Inspector cannot show a field of an interface type, so the field holds the prefab as a GameObject. When a wave is defeated, before the rest, the spawner drops the wave's reward, if it has one, at its own position in the middle of the arena. The asset gives wave 1 a Heart and wave 2 a Shield.
Press Play, clear a wave and grab its reward, then Check.
Try it yourself
// A later lesson fills this in: anything the hero can pick up.
public interface IPickup
{
}
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular Turret10Project: Arena Survivor
The HeroEnemy Types