Advanced teleportation: How to calculate the dynamical changing space?

In my 3D game I want the character to be able to teleport to a random location in the scene. There are a few challanges:
1.) The character may not teleport into the air above a valley or into a hill.
2.) The character may not teleport into the traps, trees, wall of the houses or any npc.
3.) At least traps and trees will be created randomly each time the player starts a new game.
4.) The npcs are moving around.

My solution is:

  1. After getting my random-Vector3, I can set the y-axis to 1 above the ground, after getting the position of the surface at that x,z coordinates.
  2. Then I can test, if the space is currently used by any object.
  3. If not, then I can set my fps-controller position = new Vector 3 position

I think that… works. But thats it. Is there any more elegant approach than just that brute-force calculation? Especially on more crowed or narrowed maps, I might run into timing problems and end up inside an npc or with a lag.

I’m not sure what your game looks like, but honestly, a totally fine and acceptable approach would to just make “teleportation zones”. For example, use a game object with a trigger collider and draw out a region using the collider where the player can spawn. Then, get a random position within this colliders points. This gives you the most amount of control over where your player can randomly spawn.

Edit: Or, take a look at this: Reddit - Dive into anything

:slight_smile:

I will take the approach from the link you posted, since teleport areas would still not solve my problem with moving targets I do not want to teleport into.

Thank you for your help :slight_smile: