Search Unity

Third Party [Photon] Strange RPC calls

Discussion in 'Multiplayer' started by ArnaudM, Nov 29, 2021.

  1. ArnaudM

    ArnaudM

    Joined:
    Jan 24, 2013
    Posts:
    7
    Hi everyone

    I get a strange behaviour for (what I tought was) a very simple RPC implementation.
    • The project is a mini game with various players, each player has a gun which can shoot bullets.
    • Each player is a prefab instanciated by PhotonNetwork, and has a "NetworkPlayer" component.
    • Each bullet is also a prefab instanciated by PhotonNetwork, and has a "Bullet" component. It also has a collider. (The instanciation is done in a gun script, on a child of the player in its prefab)
    • I'm trying to make the bullet call a "TakeDamage" RPC method on the player touched.
    • Given the gun position, the bullet collides the shooting player first, so I want to make sure the RPC method only gets called on the desired player.
    My attempt works, but the problem is the shooting player also gets the damage.

    Here are some code extracts :
    The Bullet component :
    Code (CSharp):
    1. [RequireComponent(typeof(Collider))]
    2. public class Bullet : MonoBehaviourPun
    3. {
    4.     private void Awake()
    5.     {
    6.         if (!photonView.IsMine)
    7.             enabled = false;
    8.     }
    9.  
    10.     void OnCollisionEnter(Collision collision)
    11.     {
    12.         GameObject collidedObject = collision.collider.gameObject;
    13.  
    14.         if (collidedObject.CompareTag("Player"))
    15.         {
    16.             PhotonView touchedPlayer = PhotonView.Get(collidedObject);
    17.             Debug.Log("Collision : " + photonView.ViewID + " touched " + touchedPlayer.ViewID);
    18.          
    19.             if (touchedPlayer.IsMine)
    20.             {
    21.                 Debug.Log(touchedPlayer.ViewID + " is mine (I am " + photonView.ViewID + " / " + photonView.Owner.NickName + ") => aborting");
    22.                 return;
    23.             }
    24.          
    25.             Debug.Log("I am " + photonView.ViewID + " and I call the RPC method on " + touchedPlayer.ViewID);
    26.             touchedPlayer.RPC("TakeDamage", touchedPlayer.Owner, photonView.ViewID);
    27.         }
    28.     }
    29. }
    The RPC method from NetworkPlayer :
    Code (CSharp):
    1. [PunRPC]
    2. public void TakeDamage(int callerID)
    3. {
    4.    if (!photonView.IsMine)
    5.        return;
    6.  
    7.    Debug.Log("I am " + photonView.ViewID + ", RPC called by " + callerID);
    8.  
    9.    // Doing stuff ...
    10. }
    11.  
    I put Debug.Log everywhere to understand, but I really don't.

    Case 1 : Here is what I get when I shoot the other player :
    Ok, this is normal ...

    Case 2 : When I shoot anywhere else :
    How is that possible ?? Why does it enter the RPC method ?? The Debug.Log saying "I call the RPC method ..." doesn't appear so it seems to work fine, but the RPC method somehow gets called anyway !!

    Also :
    • Case 2 doesn't happen on all bullets. Some of them behave as expected (which is, not doing damage to the shooter).
    • When I shoot the other player I often get the combination of case 1 and case 2 : both players get the damage !
    Using :
    • Unity 2020.3.23f1
    • PUN 2.40
    Of course, searching RPC("TakeDamage" in the whole solution only finds this one line of code in Bullet.cs, and I tried clearing and refreshing RPCs list a couple of times already ...

    Am I missing something ?
    Thank you for your help !