Friendly-fire damage PUN Addon

miroslaw88

New member
Hello, I was wondering what the best way to implement friendly-fire damage would be? At the moment I am subclassing "CharacterHealth" and overriding the "OnDamage" method:


C#:
public override void OnDamage(float amount, Vector3 position, Vector3 direction, float forceMagnitude, int frames, float radius, GameObject attacker, object attackerObject, Collider hitCollider)
    {
        //determine what team the "attacker" that shot me is on
        base.OnDamage(amount, position, direction, forceMagnitude, frames, radius, attacker, attackerObject, hitCollider);
    }

The issue is there isn't much to work with in terms of receiving "custom" data such as "what team is the player on that shot me". I originally was doing a attacker.GetComponent<MyPlayer>().team ("team" is just an int denoting what team a player is on) check, but the performance hit by doing that on each "OnDamage" call is not great.

Is there an easy way to extend what kind of information gets passed when attacking objects/players?
 
Last edited:
I added a "team" property to the Item baseclass which is set on OnEquip (based on the player's team). Then I am passing the team property around via the OnDamage calls. Looks like this works but I did have to modify the base code.
 
In order to not modify the code I would stick to an approach more similar to the first person. You can use attacker.GetCachedComponent instead of GetComponent and then you won't have the GetComponent lookup cost.
 
Top