Menu
CoddyTech

Enemy Types

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

challenge icon

Challenge

Medium

The enemies arrive. Keep everything from the last lesson. Two red grunts and an orange dasher stand in the arena, tagged Enemy, so the swing already knocks them out. Fill in three scripts:

  • Enemy becomes an abstract base. It has a protected float speed that the Inspector sets. In Start it finds the hero with FindAnyObjectByType<Hero>() and keeps its Transform. Every frame it works out the way to the hero along the ground (y set to 0) and passes it to protected abstract void Move(Vector3 toHero), which has no body.
  • Grunt derives from Enemy and overrides Move: it walks toward the hero at speed units a second (2 in the Inspector), and stands still once it is 1 unit from the hero or closer.
  • Dasher derives from Enemy, with restTime (1.5) and dashTime (0.4) that the Inspector sets. Its Move rests for restTime seconds, then dashes for dashTime seconds at speed (10) in the direction the hero was in when the dash began, then rests again, and so on.

Press Play and dodge them, then Check.

Try it yourself

using UnityEngine;

public class Enemy : MonoBehaviour
{
    // A later lesson fills this in: what every enemy has in common.
}

All lessons in Game Architecture in Unity