Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiplayer OnTriggerEnter ServerSide RPC call

Discussion in 'Multiplayer' started by blue2fox, Feb 27, 2009.

  1. blue2fox

    blue2fox

    Joined:
    Jun 8, 2008
    Posts:
    91
    I'm working on a game with AI Bots that are controlled from the server. The Bots seems to be working ok in multiplayer, except for an attack script that I have. When ever the trigger collides with the client, it is suppose to reduce the clients health. The problem is that the script seems to only work when the Server's character is watching the attack, or when the clients character is moving. Please take a look at the script bellow and post your thoughts. Thank you.


    Code (csharp):
    1.  
    2. function OnTriggerEnter (other : Collider)
    3. {
    4.     if (other.tag == "Chaos" || other.tag == "Order") // the Two different sides in the game
    5.     {  
    6.         if (Network.isServer  attack)
    7.         {          
    8.             if (other.networkView.isMine)
    9.             {
    10.                 if (other.name != "Shield_Collider")
    11.                 {
    12.                     other.SendMessage("TakeDamage", damage);
    13.                 }
    14.                 else if (other.name == "Shield_Collider")
    15.                 {
    16.                     other.SendMessage("ShieldDamage", damage);
    17.                 }
    18.             }
    19.             else if (!other.networkView.isMine)
    20.             {
    21.                 var otherPlayer = other.networkView.owner;
    22.                
    23.                 if (other.name != "Shield_Collider")
    24.                 {
    25.                     other.networkView.RPC("TakeDamage", otherPlayer, damage);
    26.                 }
    27.                 else if (other.name == "Shield_Collider")
    28.                 {
    29.                     other.networkView.RPC("ShieldDamage", otherPlayer, damage);
    30.                 }
    31.             }                              
    32.             attack = false;
    33.         }              
    34.     }
    35. }
    36.  
    37.  
     
  2. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    I guess whats causing this, is that you call the RPC function only on the affected client. If you want your server to know that the client has taken damage, then you have to add
    Code (csharp):
    1. other.SendMessage("TakeDamage", damage)
    there as well.
     
  3. blue2fox

    blue2fox

    Joined:
    Jun 8, 2008
    Posts:
    91
    Thanks for the quick reply Der Dude. Unfortunately I'm trying to get the clients computer to show the damage that it's receiving. I have stored players health individually on their respective characters, locally, that's why I've sent the RPC call, "TakeDamage", to the client. However it doesn't update the client's computer, unless the host watches the Bot attack the client, or the client is moving.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    thats because your RPC call is incorrect by default.

    You put the ownerid in there, but there is defacto no ownerid within the RPC.
    You are meant to put the RPCMode there, like RCPMode.Others for example.

    If you want to send the owner id, then you need to send it as function parameter
     
  5. blue2fox

    blue2fox

    Joined:
    Jun 8, 2008
    Posts:
    91
    Thanks for the response Dreamora. Could you explain what you mean? I'm not really following. I read in one of the forums, http://forum.unity3d.com/viewtopic.php?t=14905&highlight=rpc+player , someone posted that we could send RPC calls to specific players. I was trying to re-create that. I thought that the networkView.owner, was the networkPlayer?
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Oh, you are right, thats a second possibility.
    Always forget about it as it is only of any use for very few things (Client <-> Server communication with specific information like your character when you login to a server that stores your data between sessions and alike)

    I don't see why you would only want to send it to a specific player only in this case. Especially not granted that we are talking about the server end who is the only one to send things to a single client.

    Also, all others must know about it, not only a specific player, otherwise only you and the other player know it and the rest has totally wrong data.
    Even worse, the server potentially would have wrong data which is a no go unless you want to invite cheaters (the server should normally always be the one deciding if a desired change is valid and if so promote it to the other connected clients)
    So you would send the related object id to all players from the "server player" so they can update it on their end as well.

    or are you trying achieving something pretty special out of the normal?
     
  7. blue2fox

    blue2fox

    Joined:
    Jun 8, 2008
    Posts:
    91
    Cool, cool. Yeah thanks for the tip Dreamora. I never thought about it that way.
     
  8. blue2fox

    blue2fox

    Joined:
    Jun 8, 2008
    Posts:
    91
    I finally got it to work. Thanks everyone. What I ended up doing was trimming down the code:


    Code (csharp):
    1.  
    2. function OnTriggerEnter (other : Collider)
    3. {
    4.     if (other.tag == "Chaos" || other.tag == "Order") // the Two different sides in the game
    5.     {  
    6.         if (attack)
    7.         {
    8.             if (other.name != "Shield_Collider")
    9.             {
    10.                 other.SendMessage("TakeDamage", damage);
    11.             }
    12.             else if (other.name == "Shield_Collider")
    13.             {
    14.                 other.SendMessage("ShieldDamage", damage);
    15.             }
    16.                                
    17.             attack = false;
    18.         }              
    19.     }
    20. }
    21.  
    So like Der Dude suggested I sent the "TakeDamage" script on the server and clients, locally. Then, like Dreamora suggested, I had a RPC script that sent to 'All' the players and returned the new health after taking Damage. Thanks again for all the input.
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Glad to hear its working
    Good luck and lots of fun with it :)