Help syncing Health
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Help syncing Health
Shelvid
2017-12-31 15:33:11
So i am working on FPS game. When i first made it (i followed tutorial on Photon website) it was working well. I had projectile firing and after it hit other player that player died no problem. I decided to add Raycast shooting rather than projectile however i have this huge issue... Health doesn't seem to be syncing and whenever lets say Player1 kills Player2, Player1 leaves the game instead of Player2... here are codes:
Shooting:
[PunRPC]
void Fire()
{
RaycastHit hit;
Debug.DrawRay (fpsCam.transform.position, fpsCam.transform.forward, Color.green);
if (Physics.Raycast (fpsCam.transform.position, fpsCam.transform.forward, out hit)) {
Debug.Log (hit.transform.name);
PlayerManager1 target = hit.transform.GetComponent ();
if (target != null) {
target.TakeDamage (damage);
}
}
}</code></pre>
TakeDamage:
[PunRPC]
public void TakeDamage (float amount)
{
Health -= amount;
if (Health <= 0f)
{
Die();
}
}
Death:
[PunRPC]
void Die ()
{
GameManager.Instance.LeaveRoom();
}
Please help. I am beginner in Photon and I'm kind of lost because there aren't too much tutorials out there :)
Comments
Hi,
Typically, you must not have each client check for damage, only the owner or only the masterClient, else you will run into race conditions and most likely differences in behavior due to the latency of the network, which is something unavoidable.
I strongly advice you check the basic tutorial which will show you one secure way to check for damage.
https://doc.photonengine.com/en-us/pun/current/demos-and-tutorials/pun-basics-tutorial/player-networking
Make sure you go through the whole tutorial if it's not clear, this is a step by step tutorial. Let me know if you have questions after you went through it.
Bye,
Jean
Back to top