How to teleport the player to random locations on the map

I’m creating a final project for my senior seminar class in college and am making an environment where the player starts in an empty room and then when they walk to the other end of it, they are randomly teleported to 1 of 11 other rooms I created that have different content in them. I then want the player to be able to go back to the blank room and then repeat the process over again. I am fairly new to UE4 and blueprints, but my thinking is to create box triggers that allow the player to be moved to a random location when the player collides with it. I’ve looked up tutorials on how to randomly spawn a player or teleport them to a single location but haven’t found anything about random teleport location. I’m just wondering how to go about randomly moving the player to 1 of the 11 other rooms I created. Is there a way to label each box trigger and have a random number generator determine which room the player moves too? I hope this all makes sense and appreciate any help I can get on what way best to approach this problem.

Thanks,
Chris

You will have to create a box trigger for each room in your level BP.

Add all of them to an array which will provide you with numbers assigned to each room (aka array index). Do so on “Even begin play”.

On overlap with your special first one you get the “last index” from your array and get a “random number in range” where your minimum is 0 and the maximum your “last index”. The result will be a collison box actor.

Then just add a “set actor location” to the “get actor location” of the trigger and you’re done.

If you want a truly random position within those box collisions you will have to get the “scaled box extent” from your selected box and use “random point in bounding box”.

Your origin is the box collision actor location and the extent the scaled box extent.

Cheers

All teleportation does is call SetActorLocation for any given world location. If you want to define a specific position where the player needs to start in each room, the best way is probably to create a new Actor class and place one such object in each room at the player start position. (You could also use the existing Player Start actor for this.)

Like Erasio already wrote, you collect all these actors in BeginPlay into an array. Later, whenever the player enters the leaving area of a room is placed at the location of one of the array elements chosen at random. If you don’t want to reuse rooms, you can remove the array element after it was chosen.

Thanks so much for the help. I got it all working fine now.

Create a TargetPoint that you can place into the middle of each room.

When your character leaves the room, use a “Get All Characters Of Class” and set the class of it to the TargetPoint you just created. After that just randomly pick one and set your characters global position to the location of whichever TargetPoint you picked.

To get the broader random that you want, each your TargetPoint class can be given an X and Y value that offsets your final location from its center. Expose these two values as public and that will give you the ability to calibrate each TargetPoint to the room it is placed in.

I would give you the blueprint, but it’s YOUR homework :slight_smile: