mojira.dev
MCPE-183984

Scripting raycasts fail with maxDistance less than 1

The bug

In the scripting API, the raycasting functions getBlockFromRay and getBlockFromViewDirection take a BlockRaycastOptions struct with a maxDistance field. This field is a javascript number, so it's expected to take any floating point value.

However, during actual execution, the value is rounded down to the nearest integer. This severely limits the granularity of raycasts. It can be desirable to perform a very small raycast in a particular direction to determine if we've reached the end of a wall, for example. With this behavior (silently failing when less than 1), this is impossible and confusing.

How to reproduce

1. Download and open the attached world

2. Walk over to the cherry plank wall, looking at it very closely
3. Run /scriptevent test:raycast1
4. Run /scriptevent test:raycast2

Expected behavior
The first command should place a diamond block, then the second one should place a gold block.

Observed behavior
Only the first command worked, the second one did absolutely nothing.

Notes
The relevant javascript file is in the behavior pack, in scripts/entry.js. The relevant code is duplicated here:

if (e.id === "test:raycast1") {
   let block = e.sourceEntity.getBlockFromViewDirection({ "maxDistance": 1 });
   if (block !== undefined) {
      block.block.setPermutation(BlockPermutation.resolve("diamond_block"));
   }
}
if (e.id === "test:raycast2") {
   let block = e.sourceEntity.getBlockFromViewDirection({ "maxDistance": 0.99 });
   if (block !== undefined) {
      block.block.setPermutation(BlockPermutation.resolve("gold_block"));
   }
}

Attachments

Comments 2

Hi there,

Thanks for raising this! You are correct that the API rounds maxDistance down to the nearest integer, so values less than 1 will not return a hit. This does limit the granularity of raycasts, and in some edge cases, finer control could be helpful.

However, for most practical gameplay scenarios—such as detecting the end of a wall while moving close to it—a maxDistance of 1 is usually sufficient. If the block directly in front of the player is solid, getBlockFromViewDirection({ maxDistance: 1 }) will return a hit. If it is air, it will return undefined, which you can use to detect a gap or the end of the wall.

Here’s a helper code snippet that demonstrates this approach:

const hit = player.getBlockFromViewDirection({ maxDistance: 1 });
if (!hit) {
    // No block or air in front will return undefined: likely at the end of a wall or facing a gap
    // Handle wall end logic here
} else {
    // Solid block detected: still next to the wall
    // Handle wall-following logic here
}

While more accurate raycasting could benefit some advanced use cases, for most wall-following or gap-detection logic, the current behaviour is reliable.

Hope this helps clarify things, and thanks again for highlighting the limitation!

tryashtar

(Unassigned)

1297090

Plausible

Windows

Script-API

1.21.20.22 Preview

Retrieved