The bug
The point returned by player.getBlockFromViewDirection
does not match the actual block or face the player is looking at in some cases.
How to reproduce
1. Download and open the attached world. It has a script that displays red particles as a preview for block placement.
2. /tp @s -7.43 7.01 -40.60 61.48 28.40
Expected behavior
The red particles should be on top of the block, since that's where you're looking.
Observed behavior
They're on the side instead.
Notes
The relevant JS code is in scripts/entry.js
. I've also attached it here for convenience.
Attachments
Comments 4

One theory about this is that the raycast uses the center of the head as its origin, which works great for most entities. However, since the first-person view of the player is slightly above the center of the head, the raycast is further down than it should be.
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