2

I am in 1.14 and I want to check if the player is at a specific Y value.

I tried /execute at @a[y=10] run say hi but for some reason, this will constantly return true no matter what.

I tried /execute at @a[y=10,distance..2] run say hi but this will only work if the player's X and Y values match the command blocks.

How do I detect if the player is at a specific Y value/level?

  • 1
    The key statement in this post is "I am in 1.14". The post you are claiming to be a duplicate of mine is for an older update, as it uses r= as well as the old, execute command. The post even included someone saying "It worked for me in 1.7, but I haven't tested this on 1.10 yet." – Hydra Jun 12 at 23:03
  • 1
    @FabianRöling This is definitely not a duplicate of that question. The question you linked is very outdated. I am working in 1.14 as I mentioned, and you linked a post from 2017 with the old execute, old r= and someone even says that they haven't even tested it out in 1.10 yet on that post. – Hydra Jun 13 at 1:38
  • Ok, I retracted the vote. This is definitely a duplicate of something, but I can't find it, so I'll just answer. – Fabian Röling Jun 13 at 7:50
  • I take it back, I found the original: gaming.stackexchange.com/questions/340823/… – Fabian Röling Jun 13 at 7:56
  • 1
    @Mr.Bear I 'rejected' your edit, since the OP already asks for a way to detect if the player is at a "specific" position, and making it an 'exact specific' position seems redundant. – Joachim Jun 14 at 21:18
2

You can set up a scoreboard objective with this command:

/scoreboard objectives add yPos dummy

Then you run this command to update the height with very high precision, I recommend using this in a repeating command block:

/execute as <target> store result score @s yPos run data get entity @s Pos[1] 1000000

And finally to make every target at a very specific y-level say hi you can use this command:

/execute as @e[scores={yPos=70000000}] run say hi

In this particular case it would be for any entity standing at a y-level of exactly 70.

This solution is multiplayer friendly and works for non-player entities, too.

2

You can use this code:

/execute as <player you want to check> at @s if entity @s[y=65,dy=0] run say hi

It will say hi if the hitbox of the player is within the block at y=65. the dy=0 appears to be nessessary to prevent it from always saying hi, the x and z seem to default to the position where the command is executed.

This would probably work, too:

/execute at <player you want to check> if entity @p[y=65,dy=0] run say hi
  • This works, but the accuracy is a bit off. I noticed that the command works even if the player is not directly at the determined y value. It is within a range of ~ -1.5 and +0.5. This can be tested out by using stairs, slowly walking up them, using F3 to check the y value while the command is running. The "hi" will appear even if not exactly at the desired y value. Do you know how to resolve this problem? – Hydra Jun 14 at 0:40
  • 1
    The accuracy is not off, the command does apparently not measure quite what you want though. It measures if your hitbox is within a block at a certain hight, the block itself is 1 block high and your hitbox is 1.8 blocks high, or 1.5 while ducking. Crawling reduces it to under 1, but I don't know the exact hight, it looks like about 0.5 blocks though. I will look at it later to see if I can find a more precise solution. – bearb001 Jun 14 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.