Sphere Collider and "OnTriggerEnter"

Good afternoon, team!

I have two characters (one is the player, the other is an undead monstrosity that must be destroyed!!). Attached to the front of each character is a sphere collider of radius 1, offset by a z of 1. Each has “isTrigger” checked.

When an object enters the trigger (using OnTriggerEnter), I check to the see if the other (Collider other) is on a different team (assigned in a variable). If it is, then I want to make the object entering my collider “vulnerable”, which is just a bool at this point (so I’m setting that to true).

The problem is that if the Combat script that is checking for OnTriggerEnter is attached to both characters, BOTH are marked as vulnerable.

If approaching each other from the front, this is fine, because they should enter each other’s colliders at the same time. If one (say, the player) is approaching the other (the undead monstrosity) from the rear, however, only the undead monstrosity should be marked vulnerable.

Basically, since they are both using the same script, and both checking “OnTriggerEnter”, as soon as either enters the other, then they both count as entering.

It seems that if a trigger is entered, by either party, the “OnTriggerEnter” conditions are fulfilled, but I only want the script to fire if the character is entering another character’s sphere collider, and NOT if the character’s own sphere collider is not entered.

Any ideas? If you need more clarification, I can try to explain more clearly!

Here’s my script:

void OnTriggerEnter (Collider other)
{

if (other.gameObject.GetComponent () != null) {
CombatState myOtherCombatState = other.gameObject.GetComponent ();
string myOtherTeam = myOtherCombatState.myTeam;

if (myTeam != myOtherTeam) {
myOtherCombatState.isVulnerable = true;
}
}
}

Im not exactly sure what you are asking but maybe use tags to differentiate between characters. It would probably simplify things a lot if you create two separate scripts. one for the player and one for npc.

if(other.tag != “whatever tag”)
{

}

I will probably need to do that; some investigation reveals that “OnTriggerEnter” sends a message to both the object holding the trigger AND the object encountering the trigger. My thought process was that if I made one universal combat script, then the “skeletons” team could fight all members of the “royal” team, and if I introduced a third party, the “gremlins” team, they would fight against both sides. I s’pose making a new version of the script for each faction would both solve my problem and also wouldn’t really be that much more overhead.

if(myOtherTeam != “skeletons”){

}

And then for the royal family, in a copy+paste script, just change this to:

if(myOtherTeam != “royal”){

}

Thanks. I’ll post my fixed code if and when I get it working!

~~ chainsaw ~~