mojira.dev

Evelyn Cook

Assigned

No issues.

Reported

MCPE-227192 BlockRaycast faceLocation Offset on Side Faces Awaiting Response

Comments

[media][media][media][media][media]

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!

Hi there,

I reviewed your code and the behaviour you described. After looking closely, the code is working as intended and the API is behaving correctly.

In your script, after getting the block and face from player.getBlockFromViewDirection(), you use a switch statement to offset the block variable to an adjacent block based on the face that was hit (for example, block.below(), block.above(), etc.). As a result, the particles are spawned on the block adjacent to the face you are looking at, not directly on the face itself.

If you want the particles to appear on the actual face you are looking at, you should use hit.block directly, without offsetting it in the switch statement.

So, the observed behaviour is due to the code logic, not a bug in the API. The API is returning the correct block and face information.

Hope this helps clarify things!

This code will spawn your particle array around the actual block you are looking at:

system.runInterval(() => {
    for (let player of world.getAllPlayers()) {
        let hit = player.getBlockFromViewDirection({ maxDistance: 20 })
        if (hit !== undefined) {
            let block = hit.block //Initialise block with the hit block to verify accuracy of the raycast
            // The switch statement will offset the particles according to what face the raycast hits
            // The parameter in each block.method will determine how many blocks the particle array is offset by
            // Uncomment switch statement to offset the particle array
            // switch (hit.face) {
            //     case Direction.Down: {
            //         block = hit.block.below(1)
            //         break
            //     }
            //     case Direction.Up: {
            //         block = hit.block.above(1)
            //         break
            //     }
            //     case Direction.East: {
            //         block = hit.block.east(1)
            //         break
            //     }
            //     case Direction.West: {
            //         block = hit.block.west(1)
            //         break
            //     }
            //     case Direction.North: {
            //         block = hit.block.north(1)
            //         break
            //     }
            //     case Direction.South: {
            //         block = hit.block.south(1)
            //         break
            //     }
            // }
            let dimension = player.dimension
            let pos = block.location
            for (let x = 0; x <= 1; x += 0.5) {
                for (let y = 0; y <= 1; y += 0.5) {
                    for (let z = 0; z <= 1; z += 0.5) {
                        dimension.spawnParticle("minecraft:redstone_ore_dust_particle", {
                            x: pos.x + x,
                            y: pos.y + y,
                            z: pos.z + z,
                        })
                    }
                }
            }
        }
    }
}, 1)

If you wish to have the particle array spawn above the block you are looking at every time, use const block = hit.block.above(1)instead of the entire switch statement