Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

I'm working on a fivem lua script How can i detect if an object has landed on the ground

  1. User

    I'm working on a fivem lua script How can i detect if an object has landed on the ground

  2. Assistant

    To detect if an object has landed on the ground in a FiveM Lua script, you can use the native function GET_ENTITY_HEIGHT_ABOVE_GROUND which returns the Z-distance between the ground directly below the entity and the entity's position. We can check if this distance is approximately zero or a small threshold which means the object has likely landed on the ground.

    Here's an example:

    local object = -- YOUR OBJECT ENTITY
    local entityAboveGroundThreshold = 0.5 -- Can be adjusted as needed
    
    Citizen.CreateThread(function()
        while true do
            Citizen.Wait(0) -- Continuously iterate
    
            -- Get entity's height above ground
            local heightAboveGround = GetEntityHeightAboveGround(object)
    
            -- Check if the object is on the ground
            if heightAboveGround <= entityAboveGroundThreshold then
                print("Object has landed on the ground!") -- Do whatever you want when the object is on the ground
    
                break -- Exit the loop if you don't need to keep checking
            end
        end
    end)
    

    Keep in mind that this script will continuously check if the object has landed on the ground. Customize the example as needed depending on the specifics of your script.