Uploaded image for project: 'Minecraft (Bedrock codebase)'
  1. Minecraft (Bedrock codebase)
  2. MCPE-183984

Scripting raycasts fail with maxDistance less than 1

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • None
    • 1.21.20.22 Preview
    • Plausible
    • Windows
    • 1297090

      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"));
         }
      }
      

            tryashtar [Mod] tryashtar
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              CHK: