Teleportation mechanic problem.

I am making a character with the ability to teleport (flash or blink, if you prefer) towards the cursor by pressing R. The character can flash through everything but walls. I have been able to get this far by verifying if there are any walls between the character and the targeted Position using layerMask.
To flash I use transform.position = targetedPosition, but if there is a enemy on the targeted position, the character might get stuck inside the enemy. So, I want, in this case, the characterto change the target position to a close one. Any tips that might help?


What you can do is have a child game object to the enemies and give it a collider big enough that the enemy will always be inside the collider no matter the animation. This will give more leeway so the player isn’t halfway in the enemy.

Give the collider a tag, such as, “Teleporter Blocker” (a more general name incase you want other things to be like this). Then, make the collider trigger only.

When the player goes to do teleport, do a raycast that checks for a collider. Check the hit of whatever it hit’s and make sure do if (hit.tag == "Teleporter Blocker") ... and if it does, then targetedPosition = hit.hitPoint; (I believe it’s called hitpoint. Basically it’s the point where the raycast hit on the collider, and not the colliders center.)

And that should do it.