Swimer Posted February 19, 2022 Share Posted February 19, 2022 I tried to search on the Internet, but nothing happened, because most likely I do not know how to formulate it correctly. How do I send a ray that starts from the camera, with the direction of the camera, and get the player it hits? Link to comment
marcelluss Posted February 19, 2022 Share Posted February 19, 2022 use the following functions for this: dxDrawLine, getCameraMatrix Link to comment
Swimer Posted February 19, 2022 Author Share Posted February 19, 2022 5 minutes ago, marcelluss said: use the following functions for this: dxDrawLine, getCameraMatrix local w, h = guiGetScreenSize () local tx, ty, tz = getWorldFromScreenPosition ( w/2, h/2, 50 ) local px, py, pz = getCameraMatrix() local hit, x, y, z, elementHit = processLineOfSight ( px, py, pz, tx, ty, tz ) if hit then if elementHit then local xx,yy,zz = getElementPosition(elementHit) local sX, sY = getScreenFromWorldPosition( xx, yy, zz ) dxDrawText("G", sX, sY, sX, sY, "red", 5, 5, "default") end end I've made a small code from the example, but it doesn't work. What's the matter? Link to comment
marcelluss Posted February 19, 2022 Share Posted February 19, 2022 2 minutes ago, Swimer said: local w, h = guiGetScreenSize () local tx, ty, tz = getWorldFromScreenPosition ( w/2, h/2, 50 ) local px, py, pz = getCameraMatrix() local hit, x, y, z, elementHit = processLineOfSight ( px, py, pz, tx, ty, tz ) if hit then if elementHit then local xx,yy,zz = getElementPosition(elementHit) local sX, sY = getScreenFromWorldPosition( xx, yy, zz ) dxDrawText("G", sX, sY, sX, sY, "red", 5, 5, "default") end end I've made a small code from the example, but it doesn't work. What's the matter? can I see your full script? Link to comment
Swimer Posted February 19, 2022 Author Share Posted February 19, 2022 Just now, marcelluss said: can I see your full script? local cursorShowing = false bindKey("f2", "down", function() cursorShowing = not cursorShowing showCursor(cursorShowing) end) local nameColor = white local nameSize = 1.2 local nameFont = "pricedown" addEventHandler( "onClientRender", getRootElement(), function( ) for _, plr in ipairs( getElementsByType( "player" ) ) do -- if not (plr == localPlayer) then local x, y, z = getPedBonePosition( plr, 8 ) local sX, sY = getScreenFromWorldPosition( x, y, z ) if sX then local id = getElementData(plr, "id") local text if id then text ="Player ["..id.."]" else text = "Player" end local leftX = sX- dxGetTextWidth(text, nameSize, nameFont )/2 local topY = sY-60 local rightX = leftX local buttonY = topY dxDrawText( text, leftX, topY, rightX, buttonY, nameColor, nameSize, nameSize, nameFont, "left", "top", false, false, false, false, false) end -- end end local w, h = guiGetScreenSize () local tx, ty, tz = getWorldFromScreenPosition ( w/2, h/2, 50 ) local px, py, pz = getCameraMatrix() local hit, x, y, z, elementHit = processLineOfSight ( px, py, pz, tx, ty, tz ) if hit then if elementHit then local sX, sY = getScreenFromWorldPosition( x, y, z ) dxDrawText("G", sX, sY, sX, sY, "red", 5, 5, "default") end end end ) Link to comment
marcelluss Posted February 19, 2022 Share Posted February 19, 2022 local cursorShowing = false bindKey("f2", "down", function() cursorShowing = not cursorShowing showCursor(cursorShowing) end) local nameColor = white local nameSize = 1.2 local nameFont = "pricedown" local ped = createPed(0, 0, 0, 3); addEventHandler( "onClientRender", getRootElement(), function( ) --for _, plr in ipairs( getElementsByType( "player" ) ) do -- if not (plr == localPlayer) then local x, y, z = getPedBonePosition( ped, 8 ) local sX, sY = getScreenFromWorldPosition( x, y, z ) if sX then --local id = setElementData(plr, "id") local text if id then text ="Player ["..id.."]" else text = "Player" end local leftX = sX- dxGetTextWidth(text, nameSize, nameFont )/2 local topY = sY-60 local rightX = leftX local buttonY = topY dxDrawText( text, leftX, topY, rightX, buttonY, nameColor, nameSize, nameSize, nameFont, "left", "top", false, false, false, false, false) end -- end --end local w, h = guiGetScreenSize () local tx, ty, tz = getWorldFromScreenPosition ( w/2, h/2, 50 ) local px, py, pz = getCameraMatrix(localPlayer) -- test line -- local line = dxDrawLine3D(px, py, pz, x, y, z) local hit, x, y, z, elementHit = processLineOfSight ( px, py, pz, tx, ty, tz ) if hit then if elementHit then local sX, sY = getScreenFromWorldPosition( x, y, z ) dxDrawText("G", sX, sY, sX, sY, tocolor(255, 255, 255), 1, "default") end end end ) If I understood your idea correctly, then here is the script Link to comment
Swimer Posted February 19, 2022 Author Share Posted February 19, 2022 17 minutes ago, marcelluss said: local cursorShowing = false bindKey("f2", "down", function() cursorShowing = not cursorShowing showCursor(cursorShowing) end) local nameColor = white local nameSize = 1.2 local nameFont = "pricedown" local ped = createPed(0, 0, 0, 3); addEventHandler( "onClientRender", getRootElement(), function( ) --for _, plr in ipairs( getElementsByType( "player" ) ) do -- if not (plr == localPlayer) then local x, y, z = getPedBonePosition( ped, 8 ) local sX, sY = getScreenFromWorldPosition( x, y, z ) if sX then --local id = setElementData(plr, "id") local text if id then text ="Player ["..id.."]" else text = "Player" end local leftX = sX- dxGetTextWidth(text, nameSize, nameFont )/2 local topY = sY-60 local rightX = leftX local buttonY = topY dxDrawText( text, leftX, topY, rightX, buttonY, nameColor, nameSize, nameSize, nameFont, "left", "top", false, false, false, false, false) end -- end --end local w, h = guiGetScreenSize () local tx, ty, tz = getWorldFromScreenPosition ( w/2, h/2, 50 ) local px, py, pz = getCameraMatrix(localPlayer) -- test line -- local line = dxDrawLine3D(px, py, pz, x, y, z) local hit, x, y, z, elementHit = processLineOfSight ( px, py, pz, tx, ty, tz ) if hit then if elementHit then local sX, sY = getScreenFromWorldPosition( x, y, z ) dxDrawText("G", sX, sY, sX, sY, tocolor(255, 255, 255), 1, "default") end end end ) If I understood your idea correctly, then here is the script You probably misunderstood a little. I want to make a player interaction system, but in your system you just need to refine and make sure that the ray does not touch my player. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now