Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Designating terrain regions

Discussion in 'Editor & General Support' started by amherst, Mar 31, 2009.

  1. amherst

    amherst

    Joined:
    Dec 9, 2008
    Posts:
    30
    Does anyone know if there's a way in Unity to designate a region of a terrain as separate from other regions? I'm trying to create game play such that certain scripts will only run in particular regions of the terrain. I could write some hack to make this work but it would be so much better if there were already built in some sort of region functionality.

    Any advice would be great.

    Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,145
    Sounds like something triggers would be good for.

    --Eric
     
  3. amherst

    amherst

    Joined:
    Dec 9, 2008
    Posts:
    30
    Thanks for the suggestion. I haven't used triggers at all but I understand that they're basically a bit flag attached to a collider. Can you give any more hints on how I could use this with terrain?

    Thanks!
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    I'd suggest making a box collider as big as you need it to be. Attach a script to it that goes something like this:

    Code (csharp):
    1. // TriggerController.js
    2. // A script to indicate when the ship is in the trigger
    3.  
    4. private var zoneController : ZoneController;
    5.  
    6. function Start () {
    7. //   Debug.Log("TriggerController.js starting");
    8.    zoneController = transform.parent.GetComponentInChildren(ZoneController);
    9.    if (!lzController)
    10.       Debug.Log("TriggerController.js: No link to Zone Controller");
    11. }
    12.  
    13.  
    14. function OnTriggerEnter (other : Collider) {
    15.    if (other.tag == "Ship") {
    16.       zoneController.isInTrigger = true;
    17. //      Debug.Log("Ship = isIn Trigger");
    18.    }
    19. }
    20.  
    21. function OnTriggerExit (other : Collider) {
    22.    if (other.tag == "Ship") {
    23.       zoneController.isInTrigger = false;
    24. //      Debug.Log("Ship = NOT isIn Trigger");
    25.    }
    26. }
    This was yanked out of context, so it won't be perfect.

    You'd then use the trigger value for other functions.

    In your case rather than just ...isInTrigger = ... you'd need a different unique one for each trigger being sent to the trigger controller (which is another script)...
     
  5. amherst

    amherst

    Joined:
    Dec 9, 2008
    Posts:
    30
    Thanks a lot!

    But what would I attach the box collider to? Not the terrain I assume since it already has a Terrain Collider. Do I create an unrendered cube or something to go in the center of the region and attach the box collider to it?
     
  6. Sakar

    Sakar

    Joined:
    Feb 12, 2009
    Posts:
    141
    Add the box collider to an empty GameObject and place it in the center of the region.
     
  7. amherst

    amherst

    Joined:
    Dec 9, 2008
    Posts:
    30
    Got it working. Thanks a lot to all of you!