Search Unity

local triggers in network game

Discussion in 'Scripting' started by Tom163, Dec 8, 2007.

  1. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I want triggers that work locally in a network game, i.e. for each player on his own machine only.

    Here's my current code, this script is attached to all the objects I want to trigger:
    Code (csharp):
    1.  
    2.  
    3. function Awake() {
    4.     renderer.enabled = false; // start out invisible
    5. }
    6.  
    7. function OnTriggerEnter (other : Collider) {
    8.     renderer.enabled = true; // make the object visible
    9. }
    10.  
    11.  
    12. function OnTriggerExit (other : Collider) {
    13.     renderer.enabled = false; // make the object invisible
    14. }
    15.  
    I have a collider around my objects that is larger than the object itself and set to trigger. So when a player comes close to the object, it becomes visible, when he walks away again, it disappears (I also have a nice fade effect, but I left that out here).

    But my problem is that this makes the object visible to player A when player B comes close to the object. And that's not what I want. The object should only be visible to whatever player triggered it.


    So what's the magic bit that I'm missing here?
     
  2. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    Sorry to post with no answer.

    But I am interested in this too.

    For instance, when you are under water, turning on a dark, dense fog makes a pretty convincing looking water. But then everyone outside on the land, will suddenly be in a dense, dark fog.

    Would be interesting to be able to assign a few things to a specific camera or player.

    =my two cents.
     
  3. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    What you want to do is use the information that is sent to your Event handlers to find out who collided with your trigger/collider...

    Code (csharp):
    1.  
    2. function OnTriggerEnter(other : Collider)
    3. {
    4.     if (other.gameObject.name == "MyDude")
    5.     {
    6.         DoStuff();
    7.     }
    8. }
    9.  
    Here's all the stuff you can access from the Collider:
    http://unity3d.com/support/documentation/ScriptReference/Collider.html

    -Jeremy
     
  4. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    On a networked game you would also add a check to make sure the object is yours. If all the objects are the same on all players (instead of each player having a different game object entirely).

    Code (csharp):
    1.  
    2. function OnTriggerEnter(other : Collider)
    3. {
    4.     if (other.gameObject.name == "MyDude"  other.networkView.isMine)
    5.     {
    6.         DoStuff();
    7.     }
    8. }
    9.  
    -Jeremy
     
  5. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Thanks, this helps.

    Is there a better way than to test for the name? Like testing if it has a character controller attached or something?
     
  6. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I figured out a great solution to this - using tags, it's very simple:

    Code (csharp):
    1.  
    2. if (other.networkView.isMine  other.gameObject.tag == "Player") {
    3.    // do something
    4. }
    5.