Advanced Features: Doors
In the Getting Started section, we saw how to add Connectors and Blockers to Doorway definitions. While Connectors can be simple archways or openings, they are often used to spawn functional Door prefabs. Doors can also be placed automatically via the Lock & Key System.
This page focuses on what happens when DunGen places a prefab designated as a door and the crucial Door component that gets added.
Spawning Doors
Typically, you designate a door prefab within the Connectors > Random Prefab list of a Doorway component on your Tile prefab.
- Priority: Remember the
Prioritysetting within the Connectors list? If two connected doorways both list potential door prefabs, the doorway with the higher priority value gets its prefab chosen. If priorities are equal, one is chosen randomly. Only one door prefab is spawned per doorway connection pair.
The Door Component
Whenever DunGen successfully places a prefab that functions as a door (either through the Connectors list or the Lock & Key system), it automatically adds a Door component to the root GameObject of the instantiated door prefab if one does not already exist.
This component acts as a vital link between the visual door object and various DunGen systems. It holds information about the connection it represents and, most importantly, its current state.
Key Properties & Information
The Door component provides the following useful information (accessible via code):
IsOpen(bool): This is the most important property. It represents the logical state of the door (open or closed). You control this property from your custom door scripts (see below). It directly influences visibility for culling and traversability for pathfinding. Default Value:true(open).DontCullBehind(bool): If true, the culling system (if any) will not cull tiles behind this door, even when closed.DoorwayA(Doorway): A reference to the Doorway component on the first tile of the connection.DoorwayB(Doorway): A reference to the Doorway component on the second tile of the connection.TileA(Tile): A reference to the Tile component of the first tile.TileB(Tile): A reference to the Tile component of the second tile.
Interaction with Other Systems
The state of the Door.IsOpen property is crucial for integrating doors with other DunGen features:
- Culling: DunGen's built-in Adjacent Room Culling systems check the
IsOpenstate. IfCull Behind Closed Doorsis enabled on the culling component, tiles beyond a door whereIsOpenisfalsewill not be rendered. SettingIsOpentotrueallows the culling system to potentially "see" through the doorway. - Pathfinding: Supported Pathfinding integrations (like Unity NavMesh Components and A* Pathfinding Project Pro) use the
IsOpenstate to determine traversability.- For Unity NavMesh, NavMeshLinks generated across the doorway are typically enabled when
IsOpenistrueand disabled whenfalse. - For A* Pathfinding Project Pro, graph connection rules or tag updates often depend on
IsOpen, making areas beyond closed doors unwalkable.
- For Unity NavMesh, NavMeshLinks generated across the doorway are typically enabled when
Creating Custom Doors
If you create your own custom door prefabs (e.g., with animations for opening and closing), it is essential that your custom door script correctly manages the Door.IsOpen property.
Update IsOpen State
Your custom script (e.g., MyAnimatedDoor.cs) must get a reference to the Door component on the same GameObject and set the IsOpen property whenever the door's visual state changes.
using UnityEngine;
using DunGen;
[RequireComponent(typeof(Door))] // Ensure the Door component exists
public class MyAnimatedDoor : MonoBehaviour
{
private Door dunGenDoor;
private Animator doorAnimator; // Example
void Awake()
{
// Get the component added by DunGen
dunGenDoor = GetComponent<Door>();
doorAnimator = GetComponent<Animator>(); // Example
}
// Call this when your door visually opens
public void OpenDoor()
{
// Play animation, sound, etc.
// doorAnimator.SetTrigger("Open"); // Example
// CRITICAL: Update the DunGen Door component state
if (dunGenDoor != null)
{
dunGenDoor.IsOpen = true;
}
}
// Call this when your door visually closes
public void CloseDoor()
{
// Play animation, sound, etc.
// doorAnimator.SetTrigger("Close"); // Example
// CRITICAL: Update the DunGen Door component state
if (dunGenDoor != null)
{
dunGenDoor.IsOpen = false;
}
}
// Example: Simple interaction
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player")) // Example tag
{
OpenDoor();
}
}
}
By correctly updating Door.IsOpen, you ensure that DunGen's culling and pathfinding systems accurately reflect the current state of your custom doors, leading to correct visibility and navigation behaviour in your generated dungeons.