- Home /
How Does OnTriggerEnter() Work?
Hello everyone, the question I'm asking is probably more involved than the title would make it appear. Specifically, I'm asking how this method (and its related methods) actually go about figuring out if an object has entered / remains inside / has exited a collider.
For example, I could use collider.bounds.Contains() to check if a point is inside a sphere/capsule/mesh collider, but since bounds is "just a box" the result may not be accurate at all. Yet, OnTriggerEnter() will give an accurate result. So I'm after what makes that method so accurate over something like bounds.Contains(), or in other words, how to build my own method similar to OnTriggerEnter() / OnTriggerStay().
In my specific case, I'm trying to check if "objects of interest" are within some of my colliders when the "game starts", meaning that OnTriggerEnter() won't be called at all because the objects start inside the colliders, and bounds.Contains() isn't very accurate.
Any ideas are appreciated, thank you for reading.
And to expand on this, OnTriggerExit won't work for me if the object is teleported away while inside a trigger. Like going to a checkpoint.
You can figure out exactly how it works, Unity uses a version of PhysX to run the physics, and the source of the main project is on GitHub. Read info about that here.
Unity uses something called the Scene Graph to keep track of where things are in the game word. It's fairly complex, and replicating that will be a lot more work than what's probably viable.
If you just want to know how far some things are away from some other things, just check their distance. Have everything that you need to check register themselves to some static list, and check how far away each of those are from the collider. Remember that comparing the square distances you get from Vector3.sqr$$anonymous$$agnitude is faster than checking the distance itself.
Interesting, thanks for the heads up. In my case (with mesh colliders) distance isn't very relevant so for the moment I'm using colider.raycast() to check for intersections. If there is intersection, then the object is outside the collider. If no intersection occurs, the object should be inside the volume.
Your answer
Follow this Question
Related Questions
How to make gameObject make only one collision 1 Answer
boost and hit the edge of a collider and you go backwards 0 Answers
How to have a score counter increment in OnTriggerEnter 1 Answer
Application.LoadLevel ("lower") loads on start-up, not when triggered; Deadline is soon HELP! 3 Answers
onEnterCollider returns Collider 1 Answer