Jump to content

How to get the player the camera is on?


Swimer

Recommended Posts

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
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
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
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
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...