Search Unity

All Players take damage at the same time

Discussion in 'Multiplayer' started by Moddwyn, Feb 18, 2017.

  1. Moddwyn

    Moddwyn

    Joined:
    Jan 27, 2016
    Posts:
    49
    Shooting Script:
    Code (CSharp):
    1. RaycastHit hit;
    2. Ray ray = myCamera.ScreenPointToRay (new Vector3 (Screen.width/2, Screen.height/2, 0));
    3.  
    4. if (Physics.Raycast (ray, out hit, gunRange)) {
    5.     if(hit.transform.tag == "Player" && hit.transform.GetComponent <Shooting>() != null){
    6.            hit.transform.GetComponent<PhotonView> ().RPC ("Damage", PhotonTargets.AllBuffered, damage);
    7.      }
    8. }

    Sync Script:
    Code (CSharp):
    1. [PunRPC]
    2. public void Damage(float dmg){
    3.     health -= dmg;
    4. }
    all players take damage when one player gets damaged, why?
     
  2. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    You probably need a way of identifying which player was dealt damage, by using some sort of NetID (not sure what Photon calls it) but in UNET you can get an object's unique ID by accessing its NetworkIdentity component. You can then use that ID to check if the object that received the RPC is that object, and if it wasn't you can just disregard the RPC (that's based on your current setup, in UNET you could just use a SyncVar to make it a bit simpler on yourself).
     
  3. Moddwyn

    Moddwyn

    Joined:
    Jan 27, 2016
    Posts:
    49
    that is what I did here,
    Code (CSharp):
    1.  hit.transform.GetComponent<PhotonView> ().RPC ("Damage", PhotonTargets.AllBuffered, damage);
    it gets the player photonview which is the networkidentity in unet, then calls the rpc on them, but the problem, it does it to all players.