I do it via damage for the asset I am working on but this can be done the same with on death. I only ever run the check on master, so it got a little tricky. Here you go for reference... This is messy but works flawlessly and can also give you the attacking weapon.
C#:
// if we get here then target was a player that got killed, so
// see if we know about the damage source
Transform sourceTransform = null;
GameObject attackingObject = null;
IDamageSource source = damageData.DamageSource;
var ctx = damageData.ImpactContext;
float damage = damageData.Amount;
if (source != null)
{
attackingObject = source.SourceGameObject;
// If the originator is an item then more data needs to be sent.
if (source is CharacterItemAction)
{
var itemAction = source as CharacterItemAction;
sourceTransform = itemAction.CharacterTransform;
if (attackingObject = null)
attackingObject = itemAction.GameObject;
//TODO: Add stats
}
if (sourceTransform == null && source.TryGetCharacterLocomotion(out CharacterLocomotion characterLocomotion))
sourceTransform = characterLocomotion.transform;
//Debug.Log("GOT FROM SOURCE: " + (sourceTransform ? sourceTransform : "NULL"));
//MPDebug.Log("GOT FROM SOURCE: " + (sourceTransform ? sourceTransform : "NULL"));
}
if (sourceTransform == null || attackingObject == null && ctx.ImpactCollisionData != null)
{
if (attackingObject && ctx.CharacterItemAction != null)
{
if (attackingObject == null)
attackingObject = ctx.CharacterItemAction.GameObject;
//TODO: Add stats
}
if (sourceTransform == null && ctx.ImpactCollisionData.SourceCharacterLocomotion)
{
sourceTransform = ctx.ImpactCollisionData.SourceCharacterLocomotion.transform;
}
if (sourceTransform == null || attackingObject == null && ctx.ImpactCollisionData.SourceGameObject != null)
{
if (sourceTransform == null)
sourceTransform = ctx.ImpactCollisionData.SourceGameObject.transform;
if (attackingObject == null)
attackingObject = ctx.ImpactCollisionData.SourceGameObject;
}
// Debug.Log("GOT ALTERNATIVELY: " + (sourceTransform? sourceTransform:"NULL"));
// MPDebug.Log("GOT ALTERNATIVELY: " + (sourceTransform ? sourceTransform : "NULL"));
}
This also drives the kill feed with some other code that follows it. A polished version of kill feeds etc will all be in my asset when released