Search Unity

Third Party [PUN] RPC Issue

Discussion in 'Multiplayer' started by JannickL, Oct 10, 2016.

  1. JannickL

    JannickL

    Joined:
    May 18, 2016
    Posts:
    78
    Hey guys,
    i've got an issue with a rpc call. I want to lower the health of another player when he gets hit by it the gameobject has this script attached.
    The issue is that the gameobjects collides two or three times with the other player and so there player gets to much damage. There should just be one collision and the object should get destroyed.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SkillScript : Photon.MonoBehaviour
    5. {
    6. public int skilldmg;
    7. private GameObject target;
    8.  
    9. void Start()
    10. {
    11. skilldmg = 60;
    12. }
    13.  
    14. void OnCollisionEnter(Collision collision)
    15. {
    16. if (collision.collider.tag == "PlayerHead") // Enemys networked head
    17. {
    18. target = collision.collider.gameObject;
    19. PhotonView photonView = PhotonView.Get(this);
    20. photonView.RPC("LowerHp", PhotonTargets.All); // Call to RPC
    21. PhotonNetwork.Destroy(gameObject); // Destroy this GO
    22. }
    23. }
    24.  
    25. [PunRPC]
    26. public void LowerHp()
    27. {
    28. target.GetComponent().curHp -= skilldmg; // Enemy get damage
    29.  
    30. if (target.GetComponent().curHp <= skilldmg)
    31. {
    32. if (photonView.isMine)
    33. {
    34. GameObject.FindGameObjectWithTag("Player").GetComponent<LocalScript>().myHead.GetComponent().gold += 50; // Player get gold
    35. }
    36. }
    37. }
    38. }

    Here is the Script to sync transform:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NetMovement : Photon.MonoBehaviour {
    5.  
    6. private Vector3 correctPos;
    7. private Quaternion correctRot;
    8.  
    9. void Start()
    10. {
    11. PhotonView photonView = GetComponent();
    12. }
    13.  
    14. void Update()
    15. {
    16. if (!photonView.isMine)
    17. {
    18. transform.position = Vector3.Lerp(transform.position, this.correctPos, Time.deltaTime * 100); // Maybe to much?
    19. transform.rotation = Quaternion.Lerp(transform.rotation, this.correctRot, Time.deltaTime * 5);
    20. }
    21. }
    22.  
    23. void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    24. {
    25. if (stream.isWriting)
    26. {
    27. stream.SendNext(transform.position);
    28. stream.SendNext(transform.rotation);
    29. }
    30. else
    31. {
    32. this.correctPos = (Vector3)stream.ReceiveNext();
    33. this.correctRot = (Quaternion)stream.ReceiveNext();
    34. }
    35. }
    36. }
    Thank you guys!
     
  2. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    I't hard to understand you..... give us more infos.
     
  3. Mrslayer01

    Mrslayer01

    Joined:
    Mar 23, 2014
    Posts:
    30
    Sounds like what you need is a cool down or wrap it in a bool like this:

    Code (CSharp):
    1. void OnCollisionEnter(Collision collision)
    2. {
    3. if (collision.collider.tag == "PlayerHead") // Enemys networked head
    4. {
    5. target = collision.collider.gameObject;
    6. PhotonView photonView = PhotonView.Get(this);
    7. if (CanHit) {
    8. photonView.RPC("LowerHp", PhotonTargets.All); // Call to RPC
    9. HitTimer += 0.5f;
    10. }
    11. PhotonNetwork.Destroy(gameObject); // Destroy this GO
    12. }
    13. }
    and in the update method it would be something like this:

    Code (CSharp):
    1.        
    2.  if(HitTimer> 0) {
    3.             CanHit = false;
    4.             HitTimer -= Time.deltaTime;
    5.         } else if (HitTimer<= 0) {
    6.             HitTimer= 0;
    7.             CanHit= true;
    8.         }
    It seems like it's hitting a bunch of times maybe due to lagg, but this should be a good step to fixing it.