TerrainBoundary - jimdroberts/FishMMO GitHub Wiki

Description

TerrainBoundary is a MonoBehaviour for FishMMO that defines terrain boundaries in the game world. It draws gizmos for visualization in the Unity editor and provides boundary size and offset based on terrain data, enabling clear and interactive management of terrain-based boundaries for gameplay logic.


API Access

Fields

  • [Header] public Vector3 BoundaryOffset

    Additional offset to apply to the terrain boundary. Boundaries are inclusive; if a player is not within it, it will not apply.

  • [ShowReadonly] public Vector3 TerrainSize

    The size of the terrain, read from the terrain data. Marked readonly in the inspector.

  • [ShowReadonly] public Vector3 TerrainOffset

    The offset of the terrain center, read from the terrain data. Marked readonly in the inspector.

Methods

  • public void OnDrawGizmos()

    Draws a wireframe cube gizmo in the editor to visualize the terrain boundary.

  • public void OnDrawGizmosSelected()

    Draws a solid cube gizmo in the editor when the terrain boundary is selected. Updates terrain size and offset from terrain data.

  • public override Vector3 GetBoundaryOffset()

    Gets the offset of the terrain boundary, calculated from the object's position, terrain offset, and vertical boundary offset. Returns: Vector3. The offset vector for the terrain boundary. No exceptions are thrown.

  • public override Vector3 GetBoundarySize()

    Gets the size of the terrain boundary, calculated from terrain size and additional boundary offset. Returns: Vector3. The size vector of the terrain boundary. No exceptions are thrown.


Basic Usage

Setup

  1. Attach the TerrainBoundary component to a GameObject with a Terrain component in your scene.
  2. Adjust the BoundaryOffset field as needed to expand or contract the boundary area.
  3. Use the provided methods to retrieve boundary size and offset in your gameplay logic.

Example

// Example 1: Adding a terrain boundary in the Unity Editor
// 1. Add the TerrainBoundary component to a GameObject with a Terrain.
// 2. Adjust BoundaryOffset in the inspector.
// 3. The boundary will be visualized with gizmos in the scene view.

// Example 2: Accessing boundary data in code
Vector3 offset = terrainBoundary.GetBoundaryOffset();
Vector3 size = terrainBoundary.GetBoundarySize();

Best Practices

  • Use this component to clearly define and visualize terrain boundaries for gameplay logic.
  • Adjust BoundaryOffset to fine-tune the playable area around the terrain.
  • Document the purpose and configuration of each terrain boundary for maintainability.
⚠️ **GitHub.com Fallback** ⚠️