- Home /
OnTriggerEnter Being Called On Unexpected Object
Maybe I have a misunderstanding as to how OnTriggerEnter is supposed to work but here is my following situation and how it differs from what I would expect.
I have three objects in my scene - a player, an enemy, and an attack. The enemy and player both have rigid bodies and box colliders, neither of which is a trigger. The attack object has a box collider that IS a trigger. For scripts, the player has a script with nothing in it other than an OnTriggerEnter function that logs info to the console. The enemy has a simple script that just enables and disables the attack object in a cyclic pattern. When the attack is enabled it collides with the player.
Github with cloneable project/code: https://github.com/valevalorin/TriggerTest
What I would expect to happen: The attack object is enabled and collides with the player. No console output is logged. The player object does not have a trigger collider on it so it's OnTriggerEnter should never be called.
What actually happens: When the attack object collides with the player, the player's OnTriggerEnter function gets called and output is logged to the console.
As far as I can tell, OnTriggerEnter should only be called on objects that actually have a trigger associated to them. Is this not how OnTriggerEnter works?
Answer by donutLaserDev · Mar 15, 2018 at 12:00 PM
Hi,
OnTriggerEnter and other collision events are called on both colliding objects. So, in this case, it is called on attack object and on player.
This is explained here: https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
Your answer
Follow this Question
Related Questions
Collision detection for low relative velocity, high world velocity rigidbodies 2 Answers
How to call OnTriggerEnter once 5 Answers
Stacking mechanic (Parenting Prefab to another prefab on collision) 2 Answers
Collision vs trigger 1 Answer
Change NavMesh Speed after collision/trigger just for 1 object 1 Answer