Wave Trigger

The Runtime Spawner includes a complete wave spawning system, similar to Tower Defense games, and many other ‘hold & defend’ games and missions (Left 4 Dead, etc). The Trigger area is defined by a Box Collider (set to ‘Trigger’) as shown in the image below.

To Create a new Wave Trigger in the scene, click the ‘+’ button on the Wave Triggers section of the Runtime Spawner UI. This will automatically add a child game object for the Runtime Spawner and add the required components (Wave Trigger / Box Collider).

Note: Wave Triggers and Wave Spawnpoints do not need to be children of the Runtime Spawner object (new in 1.3.0). This lets you add them to additively loaded scenes or procgen tiles that are added at runtime at a later point.

Properties

Wave Spawn Point

The Wave Spawn Point component defines the location where AI Agents that have been spawned by a Wave (as triggered by the Wave Trigger component, above) will be spawned in the world.

Properties

Spawn Range

Defines a spawn area for this spawn point. A random spot within the range will be used to spawn the AI Agents. This range is visible as a scene view gizmo as shown below.

Fundamentals

WaveTrigger is a trigger volume that activates a WaveSpawner sequence when the player enters its collider. It supports optional reactivation after a cooldown and provides editor helpers for authoring spawn points.

Use a WaveTrigger to drive:

  • Scriptable wave encounters (hordes, ambushes, defense events).

  • One-shot or repeatable combat arenas.

  • Waves that start on player approach or automatically on level start.


Requirements

  • A BoxCollider on the same GameObject.

  • BoxCollider.isTrigger must be enabled.

  • A RuntimeSpawner must exist in the scene (the trigger registers with it at runtime).

  • A WaveSpawner asset assigned to configure the waves.

The collider is auto-assigned to TriggerZone in OnEnable().


Responsibilities

  • Detect activators (by tag) entering the trigger volume.

  • Activate the configured WaveSpawner via the central RuntimeSpawner.

  • Optionally auto-start on scene load.

  • Optionally re-arm after a cooldown to allow repeated encounters.

  • Provide optional WaveSpawnPoint anchors to control spawn locations.


Key Properties

Trigger and state

  • BoxCollider TriggerZone The collider used as the activation volume. Auto-assigned in OnEnable().

  • int ActiveWave The current wave index (0-based) being spawned for this trigger. Managed by the wave runner / RuntimeSpawner.

  • bool IsActivated Indicates whether this trigger is currently active:

    • true while a sequence is running, or when marked active.

    • Set to false when reset or before first activation.

Wave configuration

  • WaveSpawner WaveSpawner The wave table used when this trigger activates. Must be assigned for the trigger to function in play mode.

  • List<WaveSpawnPoint> WaveSpawnPoints Optional anchor points used during wave spawning. When present, spawn locations are chosen among these anchors (selection handled by the wave system).

Activation & reactivation (serialized)

  • bool startAutomatically When enabled, the trigger activates automatically after both:

    • RuntimeSpawner is initialized, and

    • WaveTrigger has started.

  • string playerTag Tag of GameObjects that can activate the trigger when entering the volume (defaults to "Player").

  • bool reactivateTrigger When enabled, the trigger will automatically reset after a cooldown and can be activated again.

  • float reactivateTime Time in seconds before the trigger resets and becomes eligible for activation again.


Events

public static event Action<WaveTrigger> onWaveTriggerActivated;
public static event Action<WaveTrigger> onWaveTriggerReset;
  • onWaveTriggerActivated Invoked when this trigger activates. RuntimeSpawner listens to this event to start the wave sequence.

  • onWaveTriggerReset Invoked after the cooldown when the trigger becomes eligible for reactivation (if reactivateTrigger is enabled).


Runtime Behaviour

Initialization and registration

On enable:

  • Subscribes to RuntimeSpawner.onSpawnerInit (play mode only) to detect when the spawner is ready.

  • Ensures TriggerZone is assigned and set to isTrigger = true.

  • If a RuntimeSpawner reference already exists, registers this trigger with RegisterWaveTrigger.

On disable:

  • Unsubscribes from RuntimeSpawner.onSpawnerInit.

  • Unregisters from RuntimeSpawner if previously registered.

On start (play mode):

  • If WaveSpawner is not set, no further initialization occurs.

  • Locates a RuntimeSpawner (FindAnyObjectByType<RuntimeSpawner>()).

  • Registers with the spawner via spawner.RegisterWaveTrigger(this).

  • Synchronizes initialized with spawner.IsInitialized.

  • If startAutomatically is enabled, begins ActivateWhenReady() to auto-fire once the spawner reports ready.

Automatic activation

ActivateWhenReady():

  • Waits until spawner.IsInitialized == true.

  • If the spawner never initializes, logs an error and exits.

  • If IsActivated is already true, exits to avoid double-firing.

  • Marks IsActivated = true and invokes onWaveTriggerActivated(this).

Volume-based activation

OnTriggerEnter(Collider other):

  • Only runs in play mode.

  • Ignores activation if:

    • IsActivated is already true,

    • No RuntimeSpawner exists, or it is not initialized,

    • The entering collider does not match playerTag.

  • When all conditions are satisfied:

    • Sets IsActivated = true.

    • Invokes onWaveTriggerActivated(this).

    • If reactivateTrigger is enabled, starts the reactivation coroutine.

Reactivation

ResetTrigger():

  • Public method to start a reset cycle from scripts or editor buttons.

  • Starts the TimeToReset() coroutine.

TimeToReset():

  • Waits for reactivateTime seconds.

  • Sets IsActivated = false.

  • Invokes onWaveTriggerReset(this).

This allows repeatable trigger behaviour when reactivateTrigger is enabled.


Editor Helpers

These members assist with authoring in the Scene view (editor only):

public void AddSpawnpoint()
  • Creates a new child WaveSpawnPoint under this trigger.

  • Positions it at the last spawn point’s position (if present), otherwise at the trigger’s position.

  • Adds it to WaveSpawnPoints and selects it in the hierarchy.

public void ShowHide(bool showHide)
  • Controls whether the trigger volume gizmo is drawn in the Scene view.

OnDrawGizmos():

  • Draws the TriggerZone volume when showHideTrigger is enabled.

  • Uses RuntimeSpawnerSettings.waveTriggerColor by default.

  • Uses a different color when IsActivated is true.


Typical Usage Patterns

1. One-shot encounter volume

A basic encounter that fires once when the player enters a room:

  1. Add a WaveTrigger GameObject to the scene.

  2. Add/ensure:

    • BoxCollider with isTrigger = true

    • WaveTrigger

  3. Assign:

    • WaveSpawner asset in the inspector.

    • Optional WaveSpawnPoints using the “Add Spawnpoint” editor button.

  4. Leave:

    • startAutomatically = false

    • reactivateTrigger = false

  5. When the player (matching playerTag) enters the volume:

    • The trigger activates.

    • RuntimeSpawner begins the configured wave sequence.

    • The trigger will not fire again unless reset manually.


2. Repeatable horde zone

A zone that can be revisited and re-challenged after a cooldown:

  1. Configure the WaveTrigger as above.

  2. Set:

    • reactivateTrigger = true

    • reactivateTime = 30 (for example)

  3. Each time the player enters:

    • If not currently active, the trigger activates and starts waves.

    • After the sequence and cooldown, the trigger resets and can be activated again on re-entry.

Optional: hook into onWaveTriggerReset to update UI or minimap icons when the encounter becomes available again.


3. Auto-start level wave

A wave sequence that starts automatically after the spawner is initialized (e.g., survival mode opening wave):

  1. Configure the WaveTrigger with a WaveSpawner.

  2. Set:

    • startAutomatically = true

    • reactivateTrigger as desired (for repeat or one-shot).

  3. When play begins:

    • The trigger waits until RuntimeSpawner.IsInitialized reports true.

    • The trigger activates and starts the wave sequence without requiring a player collider.

This pattern works well for scripted intro fights or survival modes that begin immediately.


4. Manual activation via script

To start a wave from code (e.g., from a console interaction or a UI button), call through the RuntimeSpawner:

using UnityEngine;
using MegaCrush.Spawner;

public class WaveConsole : MonoBehaviour
{
    [SerializeField] private RuntimeSpawner spawner;
    [SerializeField] private WaveTrigger trigger;

    public void ActivateFromConsole()
    {
        if (spawner == null || trigger == null)
            return;

        // Uses the same flow as trigger activation
        spawner.ActivateWaveTrigger(trigger);
    }
}
  • Attach WaveConsole to an in-world console or UI button.

  • Wire ActivateFromConsole to the interaction (OnClick, input event, etc.).

  • The wave will start using the same internal path as volume-based activation.


5. Listening for trigger activation/reset

To react to wave trigger lifecycle globally:

using UnityEngine;
using MegaCrush.Spawner;

public class WaveTriggerMonitor : MonoBehaviour
{
    private void OnEnable()
    {
        WaveTrigger.onWaveTriggerActivated += HandleActivated;
        WaveTrigger.onWaveTriggerReset     += HandleReset;
    }

    private void OnDisable()
    {
        WaveTrigger.onWaveTriggerActivated -= HandleActivated;
        WaveTrigger.onWaveTriggerReset     -= HandleReset;
    }

    private void HandleActivated(WaveTrigger trigger)
    {
        Debug.Log($"WaveTrigger activated: {trigger.name}");
        // Example: update HUD, lock doors, start encounter music
    }

    private void HandleReset(WaveTrigger trigger)
    {
        Debug.Log($"WaveTrigger reset: {trigger.name}");
        // Example: show “encounter available” marker again
    }
}

This pattern integrates wave encounters with UI, audio, and other gameplay systems.

Last updated