Jump to content

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


flomix77

Recommended Posts

Posted

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

  • Moderators
Posted

Well you can create a timer when onBotFollow is triggered and will call check3DDistance function every secs or 2 secs with the argument leader.

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

  • Moderators
Posted
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 ?

Posted

I need detect the distance between the player and ped if the player is 5.0 meters of distance of the ped, the ped kill be make an action...

sorry for my bad english.. but someone knows how to detect the distance everytime?

  • Moderators
Posted
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) 

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