mirror of
https://github.com/qbcore-fivem/qb-core.git
synced 2026-08-28 17:01:16 +00:00
LookAtEntity
The QBCore.Functions.LookAtEntity function in Lua makes a player's character turn to face a specified entity in the game. It checks if the entity exists and if the input parameters are valid. It then calculates the direction to the entity and gradually adjusts the player's heading to match this direction. The function stops either when the player is facing the entity or when the specified timeout is reached.
This commit is contained in:
@@ -52,6 +52,55 @@ function QBCore.Functions.RequestAnimDict(animDict)
|
||||
end
|
||||
end
|
||||
|
||||
---@param entity number - The entity to look at
|
||||
---@param timeout number - The time in milliseconds before the function times out
|
||||
---@param speed number - The speed at which the entity should turn
|
||||
---@return number - The time at which the entity was looked at
|
||||
function QBCore.Functions.LookAtEntity(entity, timeout, speed)
|
||||
local involved = GetInvokingResource()
|
||||
if not DoesEntityExist(entity) then turnPromise:reject(involved..' :^1 Entity does not exist') return turnPromise.value end
|
||||
if not type(entity) == 'number' then turnPromise:reject(involved..' :^1 Entity must be a number') return turnPromise.value end
|
||||
if not type(speed) == 'number' then turnPromise:reject(involved..' :^1 Speed must be a number') return turnPromise.value end
|
||||
if speed > 5.0 then speed = 5.0 end
|
||||
if timeout > 5000 then timeout = 5000 end
|
||||
|
||||
local ped = PlayerPedId()
|
||||
local playerPos = GetEntityCoords(ped)
|
||||
|
||||
local targetPos = GetEntityCoords(entity)
|
||||
local dx = targetPos.x - playerPos.x
|
||||
local dy = targetPos.y - playerPos.y
|
||||
local targetHeading = GetHeadingFromVector_2d(dx, dy)
|
||||
|
||||
local turnSpeed = speed
|
||||
local startTimeout = GetGameTimer()
|
||||
while true do
|
||||
local currentHeading = GetEntityHeading(ped)
|
||||
local diff = targetHeading - currentHeading
|
||||
if math.abs(diff) < 2 then
|
||||
break
|
||||
end
|
||||
|
||||
if diff < -180 then
|
||||
diff = diff + 360
|
||||
elseif diff > 180 then
|
||||
diff = diff - 360
|
||||
end
|
||||
|
||||
turnSpeed = speed + (2.5 - speed) * (1 - math.abs(diff) / 180)
|
||||
|
||||
if diff > 0 then
|
||||
currentHeading = currentHeading + turnSpeed
|
||||
else
|
||||
currentHeading = currentHeading - turnSpeed
|
||||
end
|
||||
SetEntityHeading(ped, currentHeading)
|
||||
Wait(0)
|
||||
if (startTimeout + timeout) < GetGameTimer() then break end
|
||||
end
|
||||
SetEntityHeading(ped, targetHeading)
|
||||
end
|
||||
|
||||
-- Function to run an animation
|
||||
--- @param animDic string: The name of the animation dictionary
|
||||
--- @param animName string - The name of the animation within the dictionary
|
||||
|
||||
Reference in New Issue
Block a user