Is it possible to combine child trigger of a object to only trigger OnTriggerEnter once?

Hello,
i want to hit monsters with a sword, the sword has a collider and i use the OnTriggerEnter to deal damage,
my monsters have different shapes and a default collider/trigger doesn’t fiit that’s why i use multiple trigger on the monsters to fill the monster shape.

But the problem is, because there a multiple triggers my sword OnTriggerEnter sometimes triggers twice in the same “swing”. For example let’s say a human like enemy which has a trigger on the head and body, if my sword would swing from the top to bottom it would trigger the had and body and get the OnTriggerEnter call twice.

Is there a way to combine the monster child triggers into a single trigger so that my sword slash in the example above would only trigger when i hit the enemy head and call trigger enter when i leave the body trigger?

Well, complex triggers have their disadvantages of course. There is no “easy” way to do this.

I’m going to guess a few things about your game. The player isn’t constantly attacking, and has to do something to initiate an attack. An enemy should only be hit once per attack by the sword, and there’s no spinning attacks that would hit them twice per swing. Lastly, all damageable entities descend from the same class inheritance.

If those assumptions are true, a simple thing to do would be whenever your sword hit an enemy, to deactivate its collision box after applying damage. This would prevent hitting an enemy twice with one swing. You could then reactivate the collision detection when you initiate a new swing.

But then lets say there’s 3 enemies in a row, and you want to be able to hit all of them with one swing. If you deactivate the sword collision on the first one, it’ll simply pass through the others, so that wont do here!

For every swing of a sword, you can store a reference to each entity hit. If you are using instances of an enemy script or such, you can do something like:

using System.Generic.Collections;

List<EnemyScript> hits;

void Swing ()
{
    hits = new List<EnemyScript> ();
    //Do your swing stuff here
}

void OnTriggerEnter (Collider other)
{
    EnemyScript script = other.GetComponent<EnemyScript>();
    //Depending on your set up, getting the reference may need to be done differently 
    if (script != null && hits.Exists(script)))
    {
        hits.Add(script);
        //ApplyDamage and such here
    }
}

This prevent the same instance of an enemy from being hit twice by any collider with that script, per Swing(). I didn’t test it, but it should work.

If you have different types of enemies, with different scripts, this can be done with something called inheritance. There’s a nice Unity guide Here that covers it, just make a new class “Damgeable” that has an “OnDamage” function, and make every damageable entity inherit the Damageable class. You can then call OnDamage(params) in the script above, easy as that! Any other questions on inheritcance should be answerd by the video.