- Home /
Photon Unity Player Kill Counter
So I'm making a multiplayer FPS, and in the game, I need to track player kills. I just need a way to store a player stat that can be changed over the network. I looked at RPCs, but due to me being a beginner at Photon, I can't seem to find out how to use them effectively.
Answer by ChristianSimon · Apr 03, 2018 at 09:20 AM
Hi,
you can take a look at the PunPlayerScores class from the PUN package. This one demonstrates a simple score system which works by using the Custom Player Properties. You can either use this one or use the class as a pattern to implement your own functionality similar to this example.
Answer by rbosse · Feb 21, 2019 at 04:38 AM
Try using my method. It works well... at least in my situation. You don't need to do RPC's for this. On my Projectile script:
private void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Player")
{
other.gameObject.GetComponent<PlayerShipSystems>().currentHull -= hullDamage;
other.gameObject.GetComponent<PlayerShipSystems>().currentArmor -= armorDamage;
other.gameObject.GetComponent<PlayerShipSystems>().currentShield -= shieldDamage;
if (other.gameObject.GetComponent<PlayerShipSystems>().currentHull <= 0)
{
ApplyKillToOwner();
}
ApplyScoreToOwner();
}
else if (other.gameObject.tag == "AI")
{
ApplyScoreToOwner();
}
}
private void ApplyScoreToOwner()
{
if (PV.IsMine)
{
//TODO: Figure out NULL reference exception
Hashtable hash = new Hashtable();
scoreOfParent = Convert.ToInt32(GetComponent<PhotonView>().Owner.CustomProperties["Score"]);
hash.Add("Score", scoreOfParent + scorePerHit);
PV.Owner.SetCustomProperties(hash);
}
this.gameObject.GetComponentInChildren<MeshRenderer>().enabled = false;
this.gameObject.GetComponent<BoxCollider>().enabled = false;
}
private void ApplyKillToOwner()
{
if (PV.IsMine)
{
Hashtable hash = new Hashtable();
killsofParent = Convert.ToInt32(GetComponent<PhotonView>().Owner.CustomProperties["Kills"]);
hash.Add("Kills", killsofParent + 1);
PV.Owner.SetCustomProperties(hash);
}
}