Skip to content

Advanced Features: DunGen Character Component

The DunGen Character component is a simple helper utility provided with DunGen that makes it easy to detect when a specific GameObject (like your player character or an important NPC) moves from one generated Tile to another.


What it Does

  • It monitors the Tile that the GameObject it's attached to currently occupies.
  • When the GameObject enters a different Tile's bounds, it fires an event.
  • This requires the GameObject to have a Collider component attached.
  • It relies on the "Trigger Placement" option being enabled on the Runtime Dungeon Generator, as it uses these trigger volumes to detect transitions.

It has no configurable settings in the Inspector – its sole purpose is to provide this transition event via code.


How to Use It

  1. Add the Component: Attach the DunGen Character component to the GameObject you want to track (e.g., your player character). You can add it via the menu: Add Component > DunGen > Character. Ensure this GameObject also has a Collider component.
  2. Enable Tile Triggers: Make sure the Trigger Placement mode is set to either 3D or 2D (depending on your dungeon's perspective) on your Runtime Dungeon component in the scene. Set the Trigger Layer appropriately if needed.
  3. Subscribe to the Event: In another script (e.g., your player controller or a UI manager), get a reference to the DunGenCharacter component and subscribe to its OnTileChanged event.

Example Script:

PlayerTileTracker.cs
using UnityEngine;
using DunGen; // Required for DunGenCharacter, Tile

public class PlayerTileTracker : MonoBehaviour
{
    private DungenCharacter characterComponent;
    private Tile currentTile;

    private void Start()
    {
        // Get the DunGenCharacter component attached to the same GameObject
        characterComponent = GetComponent<DungenCharacter>();

        if (characterComponent == null)
        {
            Debug.LogError("DungenCharacter component not found on this GameObject!", this);
            return;
        }

        // Subscribe to the tile changed event
        // The 'OnPlayerTileChanged' method below will be called when the event fires
        characterComponent.OnTileChanged += OnPlayerTileChanged;

        // Optional: Get the initial tile the character starts in
        currentTile = characterComponent.CurrentTile;

        if (currentTile != null)
            Debug.Log($"Player started in Tile: {currentTile.name}");
    }

    private void OnDestroy()
    {
        // IMPORTANT: Always unsubscribe from events when the listener is destroyed
        if (characterComponent != null)
            characterComponent.OnTileChanged -= OnPlayerTileChanged;
    }

    // This method signature matches the event delegate
    // It will be executed whenever the character enters a new tile trigger
    private void OnPlayerTileChanged(DungenCharacter character, Tile previousTile, Tile newTile)
    {
        // Store the new current tile
        currentTile = newTile;

        // Log the transition
        string prevTileName = (previousTile != null) ? previousTile.name : "None";
        string newTileName = (newTile != null) ? newTile.name : "None";
        Debug.Log($"Player moved from Tile '{prevTileName}' to Tile '{newTileName}'");

        // --- Add your custom logic here ---
        // UpdateMinimap(newTile);
        // TriggerRoomSpecificAmbience(newTile);
        // LoadResourcesForTile(newTile);
        // UnloadResourcesForTile(previousTile);
    }

    // Example method you might call from elsewhere
    public Tile GetCurrentTile()
    {
        return currentTile;
    }
}

Event Handler Parameters:

  • DunGenCharacter character: A reference to the DunGenCharacter component that triggered the event (useful if you have multiple tracked characters sharing the same handler).
  • Tile previousTile: The Tile the character was in before the transition. Can be null if this is the first tile entered.
  • Tile newTile: The Tile the character has just entered. Can be null if the character exits all tile triggers.

Use Cases

  • Minimap Updates: Highlight the current room/tile on a minimap UI.
  • Environment Effects: Change lighting, ambient sounds, or particle effects based on the current tile the player is in.
  • Resource Streaming: Trigger loading/unloading of assets specific to the entered/exited tiles (if using an asset streaming system).
  • AI Behavior: Notify AI agents about the player's current tile location.
  • Debugging: Log tile transitions to help diagnose generation or gameplay issues.

The DunGen Character component provides a convenient, event-driven way to react to character movement within your generated dungeons without needing to manually manage trigger collisions yourself. Remember the prerequisites: a Collider on the tracked object and "Trigger Placement" enabled on the generator.