Minecraft Maps / Other

Line of sight detection

  • 673 views, 1 today
  • 25 downloads, 0 today
  • 0
  • 0
Cymaera's Avatar Cymaera
Level 34 : Artisan Miner
34
Commands:
# create scoreboard objectives, call it "lineOfSight.x" and "lineOfSight.y"
# we will use these to store and calculate entity rotations
/scoreboard objectives add lineOfSight.x dummy
/scoreboard objectives add lineOfSight.y dummy

# summon armour stand, call it "lineOfSight.sensor"
/summon minecraft:armor_stand ~ ~1.9 ~ {Tags:["lineOfSight.sensor"], Marker: 1b, Invisible: 1b}

# LOOP: make sensor face away from the player
/execute as @e[​tag=lineOfSight.sensor] at @s facing entity @p eyes run tp @s ~ ~ ~ ~180 ~

# LOOP: store player and sensor rotation in scoreboards
/execute as @e[​tag=lineOfSight.sensor] store result score @s lineOfSight.x run data get entity @s Rotation[​0]
/execute as @e[​tag=lineOfSight.sensor] store result score @s lineOfSight.y run data get entity @s Rotation[​1]
/execute as @p store result score @s lineOfSight.x run data get entity @s Rotation[​0]
/execute as @p store result score @s lineOfSight.y run data get entity @s Rotation[​1]

# LOOP: add/subtract player rotation from sensor rotation to find the relative rotation
/scoreboard players operation @e[​tag=lineOfSight.sensor] lineOfSight.x -= @p lineOfSight.x
/scoreboard players operation @e[​tag=lineOfSight.sensor] lineOfSight.y += @p lineOfSight.y

# LOOP: if the sensor's relative rotation is 0, then the player is looking at it.
# you might want to leave some margin so the rotations don't have to match exactly.
/execute as @e[​tag=lineOfSight.sensor] if score @s lineOfSight.x matches -1..1 if score @s lineOfSight.y matches -1..1 run tellraw @a "Hello World!"
Progress100% complete
Tags

Create an account or sign in to comment.

Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome