- Home /
Photon RPC called twice
Hello! I have a really strange issue - I simply try to call TakeDamage method with a rpc photon for unity, but it gets called two times( checked for three clients- also two times) So If Player get hit : 1. Collision is detected 2. TakeDamage --> Player loses hp 3. TakeDamage AGAIN - and player loses hp.
What could cause it?
void OnTriggerEnter(Collider coll){
Debug.Log ("COLL! : " + coll.name);
if(coll.CompareTag(Consts.TagBullet)){
// DealDamage (2f, ctPhotonView.viewID);
PhotonView photonView = this.photonView;
photonView.RPC ("DealDamage", PhotonTargets.All, coll.GetComponent<BulletInitializer>().oBullet.Power,ctPhotonView.viewID);
}
}
[PunRPC]
public void DealDamage(float power, int playerId){
health-=power;
Debug.Log ("Hp : " + health + " player : " + name);
}
Answer by tobiass · May 09, 2016 at 10:19 AM
Both of your players simulate the world. In best case, that means that both simulate the collision. If OnTriggerEnter is called on both clients, then both will send the RPC and execute it locally, too.
Make sure only one client is entitled to send the damage RPC. Maybe it's the hit one or maybe it's the one who shot. Both options have pros and cons.
Yep, really thanks for writing this. It helped me to look at my code from different point of view and I discovered what a silly mistake I made... Simply adding "if ctPhoton.is$$anonymous$$ine" in OnTriggerEnter helped. That was one of this mistakes which just happen from time to time and when you realize how obvious that was you cant believe you didn't see that...
ah so this is the reason a bug in my project was occuring. Thank you so muuch @tobiass , your comment has saved my project!