Menu
CoddyTech

Power-Ups

Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 45 of 50.

challenge icon

Challenge

Medium

Power-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.

  • IPickup declares one method, void Collect(Hero hero);.
  • Heart implements it, with amount (2) from the Inspector: it heals the hero by amount and destroys itself.
  • Shield implements it, with duration (5) from the Inspector: it keeps the hero safe from every hit for duration seconds and destroys itself.
  • Hero gets public void Heal(int amount), which adds to the health but never above maxHealth, and logs the health as a hit does; a fallen hero is not healed. It gets public void Protect(float seconds), which keeps it safe from hits until seconds from now, as a hit's safeTime does. It also gets OnTriggerEnter: when what it touched has an IPickup, it calls Collect(this) on it. A CharacterController gets trigger messages without a Rigidbody.
  • Each Wave gets a public 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