- Home /
Detect if a non-trigger collider is inside another collider
In a game I'm working on, the player can teleport into any point of the map. This results in a case where player can be teleported for example inside a rock (or any other solid object). When that happens, I want to disable all colliders of that rock object and enable them once player exits the object. I want all solid objects to have this functionality and for them to detect the player and not other way around.
Problem I am having is detecting when player is suddenly teleported inside another solid object, since they both have a solid colliders and not triggers. I'm just trying to figure out possible ways to achieve this and would appreciate some pointers in right direction.
Answer by Jjules · Jun 25, 2018 at 10:24 AM
I'm not sure allowing solid objects to move inside of each other, THEN fixing it on reaction is the best idea.
One approach would be to, right before teleporting, take the point you're teleporting to, and have the rock run a bounds.Contains on that point, and if it returns true, disable the object. The problem, though, is that this would have to run on every potential object every time the player teleported
Another solution would be to make the player's collider a trigger on the frame he teleports, setting isTrigger to true. Then, check for a collision on that frame, and if there is a collision, trigger some function in the other object to turn ITS collider into a trigger. Then the player can go back to a non-trigger on that frame, and the other object can use OnTriggerExit to know when the player is no longer inside of it
I know this isn't exactly the solution you were hoping for, but hopefully it gave you some idea of what direction to go in!
Player can basically blindly pick a point in the map and teleport there. Which means that sometimes they might end up in a solid object and I can't prevent that. I can't set the collider to trigger either because where ever player is teleported the collider still needs to be able to collide with some objects (for example stand on ground, without falling through).
You may not be able to prevent objects from going through each other, that's fine, I'm specifically saying having a "solid" object, as in a non trigger collider, inside of another isn't a good idea.
And if you do as I suggested, the player turns to a trigger literally for one frame, so there should be no collision issues because the player will be solid again before the user gets to move. It should also be too brief for the player to fall through the floor, but if they do you can always set the rigidbody to be kinematic on that frame and that problem shouldn't happen
Can you add a second collider to the player, and set that one as a trigger and use itin the way Jjules described to detect the collisions?
Your answer
Follow this Question
Related Questions
Strange issue with collider 1 Answer
How to get OnTriggerEnter effect without using isTriggered on Collider? 1 Answer
OnCollisionEnter isnt called when player lands on object 1 Answer
Inconsistent Collision Detection 3 Answers
How to detect collision between 2 objects while checking are they the ones that need to collide? 1 Answer