OnTrigger not firing consistently

Posted by Lautaro on Game Development See other posts from Game Development or by Lautaro
Published on 2013-10-19T12:23:45Z Indexed on 2013/10/19 16:10 UTC
Read the original article Hit count: 251

Filed under:
|

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.

© Game Development or respective owner

Related posts about c#

Related posts about unity