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

Fivem script how can i detect if an object lands on the ground

  1. User

    Fivem script how can i detect if an object lands on the ground

  2. Assistant

    To detect if an object lands on the ground in a FiveM script, you can use raycasting. Raycasting is a technique to detect collisions between objects in a 3D space.

    Here's an example of how to use raycasting to detect when an object lands on the ground in a FiveM script (in Lua):

    -- Replace "your_object" with the name of your object (entity)
    local your_object = GetEntityCoords(your_object)
    
    -- Set "distance_from_ground" as the distance value to consider the object as "landed" or "on the ground"
    local distance_from_ground = 1.0
    
    -- Perform raycasting
    local function checkObjectOnGround(obj_coords)
        local startPos = vector3(obj_coords.x, obj_coords.y, obj_coords.z)
        local endPos = vector3(obj_coords.x, obj_coords.y, obj_coords.z - distance_from_ground)
    
        local ray = StartShapeTestCapsule(startPos, endPos, 0.0, 10, your_object, 7)
        local _, _, _, _, groundEntity = GetShapeTestResult(ray)
    
        return groundEntity ~= 0
    end
    
    -- Main loop
    Citizen.CreateThread(function()
        while true do
            Citizen.Wait(0)
    
            local obj_coords = GetEntityCoords(your_object)
    
            if checkObjectOnGround(obj_coords) then
                -- The object has landed on the ground
                print("The object has landed on the ground!")
            else
                -- The object is still in the air
                print("The object is still in the air.")
            end
        end
    end)
    

    In this example, the checkObjectOnGround function returns true when the object is on the ground and false when it's not. The function uses the raycasting technique to check the distance between the object and the ground. The main loop keeps checking the object's position relative to the ground and provides the output accordingly.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.