Advertisement
Guest User

Minetest Raycast issues

a guest
Apr 3rd, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.08 KB | None | 0 0
  1. local function get_ray(range)
  2.    local p = minetest.camera:get_pos()
  3.    p.x = p.x / 10
  4.    p.y = p.y / 10
  5.    p.z = p.z / 10
  6.    local direction = minetest.camera:get_look_dir()
  7.    local limit = vector.multiply(direction, range)
  8.    local finish = vector.add(p, limit)
  9.  
  10.    local ray = minetest.raycast(p,
  11.                                 finish,
  12.                                 false, -- include objects
  13.                                 true) -- include liquids
  14.    return ray
  15. end
  16.  
  17. local function is_good_hit(thing)
  18.    if thing == nil then return false end
  19.    if thing == "default:air" then return false end
  20.    return true
  21. end
  22.  
  23. function Pointer(range)
  24.    range = range or 4 -- let's hope for the best -_-
  25.  
  26.    local item = ""
  27.    local node_pos = nil
  28.    for hit in get_ray(range) do
  29.       local thing = minetest.get_node_or_nil(hit.under) -- to get some coords
  30.       if thing ~= nil then
  31.          thing = thing.name
  32.       end
  33.  
  34.       if is_good_hit(thing) then
  35.          item = thing
  36.          node_pos = hit.under
  37.          break
  38.       end
  39.    end
  40.    return item, node_pos
  41.    
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement