Advertisement
jaklsfjlsak

8/13激光测距

Aug 13th, 2023 (edited)
1,302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.88 KB | None | 0 0
  1. local laser = peripheral.find("warpdriveLaser")
  2.  
  3. laser.beamFrequency(1420)
  4.  
  5. -- Check if the laser was found
  6. if not laser then
  7.   print("No warpdriveLaser detected")
  8.   os.exit()
  9. end
  10.  
  11. print("Press the 'M' key to emit a laser scan and calculate the layer offset.")
  12.  
  13. -- Loop to wait for key events
  14. while true do
  15.   -- Wait for a key event
  16.   local event, key = os.pullEvent("key")
  17.  
  18.   -- Check if the "M" key was pressed (key code 50)
  19.   if key == 50 then
  20.     -- Get the laser's own position
  21.     local _, laserY, _ = laser.getLocalPosition()
  22.  
  23.     -- Emit a laser scan in the Y- direction (0, -1, 0)
  24.     laser.emitBeam(0, -1, 0)
  25.  
  26.     -- Get the scan result
  27.     local _, _, targetY = laser.getScanResult()
  28.  
  29.     -- Calculate the layerOffset
  30.     local mineTarget = laserY - targetY
  31.  
  32.     -- Print the layerOffset
  33.     print("Target is: " .. mineTarget .. "blocks below")
  34.   end
  35. end
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement