Stopping Players from Damaging Eachother?

Hello, I wanted to know how to stop players from being able to shoot each other. I don't want to make the player immortal or anything as Im trying to make a simple coop thing.
 

Justin

Administrator
Staff member
You could implement friendly fire by subclassing the CharacterHeatlh component and then override the OnDamage method.
 

Andrew

Moderator
Staff member
A quick example:

C#:
public class FriendlyFireCharacterHealth : CharacterHealth
{
    ...
    
    public override void OnDamage(float amount, Vector3 position, Vector3 direction, float forceMagnitude, int frames, float radius, GameObject attacker, object attackerObject, Collider hitCollider) {
        if (attacker.CompareTag("Player")) {
            return;
        }
        
        base.OnDamage(amount, position, direction, forceMagnitude, frames, radius, attacker, attackerObject, hitCollider);
    }
}

This would ignore all damage dealt by any attacker with the "Player" tag.
 
Top