Hello everyone, I’m working on a teleportation system that lets the player instantly warp to a fixed distance in the direction their camera is pointed. Currently I am trying to implement a “savior” mechanic that will attempt to relocate the player to a safe location should they end up inside of a wall. To do this, I would like for it to find the closest point on the collider it is inside of, and move the player there if it isn’t too far away from the desired location.
The issue is that Collider.ClosestPoint just returns the position you give it if it is inside of the collider. Is there anyway that I can find the closest point from inside of the collider? One potential solution I thought of was to find the collider face closest to the search position and give the perpendicular vector to it, but I’m failing to find any way of accessing individual surfaces of a colliding mesh/box/etc.
I’d really appreciate any input here, even if what I’m asking for is impossible. Unless there is some custom unity package that can accomplish my task? Thank you!
If you’re using a nav mesh for navigation you can use NavMesh.SamplePosition to find the closes position from a given point, to the closes point that is inside of the nav, you can use Walkable as a layer mask.
You can try using the collider’s bounds. Since they’re the limit of the collider, you can find a vector in the bounds maybe using closesPointInBounds and determine the direction of this new vector from the player, then move it along that direction to that position + your character collider’s diameter should in theory remove him form the collider, I think.
The same principle would apply if you take the direction of the vector that exists between the vectors of your player and the center of the collider xhe’s in, and using that direction, move it with a magnitude of the collider raidus xhe’s + your character collider’s diamtere
Hmm, unfortunately this doesn’t seem to be working either, the closestPointInBounds function also just returns the given position if it’s inside of it.
EDIT: just saw your edit, I’ll give that solution a shot
EDIT2: Actually I just thought of an issue with this, if the collider the player is in is really long (like a 2x4 shape) then the direction from the center out towards the player might give the wrong closest point. Like if it goes straight out to the distant end instead of just going through the long side next to it. I’m attaching a visual example.
yeah, I was thinking on a spherecollider/capsule, wich is not what you’ll probably find.
But then if it’s a box collider, you should solve it like you did in your drawing: you use Collider.bounds to check wich bound is closer to you, and escape to that position.
First you’d need to check the collider’s center to see in wich quadrant of the box you’re placed in, this will give you the position you have inside of the collider. Then you see wich bound is closer in that area. Say you’re in the north east quarter, you know that your best option is either to escape towards the north or towards the east. Then, to choose wich one, you can calculate your distance to the center to know how far you’re from the north bound and how far you’re form the east bound, and choose wichever is smaller.
Before choosing one, you should make sure your character won’t land inside another object’s collider.
I’m not really sure I understand. The closestPointInBounds function cannot find the closest bound point from within the collider, so how would I be able to determine which is closest from the collider’s center?