- Home /
OnTriggerEnter only works if gameObject is moving
Hi,
I have a question regarding the onTriggerEnter function. My scene has the following set up:
I have one gameObject with a rigidbody component and is Kinematic set to true. This object can be controlled by the player and should avoid upcoming obstacles, without beeing affected by the physics system.
The obstacles have a box collider and isTrigger set true.
the player object has the following script running:
function OnTriggerEnter(myTrigger : Collider){
Debug.Log(myTrigger.gameObject.name)}
My problem is that the onTriggerEnter function is only called when I move the player object, while it hits the trigger. If it is standing still while the trigger hits it, the function is not called. Is there a failure with the script or the set up? Because I need to activate the trigger, even if the player object is not moving.
Thanks in advance
Answer by Ejlersen · Dec 21, 2010 at 07:59 PM
Just looked through the documentation. Maybe you can find your answer here:
Hi, I already the chapter over and over and it seems like I# m missing something. $$anonymous$$aybe rigidbodys need to be in movement to make trigger checks but that wouldn't make much sense, would it?
Triggers are most often used as something staticly in the scene, which is then triggered by something moving into its collider. Rarely its the other way around. You could try to add a rigidbody to the trigger object, maybe then it will register.
Thanks a lot. I added a rigidbody and put set it to kinematic and now it works.
Answer by jordankid93 · Jan 01, 2019 at 07:07 AM
Incase others might come across this, I was having an issue where a collider2d that I toggle activate wasn't triggering OnTriggerEnter2D until I moved. The solution for me was that I had to set my RigidBody2D sleeping mode to Never Sleep
.
Answer by Buzzrick · Aug 02, 2013 at 02:02 AM
I had exactly the same problem. It took a bit of figuring out but the documentation on BoxCollider states that:
"If you are moving an object through its Transform component but you want to receive Collision/Trigger messages, you must attach a Rigidbody to the object that is moving."
I ended up having to add a Rigid body to both of my objects for this to work consistently, because they were both moving objects.
Answer by Occma · May 12, 2015 at 09:44 AM
I have the same Problem and as I see, this is one of the first threads to pop up in google.
To fix the bug in my case a added the rigidbody to the moving object instead of the player. Now both have rigidbodys and behave as intended.
Hope this helps
Answer by ycode · Dec 27, 2018 at 05:39 PM
I still got the same symptom although my gameobjects already had Rigidbody, as other answers mentioned.
Then, I found that even having Rigidbody, onTriggerEnter is still not called if the both gameobjects' rigidbodys do not have "Use Gravity" checked.
Checking "Use Gravity" of Rigidbody on either gameobject makes onTriggerEnter called without the gameobjects moving.
It's been a while since the question is raised, so that I'm not sure it was the case back then, but at least, it works this way as of Unity 2018.2.18f1.
btw, this is also the same for OnCollisionEnter.
Your answer
Follow this Question
Related Questions
OnTriggerEnter gives me error? 2 Answers
Disable Mesh Collider Trigger 1 Answer
OnTriggerEnter fails to activate 1 Answer
OnTriggerEnter only sent to one of the colliding objects, not both? 0 Answers