How can you detect the fractional part of the player's y coordinate? [1.15]

My goal is to be able to detect if the fractional part of the player's y coordinate is within the ranges:

  • .0 through .4
  • .41 to 0.5
  • .51 to 1

So for example, if the player is at y-value 64.375, this would fall under the .0 through .4 category.


Solution 1:

You can do this with only scoreboards and /execute store, without needing any dummy entities, teleportation, fancy pictures, etc., but using the fact that all entities (including players) store their coordinates in the Pos tag in NBT and that scoreboards use integer rounding (always down).

Firstly, two scoreboards are needed:

/scoreboard objectives add y dummy
/scoreboard objectives add y_10 dummy

Also, a standard in commands is to have a const scoreboard, so I'll put a 10 in there instead of one of the two existing ones:

/scoreboard objectives add const dummy
/scoreboard players set 10 const 10

Then you can just store your Y coordinate in the first two scoreboards, but one time multiplied with 10:

/execute store result score @s y run data get entity @s Pos[1]
/execute store result score @s y_10 run data get entity @s Pos[1] 10

Since scoreboards are integers, you would for example get 56 and 563 in them, if your Y position is 56.39154.

Now you can multiply the smaller one with 10:

/scoreboard players operation @s y *= 10 const

This would set your y score to 560 in my example.

Next, subtract the two:

/scoreboard players operation @s y_10 -= @s y

And there it is, the value 3 in the scoreboard y_10. You can for example do something depending on your wanted range of .0 to .4 like this:

/execute if score @s y_10 matches 0..3 run …

I set the upper limit to 3, because that 3 is a result of integer rounding, so the command would actually trigger if your Y coordinate's fractional part is between 0 and 0.399999….

Solution 2:

I'm actually answering my own question, because I have found an answer.


First, I align entities to the bottom of the block the player is in, with /execute align y This makes sure the entities start at a whole number y value.

Then I position them a certain distance upwards, by teleporting them upwards. Example: tp ~ ~0.2 ~ Now the entities are always at a y value of 21.2 or 64.2, etc.

Finally, I use the [distance=..<number>] selector.


I created this 2D-side view diagram to help demonstrate:

In the diagram, the orange square represents the block that the player is standing in. The area_effect_clouds are represented by the black dots, and are at identical x and z coordinates of the player.

Lets say the player checks if they are anywhere from y=0 to y=0.4, with execute if entity @e[tag=customTag,distance=..0.2]

If the command returns true, we now know the player is within 0.2 blocks of that entity. This can be repeated for the other 3 sections.