Help syncing Health

Options
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<PlayerManager1> ();

			if (target != null) {
				target.TakeDamage (damage);
			}
		}
	}
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 :)

Answers

  • jeanfabre
    Options
    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