Jump to content

[HELP] Animation chasing a player.


Ryuto

Recommended Posts

Hello how are you?

 

I want to ask if it is possible to use an animation that chases a nearby player that is being targeted.

I give an example:

Pressing the "1" key while selecting a nearby player activates Woozie's running animation and then begins chasing the player for a certain amount of time.

 

I give an example of what I'm looking for...

 

 

and this is the code to target a player.

 

local playerSelectedTarget
local alpha = 255


addEventHandler("onClientRender", root,
    function()
        if ( getKeyState("mouse2") == true ) then
            local x,y,z = getElementPosition(localPlayer)
             for k,target in ipairs(getElementsByType("player",root,true)) do
                if not ( target == localPlayer ) then
                    local px,py,pz = getElementPosition(target)    
                    local distance = getDistanceBetweenPoints3D( px, py, pz, x, y, z )
                    if playerSelectedTarget ~= localPlayer and ( distance < 30 ) then --target distanc
                        playerSelectedTarget = target
                        local alpha = 255
                        setElementData(localPlayer, "Select", true)
                    end
                end    
            end
            if isElement(playerSelectedTarget) then        
                local Px,Py,Pz = getCameraMatrix( )
                local zhx,zhy,zhz = getPedBonePosition(playerSelectedTarget,3)
                local dist = getDistanceBetweenPoints3D ( Px, Py, Pz, zhx,zhy,zhz )
                if playerSelectedTarget ~= localPlayer and ( dist < 30 ) then --target distanc
                if ( isLineOfSightClear ( Px, Py, Pz, zhx,zhy,zhz, true, true, false, false, true, false, false, playerSelectedTarget ) ) then
                local sxx,syy = getScreenFromWorldPosition(zhx,zhy,zhz+0.3)
                if sxx and syy then                        
                 local size = 1300  / dist
                   dxDrawImage( sxx - ( size / 2 ), syy - ( size / 4.5 ), size, size, "Effect/Select.webp", 0, 0, 0, tocolor ( 255, 255, 255, alpha ),false) 
                   end
                end
             end
            end
        else
            if isElement(playerSelectedTarget) then        
            local alpha = 0
            setElementData(localPlayer, "Select", false)
            end    
        end        
    end
)

 

thanks for reading this post.

Link to comment
6 hours ago, Ryuto said:

Hello how are you?

 

I want to ask if it is possible to use an animation that chases a nearby player that is being targeted.

I give an example:

Pressing the "1" key while selecting a nearby player activates Woozie's running animation and then begins chasing the player for a certain amount of time.

 

I give an example of what I'm looking for...

 

 

and this is the code to target a player.

 

local playerSelectedTarget
local alpha = 255


addEventHandler("onClientRender", root,
    function()
        if ( getKeyState("mouse2") == true ) then
            local x,y,z = getElementPosition(localPlayer)
             for k,target in ipairs(getElementsByType("player",root,true)) do
                if not ( target == localPlayer ) then
                    local px,py,pz = getElementPosition(target)    
                    local distance = getDistanceBetweenPoints3D( px, py, pz, x, y, z )
                    if playerSelectedTarget ~= localPlayer and ( distance < 30 ) then --target distanc
                        playerSelectedTarget = target
                        local alpha = 255
                        setElementData(localPlayer, "Select", true)
                    end
                end    
            end
            if isElement(playerSelectedTarget) then        
                local Px,Py,Pz = getCameraMatrix( )
                local zhx,zhy,zhz = getPedBonePosition(playerSelectedTarget,3)
                local dist = getDistanceBetweenPoints3D ( Px, Py, Pz, zhx,zhy,zhz )
                if playerSelectedTarget ~= localPlayer and ( dist < 30 ) then --target distanc
                if ( isLineOfSightClear ( Px, Py, Pz, zhx,zhy,zhz, true, true, false, false, true, false, false, playerSelectedTarget ) ) then
                local sxx,syy = getScreenFromWorldPosition(zhx,zhy,zhz+0.3)
                if sxx and syy then                        
                 local size = 1300  / dist
                   dxDrawImage( sxx - ( size / 2 ), syy - ( size / 4.5 ), size, size, "Effect/Select.webp", 0, 0, 0, tocolor ( 255, 255, 255, alpha ),false) 
                   end
                end
             end
            end
        else
            if isElement(playerSelectedTarget) then        
            local alpha = 0
            setElementData(localPlayer, "Select", false)
            end    
        end        
    end
)

 

thanks for reading this post.

local playerSelectedTarget
local alpha = 255
local isChasing = false
local chaseDuration = 10000 

addEventHandler("onClientKey", root,
    function(key, state)
        if state and key == "1" and isElement(playerSelectedTarget) then
            startChasingPlayer()
        end
    end
)

function startChasingPlayer()
    if not isChasing then
        isChasing = true
        setElementData(localPlayer, "Select", false) 
        setPedAnimation(localPlayer, "ped", "run_player", -1, true, false, false) -- Change "run_player" to the animation name you want !!!!!
        setTimer(stopChasingPlayer, chaseDuration, 1)
    end
end

function stopChasingPlayer()
    isChasing = false
    setPedAnimation(localPlayer) 
end

addEventHandler("onClientRender", root,
    function()
        if (getKeyState("mouse2") == true) then
            local x, y, z = getElementPosition(localPlayer)
            for k, target in ipairs(getElementsByType("player", root, true)) do
                if not (target == localPlayer) then
                    local px, py, pz = getElementPosition(target)
                    local distance = getDistanceBetweenPoints3D(px, py, pz, x, y, z)
                    if playerSelectedTarget ~= localPlayer and (distance < 30) then
                        playerSelectedTarget = target
                        alpha = 255
                        setElementData(localPlayer, "Select", true)
                    end
                end
            end
            if isElement(playerSelectedTarget) then
                local Px, Py, Pz = getCameraMatrix()
                local zhx, zhy, zhz = getPedBonePosition(playerSelectedTarget, 3)
                local dist = getDistanceBetweenPoints3D(Px, Py, Pz, zhx, zhy, zhz)
                if playerSelectedTarget ~= localPlayer and (dist < 30) then
                    if (isLineOfSightClear(Px, Py, Pz, zhx, zhy, zhz, true, true, false, false, true, false, false, playerSelectedTarget)) then
                        local sxx, syy = getScreenFromWorldPosition(zhx, zhy, zhz + 0.3)
                        if sxx and syy then
                            local size = 1300 / dist
                            dxDrawImage(sxx - (size / 2), syy - (size / 4.5), size, size, "Effect/Select.webp", 0, 0, 0, tocolor(255, 255, 255, alpha), false)
                        end
                    end
                end
            end
        else
            if isElement(playerSelectedTarget) then
                alpha = 0
                setElementData(localPlayer, "Select", false)
            end
        end

        if isChasing and isElement(playerSelectedTarget) then
            local px, py, pz = getElementPosition(localPlayer)
            local targetX, targetY, targetZ = getElementPosition(playerSelectedTarget)
            local angle = math.atan2(targetY - py, targetX - px)
            local speed = 0.1 

            local newX = px + math.cos(angle) * speed
            local newY = py + math.sin(angle) * speed

            setElementPosition(localPlayer, newX, newY, pz)
        end
    end
)

 

Edited by Sr.black
Link to comment
On 24/12/2023 at 19:15, Sr.black said:
local playerSelectedTarget
local alpha = 255
local isChasing = false
local chaseDuration = 10000 

addEventHandler("onClientKey", root,
    function(key, state)
        if state and key == "1" and isElement(playerSelectedTarget) then
            startChasingPlayer()
        end
    end
)

function startChasingPlayer()
    if not isChasing then
        isChasing = true
        setElementData(localPlayer, "Select", false) 
        setPedAnimation(localPlayer, "ped", "run_player", -1, true, false, false) -- Change "run_player" to the animation name you want !!!!!
        setTimer(stopChasingPlayer, chaseDuration, 1)
    end
end

function stopChasingPlayer()
    isChasing = false
    setPedAnimation(localPlayer) 
end

addEventHandler("onClientRender", root,
    function()
        if (getKeyState("mouse2") == true) then
            local x, y, z = getElementPosition(localPlayer)
            for k, target in ipairs(getElementsByType("player", root, true)) do
                if not (target == localPlayer) then
                    local px, py, pz = getElementPosition(target)
                    local distance = getDistanceBetweenPoints3D(px, py, pz, x, y, z)
                    if playerSelectedTarget ~= localPlayer and (distance < 30) then
                        playerSelectedTarget = target
                        alpha = 255
                        setElementData(localPlayer, "Select", true)
                    end
                end
            end
            if isElement(playerSelectedTarget) then
                local Px, Py, Pz = getCameraMatrix()
                local zhx, zhy, zhz = getPedBonePosition(playerSelectedTarget, 3)
                local dist = getDistanceBetweenPoints3D(Px, Py, Pz, zhx, zhy, zhz)
                if playerSelectedTarget ~= localPlayer and (dist < 30) then
                    if (isLineOfSightClear(Px, Py, Pz, zhx, zhy, zhz, true, true, false, false, true, false, false, playerSelectedTarget)) then
                        local sxx, syy = getScreenFromWorldPosition(zhx, zhy, zhz + 0.3)
                        if sxx and syy then
                            local size = 1300 / dist
                            dxDrawImage(sxx - (size / 2), syy - (size / 4.5), size, size, "Effect/Select.webp", 0, 0, 0, tocolor(255, 255, 255, alpha), false)
                        end
                    end
                end
            end
        else
            if isElement(playerSelectedTarget) then
                alpha = 0
                setElementData(localPlayer, "Select", false)
            end
        end

        if isChasing and isElement(playerSelectedTarget) then
            local px, py, pz = getElementPosition(localPlayer)
            local targetX, targetY, targetZ = getElementPosition(playerSelectedTarget)
            local angle = math.atan2(targetY - py, targetX - px)
            local speed = 0.1 

            local newX = px + math.cos(angle) * speed
            local newY = py + math.sin(angle) * speed

            setElementPosition(localPlayer, newX, newY, pz)
        end
    end
)

 

Thank you very much, I am getting closer to what I am looking for.

but I have two problems...
 The first is that I add an animation to the script code, but when the link is played, the character appears completely static without any animation.

And the second is that when I use the bind while at height, the character goes to the opponent's position but while at height and not to the position on the ground.

Here I leave a video of the aforementioned errors so that it is easier to understand them.

 

 

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