# OnTrigger not firing consistently

I have a Prefab called Player which has a Body and a Sword. The game uses 2 instances of Player, Player1 and Player2.

I use Player1 to strike Player2. This is code on the sword. My hope is that Sword of Player1 will log on contct with Body of Player2. It happens but only the first hit and then i have to hit several times before another strike is logged. But when i look at log from OnTriggerStay it looks like the TriggerExit is never detected untill long after the sword is gone.

    void OnTriggerEnter(Collider other)
{
//Play sound to confirm collision
var sm = ObjectDirectory.soundManager;
sm.PlaySoundClip(sm.gui_02);

Debug.Log(other.name + " - ENTER" );

}

void OnTriggerStay(Collider other)
{
Debug.Log(other.name + " - collision" );
}

void OnTriggerExit(Collider other)
{
Debug.Log(other.name + " - HAS LEFT" );
}


DEBUG LOG:

Player2 - ENTER UnityEngine.Debug:Log(Object) SwordControl:OnTriggerEnter(Collider) (at Assets/Scripts/SwordControl.cs:28)

Player2 - collision UnityEngine.Debug:Log(Object) SwordControl:OnTriggerStay(Collider) (at Assets/Scripts/SwordControl.cs:34)

(The last debug log then repeated hundreds of times long after the sword of player 1 had withdrawn and was in no contact with player 2 )

EDIT: Further tests shows that if i move player1 backwards away form player2 i trigger the OnTriggerExit. Even if the sword is not touching Player2 since after the blow. However even after OnTriggerExit it takes many tries untill i can get another blow registered.

• My first guess would be the collider bounds, which I think is a box surrounding the entire object by default. So even though the sword isn't 'touching' the other character, it's still inside the 'bounding box' of that character. – MeltingPoint Oct 19 '13 at 18:33
• You mean the mesh colliders which are not visible? Yes but i have made sure that they are not touching. – Daarwin Oct 19 '13 at 20:54

I believe you have your code backwards. The trigger(on player 2) should be checking if a GameObject with a rigidbody AND a collider entered it, rather than if the sword entered the trigger.

In addition, your animation (or the collision itself) maybe happening to quickly, so the event may never get called.

• Ive done some testing about it now. It seems that a trigger does not fire if it itself moves uppon the target rigidbody. It has to be the rigidbody that moves into the trigger. Otherwise it does not fire. How can i have gotten this far without realising that! – Daarwin Oct 20 '13 at 11:22

So here's what's going on. You are calling OnTriggerEnter() which responds to ANY collider. You need to only check for the sword's collider. It should be something like this:

void OnTriggerEnter(Collider other)
{
if(other.collider.tag == "MeleeWeapon") {
//Play sound to confirm collision
var sm = ObjectDirectory.soundManager;
sm.PlaySoundClip(sm.gui_02);

Debug.Log(other.name + " - ENTER" );
}

}

void OnTriggerStay(Collider other)
{
if(other.collider.tag == "MeleeWeapon")
{
Debug.Log(other.name + " - collision" );
} else {
Debug.Log("Another collider is hitting this object." )
}
}

void OnTriggerExit(Collider other)
{
if(other.collider.tag == "MeleeWeapon") Debug.Log(other.name + " - HAS LEFT" );
}


Just make sure to add the MeleeWeapon tag to the sword (and any other melee weapons). I don't know how you have your combat setup so I can't give you an accurate way of handling this. But this will fix the problem you are having.

• How do you think i should have my combat setup? Sword is the trigger, and it it also where i handle the triggering. I think you mean to do the opposite? That the body is a trigger and handles the code if the sword enters the body-trigger. I understand what you mean about checking what object is triggering but still, how come i get debug logs about the body still being in TriggerStay even when the sword has left? – Daarwin Oct 20 '13 at 8:15