Menu
CoddyTech

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 icon

Challenge

Hard

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

  • Boss implements IDamageable. In Awake it sets its health to maxHealth and makes a StateMachine and its three states, kept in public fields machine, chase, windup and rest (each state gets the boss in its constructor). In Start it raises HealthChanged with its health and maxHealth and starts in chase; every frame it ticks the machine.
  • ChaseState: tints the golem Color.gray, and walks it toward the player along the ground at speed. Once the player is within slamRange, it switches to the windup.
  • WindupState: tints it Color.red and stands still for windupTime seconds. Then it logs Slam!, gives the player slamDamage through its IDamageable if the player is still within slamRange, and switches to the rest.
  • RestState: tints it Color.cyan and waits restTime seconds, then switches back to the chase. Every windup and every rest lasts its full time.
  • TakeDamage counts only while the golem is in its rest state and still standing: it takes the amount off and raises HealthChanged. At 0 health it logs The golem falls, invokes onDefeated and 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;
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Game Architecture in Unity