Boss Fight
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 50 of 50.
A stone golem guards the gate out of the arena. It stomps toward you, and when you come close it raises its fists for a second and slams the ground, which hurts you if you are still near. After a slam it rests, and only then can your sword hurt it: its stone skin shrugs off every hit while it walks or winds up. Its color says what it is doing: gray as it walks, red as it winds up, cyan as it rests. Bring its health to 0 and the gate opens. The bar at the top of the screen follows every hit.
Write the golem's brain as a state machine with a class for each state: Boss, and ChaseState, WindupState and RestState in BossStates.cs. The player, the health bar, the gate, IState, StateMachine and IDamageable are ready.
Challenge
HardThe Inspector gives Boss player, speed (2), slamRange (2.5), windupTime (1), restTime (2), slamDamage (2) and maxHealth (6), and wires its onDefeated UnityEvent to the Gate's Open. PlayerDistance() and Tint(color) are ready.
BossimplementsIDamageable. InAwakeit sets its health tomaxHealthand makes aStateMachineand its three states, kept in public fieldsmachine,chase,windupandrest(each state gets the boss in its constructor). InStartit raisesHealthChangedwith its health andmaxHealthand starts inchase; every frame it ticks the machine.ChaseState: tints the golemColor.gray, and walks it toward the player along the ground atspeed. Once the player is withinslamRange, it switches to the windup.WindupState: tints itColor.redand stands still forwindupTimeseconds. Then it logsSlam!, gives the playerslamDamagethrough itsIDamageableif the player is still withinslamRange, and switches to the rest.RestState: tints itColor.cyanand waitsrestTimeseconds, then switches back to the chase. Every windup and every rest lasts its full time.TakeDamagecounts only while the golem is in its rest state and still standing: it takes the amount off and raisesHealthChanged. At 0 health it logsThe golem falls, invokesonDefeatedand deactivates itself.
Press Play, dodge the slams and strike while it rests, then Check.
Try it yourself
using System;
using UnityEngine;
using UnityEngine.Events;
public class Boss : MonoBehaviour
{
public Transform player;
public float speed = 2f;
public float slamRange = 2.5f;
public float windupTime = 1f;
public float restTime = 2f;
public int slamDamage = 2;
public int maxHealth = 6;
public event Action<int, int> HealthChanged;
public UnityEvent onDefeated;
// How far the player is, across the ground.
public float PlayerDistance()
{
Vector3 d = player.position - transform.position;
d.y = 0f;
return d.magnitude;
}
// Gray while it walks, red while it winds up, cyan while it rests.
public void Tint(Color color)
{
GetComponent<Renderer>().material.color = color;
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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