#Block Raycast is NOT Accurate

1 messages · Page 1 of 1 (latest)

silent plaza
#

Hey all,

I'm seeking a better solution to raycasting from the players view to check if they are looking at a block. The entity's builtin one is inaccurate.
Please see code and image below.

system.afterEvents.scriptEventReceive.subscribe(e => {
        // Get source entity and location
        let srcEntity = e.sourceEntity;

        // Send raycast
        let blockRaycastHit = srcEntity.getBlockFromViewDirection()
        if (typeof blockRaycastHit !== "undefined") {
            if (typeof blockRaycastHit.block !== "undefined") {
                let block = blockRaycastHit.block
                world.sendMessage(block.typeId) // Say block name
                ...
            }
        }
    }
})
silent plaza
#

In the image, I expect that it outputs minecraft:bamboo_stairs, cause I'm clearly looking at that block physically.

lone acorn
#

block ray cast is not the same ray cast what client uses

#

its different method

#
  • client
  • server
silent plaza
# lone acorn - client - server

So is there a way to have the server raycast be more accurate? I saw there was other ways of sending raycasts via vectors. And hoping another solution will work

paper grotto
#

I think the reason could be because camera is positioned 1.62 blocks above player origin while execute and possibly scripting as well assumes a position of 1.52. So you can try offsetting it by 0.1 blocks up and see if that will resolve the issue

silent plaza
# paper grotto I think the reason could be because camera is positioned 1.62 blocks above playe...

Yes, it's spot on now! Thank you, Veka :)
New code:

        // Get source entity and location
        let srcEntity = e.sourceEntity;
        // Send raycast
        let srcLoc = srcEntity.getHeadLocation()
        let blockRaycastHit = dimension.getBlockFromRay(new Vector(srcLoc.x, srcLoc.y + 0.1, srcLoc.z), srcEntity.getViewDirection(), {maxDistance: 8})
        let block = blockRaycastHit?.block
        if (typeof block !== "undefined") {
            world.sendMessage(`hit block ${block.typeId}`)
        }