Enemy Types
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 42 of 50.
Challenge
MediumThe 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:
Enemybecomes anabstractbase. It has aprotected float speedthat the Inspector sets. InStartit finds the hero withFindAnyObjectByType<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 toprotected abstract void Move(Vector3 toHero), which has no body.Gruntderives fromEnemyand overridesMove: it walks toward the hero atspeedunits a second (2 in the Inspector), and stands still once it is 1 unit from the hero or closer.Dasherderives fromEnemy, withrestTime(1.5) anddashTime(0.4) that the Inspector sets. ItsMoverests forrestTimeseconds, then dashes fordashTimeseconds atspeed(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
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular Turret10Project: Arena Survivor
The HeroEnemy Types