Search Unity

Third Party Photon PunRPC Sync Health

Discussion in 'Multiplayer' started by TheBox101, Apr 6, 2019.

  1. TheBox101

    TheBox101

    Joined:
    Apr 29, 2018
    Posts:
    6
    I just implemented firing into my game with this code:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (!PV.IsMine)
    4.         {
    5.             return;
    6.         }
    7.         if (Input.GetMouseButtonDown(0) && !PauseMenu.GameIsPaused)
    8.         {
    9.             PV.RPC("RPC_Shooting", RpcTarget.All);
    10.         }
    11.         healthDisplay.text = avatarSetup.playerHealth.ToString();
    12.     }
    13.  
    14.     [PunRPC]
    15.     void RPC_Shooting()
    16.     {
    17.         RaycastHit hit;
    18.         if (Physics.Raycast(rayOrigin.position, rayOrigin.TransformDirection(Vector3.forward), out hit, 1000))
    19.         {
    20.             Debug.DrawRay(rayOrigin.position, rayOrigin.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
    21.             Debug.Log("Did Hit");
    22.             if (hit.transform.tag == "Avatar")
    23.             {
    24.                 hit.transform.gameObject.GetComponent<AvatarSetup>().playerHealth -= avatarSetup.playerDamage;
    25.             }
    26.         }
    27.         else
    28.         {
    29.             Debug.DrawRay(rayOrigin.position, rayOrigin.TransformDirection(Vector3.forward) * 1000, Color.white);
    30.             Debug.Log("Did not Hit");
    31.         }
    32.     }
    The strange part about this though is that when in the editor and shot at by a standalone the health updates correctly and is displayed on the healthDisplay object. But when I shoot at a standalone from my editor the health doesn't update there, but it does update for that player locally.
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    For one thing it looks like you are performing the hit test on every client and updating the health variable locally, so it's entirely possible for the hit to register on one client but miss on another client due to latency, which would produce the results you see.

    You should instead make a single player responsible for performing the hit test (usually (for PUN) either the player firing the shot or the master client), and then you could RPC the result of the test (hit/missed, hit point for impact particle display, etc. etc) to the other clients.

    Furthermore, you could make your playerHealth variable a CustomProperty and then you only need to update it on one client and it will automatically propagate the new value to the other clients, guaranteeing they are kept synchronised.

    Or maybe take a look at Photon Bolt instead, as it seems to be geared more towards FPS type games. (if that's what you're making).
     
    tobiass likes this.
  3. TheBox101

    TheBox101

    Joined:
    Apr 29, 2018
    Posts:
    6
    Thanks for the great advice! It works now.
     
    Munchy2007 likes this.