Jump to content

How To detect if ped is near a player / player is near a ped


flomix77

Recommended Posts

Thanks but now i have another question, i have this code:

function check3DDistance(leader) 
    local pX, pZ, pY  = getElementPosition( leader ) 
    local bX, bZ, bY  = getElementPosition( source )     
    local distancia = getDistanceBetweenPoints3D ( pX, pZ, pY, bX, bZ, bY ) 
    outputChatBox ( "the distance between "..getPlayerName(leader).." and the zombie is of "..tonumber(distancia)) 
end 
addEventHandler("onBotFollow", getRootElement(), check3DDistance) 

but dont works.. what "callback" i can use to check the distance when the ped is moving..?

Link to comment
function check3DDistance(leader) 
    local pX, pZ, pY  = getElementPosition( leader ) 
    local bX, bZ, bY  = getElementPosition( source )     
    outputChatBox ( "the distance between "..getPlayerName(leader).." and the zombie is of "..getDistanceBetweenPoints3D ( pX, pZ, pY, bX, bZ, bY ) ) 
end 
addEventHandler("onBotFollow", getRootElement(), check3DDistance) 

Link to comment
  • Moderators
function check3DDistance(leader) 
    local pX, pZ, pY  = getElementPosition( leader ) 
    local bX, bZ, bY  = getElementPosition( source )     
    outputChatBox ( "the distance between "..getPlayerName(leader).." and the zombie is of "..getDistanceBetweenPoints3D ( pX, pZ, pY, bX, bZ, bY ) ) 
end 
addEventHandler("onBotFollow", getRootElement(), check3DDistance) 

What's the point of doing that ? to make this line even longer ?

Link to comment
  • Moderators
function onBotTargeting( player ) 
    setElementData( source, "playerTargeted", player) 
end 
addEventHandler("onBotFollow", getRootElement(), onBotTargeting) 
  
function check3DDistance(leader) 
    for k, bot in ipairs ( getElementsByType("ped") ) do 
        local targetedPlayer = getElementData(bot, "targetedPlayer") 
        if targetedPlayer then 
            local pX, pZ, pY  = getElementPosition( targetedPlayer ) 
            local bX, bZ, bY  = getElementPosition( bot ) 
            local distance = getDistanceBetweenPoints3D ( pX, pZ, pY, bX, bZ, bY ) 
            if distance < 5 then 
                --do your stuff here 
                outputChatBox ( "the distance between "..getPlayerName(leader).." and a zombie is under 5 ("..distance..")" ) 
            end 
        end 
    end 
end 
setTimer(check3DDistance, 1000, 0) 

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