Search Unity

Players being "Perma-hit"

Discussion in 'Multiplayer' started by Mech707, Nov 17, 2014.

  1. Mech707

    Mech707

    Joined:
    Jun 13, 2013
    Posts:
    2
    SOLVED!

    Hey Unity Forums,

    I posted this on unity answers, but it was rejected for some stupid reason. Maybe you guys could help me out?

    I'm working on an online-deathmatch type game and have an issue where players are getting hit, and don't stop getting hit.

    What that means is, when a player is hit by another ONCE, the animations of him getting hit (camera shake, etc.) play and his health goes down, but these actions don't stop. His health still keeps going down even when he's no-longer getting hit.

    This issue only happens when there are 3 or more players connected, so I'm pretty sure it's an issue with networking.

    Here's the code for the player's health box getting hit:

    Code (CSharp):
    1. [RPC] public void getHit(float amount){
    2.         if (offlineMode == false){
    3.             log ("I'm Hit!");
    4.             if (health){
    5.                 health.loseHealth (amount);
    6.             }
    7.             if (networkView.isMine == false){
    8.                 networkView.RPC ("getHit", RPCMode.OthersBuffered, amount);
    9.             }
    10.         }else{
    11.             log ("I'm Hit!");
    12.             health.loseHealth (amount);
    13.         }
    14.     }
    and here's the code for the player's weapon telling the hitbox to get hit:

    Code (CSharp):
    1. voidOnTriggerEnter(Collider other){
    2.      if(other.tag =="PlayerTargetArea"){
    3.           Debug.Log("HIT SOMETHING!");
    4.           other.GetComponent<TargetAreaScript>().getHit (damage);
    5.      }
    6. }


    Anyone know what's up?

    Thanks!
     
    Last edited: Nov 17, 2014
  2. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    456
    You're recursively calling RPCs on every client. Take a look at line 8. If the network view doesn't belong to the object that this RPC is being invoked on then it sends out a gethit to every other client, buffered no less, causing every object that exists to recusively send out RPCs to everyone but itself, and they all send several back, until everyone is dead.

    Quite a bloody piece of code.
     
    Last edited: Nov 17, 2014
    Mech707 likes this.
  3. Mech707

    Mech707

    Joined:
    Jun 13, 2013
    Posts:
    2
    Wow! that explains why it wasn't happening with only two players in game. If a third player enters the game, then that player plus the attacking player will RPC to eachother infinitly, causing this Perma-Hit. Thank you so much Glader!

    I tried this fix. It seems to be working now:
    Code (CSharp):
    1. public void getHit(float amount){
    2.         if (offlineMode){
    3.             log ("I'm Hit!");
    4.             health.loseHealth (amount);
    5.         }else{
    6.             networkView.RPC ("getHitRpc", RPCMode.All, amount);
    7.         }
    8.     }
    9.  
    10.     [RPC] public void getHitRpc(float amount){
    11.         if (offlineMode == false){
    12.             log ("I'm Hit!");
    13.             if (health){
    14.                 health.loseHealth (amount);
    15.             }
    16.             if (networkView.isMine == false){
    17.                 //networkView.RPC ("getHit", RPCMode.OthersBuffered, amount);
    18.             }
    19.         }else{
    20.             log ("I'm Hit!");
    21.             health.loseHealth (amount);
    22.         }
    23.     }
     
  4. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    456
    That looks fine but I'm not sure you need to check if you're in offline mode in the getHitRPC method anymore. If you only call that RPC over the network when you are online, which is checked in getHit, you can probably drop that check especially since the code seems the same either way.

    Regardless, I'm glad I was able to help stop the recursive bloodbath that function was causing your players and it seems fixed now to me too.
     
    Mech707 likes this.