Search Unity

Info triggers

Discussion in 'Scripting' started by gabrielh, Apr 22, 2008.

  1. gabrielh

    gabrielh

    Joined:
    Feb 24, 2008
    Posts:
    51
    Hi!

    I want to make info triggers in my game level, which means that I place (empty, invisible) objects in the level and if the players runs into a circular distance of this (say 10m) a info GUI pops up. Later I will read these info hotspots as 3D positions from a DB and create them at start-up.
    The question I have is which kind of object should I use for the task. The requirements are:
    - instantiatable at run-time
    - collision detection

    I rather have to much of a choice here (I guess it could be GameObject, Gizmo, etc.). What's the "clean" way of doing it?

    Thanks!
    Gabriel
     
  2. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    i'd make a cylinder in you 3d app at the size you want (max distance you want to trigger at) - make sure the pivot is set to the base of the object, or wherever. then you don't need to worry about offsets or whatever at instantiation time! bring it into unity apply a mesh collider to it, make that collider a trigger. then remove the mesh renderer and mesh filter. then turn that into a prefab. put that prefab into a "Resources" directory in your assets folder. that is what you will load at runtime to place at you 3d points in your world which you are grabbing from your DB. Use "Resources.Load ("triggerName") to grab the prefab.

    I'd put a script on the trigger object that checks against a certain Tag type to make sure the triggering object is the player or whoever. That script reports to the Game Manager that it has been triggered and which trigger it was based off it's object name. Which you set when you instantiate it. The GM checks against a hashtable or something using that name as the key. display the gui popup and the required info.

    No doubt there are other ways of doing it! :wink:

    Cheers.
     
  3. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    You could create an empty GameObject something like this:




    Code (csharp):
    1. var playerMessage = "";
    2. var triggers:Array = new Array();
    3. function Start(){
    4.     // triggers is multi-dimensional, it contains a vector3 for location, a radius, and a message.
    5.     triggers = [    [Vector3(10, 0, 0), 5, "Alert One"],
    6.                     [Vector3(20, 0, 0), 10, "Alert Two"]
    7.     ];
    8.    
    9. }
    10.  
    11. function Update () {
    12.     var localMessage = "";
    13.     for (var i = 0; i<triggers.length; i++){
    14.         var things : Collider[] = Physics.OverlapSphere(triggers[i][0], triggers[i][1]);
    15.         for (var j = 0; j < things.length; j++){
    16.             if(things[j].name == "Player"){
    17.                 localMessage = triggers[i][2];
    18.             }
    19.         }
    20.     }
    21.     playerMessage = localMessage;
    22.     /* //Debug Code
    23.     if (playerMessage != ""){
    24.         Debug.Log(playerMessage);  
    25.     }*/
    26. }
    Your info triggers are set as an array, and the public variable "playerMessage" is always either empty or contains the current info... it could be taken by some script that displays text and updates a labels.