How can I make the player teleport a certain amount of blocks?

I want to make a sort of elevator in Minecraft with a command block. You step into a small room, press the button and get sent a few blocks up to a room above the current room.

The first room and the second room

Is there a way to send the player a few blocks up or down?


Solution 1:

You can use a simple /tp command, you either use absolute values, to teleport the player to 100 64 100 you would use this:

/tp @p 100 64 100

Or relative values, for 3 blocks up that would look like this:

/tp @p ~ ~3 ~

Note that this will teleport the player relative to the command block, to teleport the player relative to the current loaction you would use:

/execute at @p run tp @p ~ ~3 ~

Solution 2:

on the command block in the room down, input the following command:

/tp @p ~ ~9 ~

and on the command block in the room up, input the following command:

/tp @p ~ ~-9 ~

these two should teleport the nearest player in the same x and z coordinate, but +9 or -9 from the command block's y coordinate from the command block initiating the command (respectively)