How do I limit my raycast range?



  • I have these raycast commands and I want to put a range of 6 blocks on it. How can I do it?

    execute as @e[dx=0, distance=0.1..] positioned ~-0.99 ~-0.99 ~-0.99 if entity @s[dx=0] run effect give @s instant_damage 1
    execute positioned ^ ^ ^1 if block ~ ~ ~ air run function oscar:ray
    


  • How to do it

    Set a number equal to how many times the raycast can advance.

    Decrease the number each time you advance the raycast 1 block forwards.

    When you reach zero, stop advancing the raycast.

    In practice

    Before you run the raycast function:

    scoreboard objectives add count dummy
    scoreboard players set #limit count 20
    

    in your raycast function:

    scoreboard players remove #limit count 1
    

    at the beginning of this line:

    execute positioned ^ ^ ^1 if block ~ ~ ~ air run function oscar:ray 
    

    add:

    execute if score #limit count matches 1.. positioned ^ ^ ^1 if block ~ ~ ~ air run function oscar:ray 
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2