How can i prevent my character from teleporting out of level?

Hi, i am trying to make my character shoot a sphere and then using a button teleport where the sphere is at that moment. The only problem is that if the sphere is very close to a wall, floor or ceiling at that time, my character teleports inside that wall and then starts to fall out of the level. Can someone think of a way to prevent that from happeming? thanks!

I guess it depends on your scene, but you could evaluate the distance from sphere to nearest wall, and adjust the teleporting position accordingly. That could be done by raycasting, or using Vector3.Distance. Raycasting would be more convenient, i’d say. Check it out in the docs, RaycastHit.Distance is what you might be after…

if the ball is rolling on the floor though, this wouldn’t work. I need something that finds an allowed position to teleport to. This means that if the ball hits the floor, the player is teleported above the ball, if it hits the roof he is teleported under the ball and so on