- Home /
OnTriggerEnter works, OnTriggerExit doesn't
The player object has a gameobject with a small collider cube (set as trigger) at eye height. The following script is attached to that object:
// Use this for initialization
void OnTriggerEnter(Collider trigger ) {
if (trigger.gameObject.tag == "flooder" && !playerStats.m_headUnderWater){
Debug.Log("head under water");
playerStats.toggleUnderWater(true);
}
}
void OnTriggerExit(Collider trigger) {
Debug.Log("LeftTrigger!!!");
Debug.Log(trigger.gameObject.tag);
Debug.Log(trigger);
if (trigger.gameObject.tag == "flooder"){
Debug.Log("head left water");
playerStats.toggleUnderWater(false);
}
}
This is supposed to detect if a players head is underwater to trigger drowning.
The on trigger enter works fine, but on trigger exit will never register (not even the debug statements)
The body of water has a kinematic rigid body and a mesh collider set (also with trigger = true) to make collision possible and they are both in a layer that detects collision with the other layer. Tried setting the rigid body collision detection to continuous but even that did nothing.
Any help is appreciated!
hi try using trigger.collider.tag and
try removing if (trigger.gameObject.tag == "flooder"){
in OnTriggerExit and just use
playerStats.toggleUnderWater(false);
Not even the debugs get printed, so it's not in the if statement
watch your collider of water when you jump out
maybe you are draging colider with you
maybe you delete the colider
maybe you make water false before you get out I don't know how other scripts go
do you really jump out or just swim to the surface?
what happens if it's not kinematic?
it shouldn't move anyway as it's trigger so collider never colides because it's trigger so does not listenes to forces I think
So after watching the collider it is not reacting to the impact with the player. The collider is definitely there. Switching kinematic changes nothing. The water rises with 0.01 each fixed update. But so slow that after the head "submerges" a jump clears the collider box from the collider mesh of the water.
I also tried adding oncollisionenter,exit, stay and ontrigger stay. Nothing gets printed/triggered after ontriggerenter :(
Nope - everything that moves needs to have a rigidbody or it's collision information may not be updated - plus you get a lot of performance warnings.
Answer by nopogo · Apr 12, 2013 at 09:11 AM
Found out that box colliders work in this setup, but mesh colliders do not. I'm thinking about submitting a bug report to unity
$$anonymous$$eshColliders are generally a nightmare - they suffer from back face culling and are 1 directional - these things are documented in the Unity Docs but the practical experience of them can seem different.
Answer by m.alekseev · Apr 09, 2013 at 03:49 PM
Maybe your rigid body falls asleep?
This is literally 1 second after ontriggerenter, I jump in water and the ontriggerenter gets registered. Then jump out but nothing
to be sure i also set the rigidbody asleep timer to infinite in physics settings
Answer by Er_ellison · Sep 17, 2017 at 11:34 AM
No , You wrong
I had the same problem but , think a little bit about it !
when you disabling that object while it entering to the area , YOU DISABLED IT , means that there is no active collider on scene that determines the OnTriggerExit !
if you use SetActive to disable the object that you want , Completely Wrong , You can use :
yourObject.transform.localScale = Vector3.zero;