Jump to content

player follow player


Monty

Recommended Posts

Example of "follow" function:

--Server Side 
  
function addPed(thePlayer) 
  
    local x, y, z = getElementPosition( thePlayer ); 
    x = x + math.cos( math.rad( getPlayerRotation( thePlayer ) ) ) * 2; 
    y = y - math.sin( math.rad( getPlayerRotation( thePlayer ) ) ) * 2; 
     
     
    if (thePed) then 
        destroyElement(thePed) 
    end 
     
    thePed = createPed ( math.random( 10, 270 ), x, y, z) 
    triggerClientEvent ( "followPlayer", Ped, thePlayer, true ) 
end 
addCommandHandler ( "ped", addPed ) 
  
  
  
  
  
--Client Side 
  
local followTo  
local follower 
addEvent("followPlayer", true) 
addEventHandler("followPlayer", root, 
function (thePlayer, value) 
    if (isElement(source) and isElement(thePlayer) ) then 
        if (value) then 
            followTo = thePlayer 
            follower = source 
            removeEventHandler( "onClientRender", root, FollowToPlayer) 
            addEventHandler( "onClientRender", root, FollowToPlayer) 
        else 
            removeEventHandler( "onClientRender", root, FollowToPlayer) 
        end 
    end 
end) 
  
function FollowToPlayer() 
    local ped = follower --Ped who has to follow the player 
    local plr = followTo -- The Player 
    if ped then 
        local x, y, z = getElementPosition( plr ); 
        local px, py, pz = getElementPosition( ped ); 
        local distance = getDistanceBetweenPoints3D( x, y, z, px, py, pz ) 
        if distance < 3.5 then 
            setPedControlState( ped, "forwards", false ); 
            if ( isPedDucked( plr ) ~= isPedDucked( ped ) ) then 
                setPedControlState( ped, "crouch", isPedDucked( getLocalPlayer( ) )); 
                return 
            end 
        else 
            setPedControlState( ped, "forwards", true ); 
            if distance < 6.0 then 
                if isPedDucked( plr ) ~= isPedDucked( ped ) then 
                    setPedControlState( ped, "crouch", false ); 
                    setPedControlState( ped, "crouch", true ); 
                    setPedControlState( ped, "walk", false ); 
                    setPedControlState( ped, "sprint", false ); 
                    return 
                else 
                    setPedControlState( ped, "walk", true ); 
                    setPedControlState( ped, "sprint", false ); 
                end 
            elseif distance < 10.0 then 
                setPedControlState( ped, "walk", false ); 
                setPedControlState( ped, "sprint", true ); 
            else 
                setPedControlState( ped, "walk", false ); 
                setPedControlState( ped, "sprint", true ); 
            end 
             
            local X, Y = 0, 0; 
             
            X = math.abs( x - px ); 
            Y = math.abs( y - py ); 
            faceMe = math.deg( math.atan2( Y , X ) ); 
  
             
            if ( x >= px ) and ( y > py ) then      -- north-east 
                faceMe = 90 - faceMe 
            elseif ( x <= px ) and ( y > py ) then  -- north-west 
                faceMe = 270 + faceMe 
            elseif ( x >= px ) and ( y <= py ) then -- south-east 
                faceMe = 90 + faceMe 
            elseif ( x < px ) and ( y <= py ) then  -- south-west 
                faceMe = 270 - faceMe 
            end 
  
            setPedRotation( ped, faceMe ); 
            setPedLookAt( ped, x, y, z + .5 ); 
        end 
    end 
  
end 
  
  
  
_setPedRotation = setPedRotation 
function setPedRotation( ped, rot ) 
    return _setPedRotation( ped, 360 - rot ); 
end 
  

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