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.
Answer by exzizt · Mar 25, 2018 at 10:11 PM
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: https://www.reddit.com/r/Unity3D/comments/3azuud/choose_random_location_that_doesnt_end_up_inside/
:)
Answer by FH1979 · Mar 27, 2018 at 05:19 PM
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 :)
Your answer
Follow this Question
Related Questions
Modify Built-In Class Variables Get/Set functions 1 Answer
Teleporting problems 1 Answer
Sprites do not refresh/reimport with ScriptedImporter 0 Answers
Can't teleport a car? 0 Answers
Need help clamping rotations to a range 0 Answers