Networked OnDeath event

jackmerrill

New member
Hi there! I'm using the Opsive EventHandler and registering `OnDeath` to an event. However, the Death event doesn't seem to run when a player is killed by another player.

Code for registering:
C#:
EventHandler.RegisterEvent<Vector3, Vector3, GameObject>(gameObject, "OnDeath", OnDeath);

OnDeath method:
C#:
private void OnDeath(Vector3 pos, Vector3 force, GameObject attacker)
{
    Debug.Log("Death");
    if (attacker != null)
    {
        PhotonView attackerView = attacker.GetComponent<PhotonView>();
        Debug.Log("Attacker " + attackerView.Owner.NickName + " killed " + PhotonNetwork.LocalPlayer.NickName);
        onDeath.Invoke(attackerView);
    }
}

Nothing is logged, nothing happens.

What do I do? Am I doing something wrong?

Thanks!
 
The OnDeath event isn't sent for the remote players. You could add an RPC though to the local player that gets sent to all of the others.
 
I finally got it to work!

Here's what I ended up doing:

C#:
using UnityEngine;
using Opsive.UltimateCharacterController.Traits;
using Photon.Pun;
using Photon.Realtime;

public class PunClient : MonoBehaviour
{
    public Health health;

    private PhotonView photon;

    private void Start()
    {
        photon = PhotonView.Get(this);
        health = GetComponent<Health>();

        health.OnDeathEvent.AddListener((Vector3 pos, Vector3 force, GameObject attacker) =>
        {
            PhotonView attackerView = attacker.GetComponent<PhotonView>();
            photon.RPC("OnDeathSync", RpcTarget.All, attackerView.ViewID);
        });
    }

    [PunRPC]
    public void OnDeathSync(int attackerViewId, PhotonMessageInfo info)
    {
        Debug.Log("Death");
        PhotonView attackerView = PhotonView.Find(attackerViewId);
        Player attacker = attackerView.Owner;
        Debug.Log("Player " + info.Sender.NickName + " was killed by " + attacker.NickName);
        gameMode.onDeath.Invoke(attackerView);
    }
}
 
Top