- Home /
Determine that an object is -within- a trigger
Let's say I have an oddly shaped MeshCollider set up as a trigger, and I want to check if a certain object is within this trigger.
Unfortunately, OnTriggerStay is only called if the object touched the boundaries of the trigger, and I found OnTriggerEnter and OnTriggerExit rather unreliable to tell which objects went in and which went out.
Any bright ideas?
When you say OnTriggerEnter is unreliable, what do you mean? That it's not detecting objects or that you don't know how to access that info?
How oddly shaped is the $$anonymous$$eshCollider? If you are having problems with the OnTrigger functions I would suppose it's a non-convex shape. Can you give us more information on this?
Answer by Bampf · Feb 04, 2011 at 12:18 PM
Haven't tried this, but the following algorithm from computational geometry should work:
http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html#Polyhedron
Basically, you fire a ray from a point, and if that ray hits an odd number of faces of the mesh, then you are inside. Use RaycastAll to get back all the hits, not just the first one, and use the new raycast layers feature to avoid hitting other meshes.
Your answer
Follow this Question
Related Questions
OnTrigger event when colliders are already touching eachother 1 Answer
Combining Triggers 1 Answer
Trigger animation going crazy 3 Answers
How to use OnTriggerEnter with multiple triggered objects? 1 Answer
Work around for onTriggerStay? 1 Answer