Skip to content

get_distance

Mika // Aeldrion edited this page Aug 18, 2020 · 2 revisions

Returns the distance between the executing entity and the executing position, with a scale of 50. Caps at 926 blocks.

Output

  • $out aestd score: the distance. 50 = 1 metre.

Example

The following functions display the distance in metres of every player to the nearest creeper, if there is one in a 32 blocks radius.

# mypack/main.mcfunction
execute as @a at @s if entity @e[type=minecraft:creeper, distance=..32] run function mypack:distance_to_nearest_creeper

# mypack/distance_to_nearest_creeper.mcfunction
execute at @e[type=minecraft:creeper, sort=nearest, limit=1] run function aestd1:entity/get_distance
scoreboard players operation $out aestd /= $50 aestd.const
tellraw @s actionbar {"score": {"name": "$out", "objective": "aestd"}}

Technical details

The coordinates of the executing position are determined using a newly spawned entity's Pos NBT tag. The coordinates of the executing entity are grabbed from its Pos tag. The distance is calculated using the following formula:

Euclidean distance formula between two points in a 3D space

The square root is calculated using the aestd1:math/square_root function.