- Home /
Detecting mesh collisions after the fact (overlap)?
Is it possible to detect when two colliders overlap but NOT due to a collision? I have a time travel mechanic that involves enabling/disabling large portions of the scene. From the player's perspective, he can travel forward/backward in time and the scene changes around him, but his position does not. This means he can easily end up inside a rock or a wall. I need to detect that overlap and correct it.
I can do this manually with a series of ray collisions on each axis to determine if the player is stuck inside a collider, or if a collider is stuck inside the player, but it's a little clumsy and possibly slow or prone to errors. Is there any way to leverage PhysX to do this for me?
Have you tried using Collider.OnCollisionStay()? Since the objects you won't be colliding with will be disabled, and objects you aren't overlapping shouldn't fire the function, it should only get fired once, but bear in $$anonymous$$d that the physics engine is likely raycasting internally to deter$$anonymous$$e if it should fire the function.
Doing it yourself, raycasting can have a layer mask which can speed it up a lot. A quick improvement is to simply check against a radiusSquared around your gameObjects to deter$$anonymous$$e if you should raycast against them (you could even put this in $$anonymous$$onoBehaviour.OnEnable) and then when you are done enabling stuff, only raycast against the objects close enough to your character from the side of their capsule which faces the object. If they don't hit, then you're inside (assumes convex colliders).
I can check OnCollisionStay again but I seem to recall that it wouldn't fire if the colliders suddenly appeared overlapping and did not actually collide.
And also, convex colliders isn't an option. It's gotta work on complicated objects.
Answer by Matt 12 · Aug 30, 2010 at 11:29 PM
Haha, that was easy. Just made the player a rigid body with appropriate controller, now PhysX takes care of the problem for me. There's a few corner cases I need to check for but those should be easy.
Answer by xinlog · Dec 05, 2010 at 04:00 AM
You can detect by raycasting from the camera :
RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Camera.main.WorldToScreenPoint(detectedPos)); if(Physics.Raycast(ray,out hit)){ //overlap } else{ //not overlap }
Your answer
Follow this Question
Related Questions
Enemy bouncing physics 0 Answers
How to avoid a gameobject inside another while they're in collision? 0 Answers
Normal vector from collision? 1 Answer
Bump when crossing box colliders 1 Answer
How can i create a character like dragon hill game, Digging underground, what collider should i use? 0 Answers