Fivem script how can i detect if an object lands on the ground
- User
Fivem script how can i detect if an object lands on the ground
- Assistant
In a FiveM script, you can use the ESX framework and collision events to detect whether an object with a specific model has landed on the ground. Here's an example script on how you could achieve this:
local objectModel = 'prop_box_wood02a' -- Replace with the object model you want to check local objectThresholdZ = 0.95 -- The minimum Z-axis value to consider that the object is on the ground local object = nil local objectCoord = nil Citizen.CreateThread(function() while true do Citizen.Wait(0) -- Always add a wait in your main loop to prevent freezing local playerPed = PlayerPedId() local playerCoord = GetEntityCoords(playerPed) local objectFound, object = FindFirstObject() if DoesEntityExist(object) then if GetEntityModel(object) == GetHashKey(objectModel) then objectCoord = GetEntityCoords(object) local groundZ, success = GetGroundZAtCoords(objectCoord) if success and (objectCoord.z - groundZ) <= objectThresholdZ then print(object .. ' is on the ground') -- Trigger your action here end end end end end) - User
GetGroundZAtCoords not a native
- Assistant
You're right, I apologize for the mistake. The correct native function is
GET_GROUND_Z_FOR_3D_COORD. Here's the updated script with the correct native function:local objectModel = 'prop_box_wood02a' -- Replace with the object model you want to check local objectThresholdZ = 0.95 -- The minimum Z-axis value to consider that the object is on the ground local object = nil local objectCoord = nil Citizen.CreateThread(function() while true do Citizen.Wait(0) -- Always add a wait in your main loop to prevent freezing local playerPed = PlayerPedId() local playerCoord = GetEntityCoords(playerPed) local objectFound, object = FindFirstObject() if DoesEntityExist(object) then if GetEntityModel(object) == GetHashKey(objectModel) then objectCoord = GetEntityCoords(object) local groundZResult, groundZ = GetGroundZFor_3dCoord(objectCoord.x, objectCoord.y, objectCoord.z) if groundZResult and (objectCoord.z - groundZ) <= objectThresholdZ then print(object .. ' is on the ground') -- Trigger your action here end end end end end)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.