Enemy Types
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 51 of 64.
Challenge
MediumAdd two kinds of enemy on one abstract base.
- Make
Enemyabstract. Each frame it passes the way to the hero, y set to 0, toprotected abstract void Move(Vector3 toHero);. Grunt : EnemyoverridesMove: walk at the hero atspeed, stopping within 1 unit.Dasher : EnemyrestsrestTime, dashesdashTimeatspeedtoward where the hero was, and repeats.
Press Play and dodge them. Then Check.
Try it yourself
using UnityEngine;
public class Enemy : MonoBehaviour
{
// [SerializeField] lets the Inspector set a protected field too.
[SerializeField] protected float speed = 2f;
// 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 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