[How-To] ACTUALLY place entities on ground correctly from far distances

Place properly on ground in unrendered zones?

Yes!

I’ve seen tutorials and how-tos on using GetGroundZFor_3dCoord and PlaceObjectOnGroundProperly, however as I’ve encountered, GetGroundZFor_3dCoord will only work if the area you’re trying to get is within your render distance.

I guess you could call this a sort of bootleg work around, but it gets the job done and honestly I don’t know if there’s another way (correct me if there is).

Using SetFocusPosAndVel(), we can move our render camera to any spot around the map, allowing the GetGroundCoord... native to retrieve the Z coord.

local ground_check = 0.0 -- dont change (beginning z coord check)
local mapCoords = {x = 123.00, y = 456.00, z = "Theres no z :("}

SetFocusPosAndVel(mapCoords.x, mapCoords.y, ground_check)
local ground, z = GetGroundZFor_3dCoord(mapCoords.x, mapCoords.y, 999.0, 0) -- Haven't experimented with changing the 999.0, I don't think it should do anything different if changed.
-- while there is no ground and the check coord has not exceeded 800:
while not ground and ground_check <= 800.0 do -- no terrain in the game exceeds z=800
    ground_check = ground_check + 40.0 -- increment the z coord by 40 if no ground was found (make this lower if youre having trouble getting areas)
    SetFocusPosAndVel(mapCoords.x, mapCoords.y, ground_check) -- Set the camera render at the new z coord
    ground, z = GetGroundZFor_3dCoord(mapCoords.x, mapCoords.y, 999.0, 1) -- try to get the ground and z coord at this camera render z coord.
    Citizen.Wait(10)
end

ClearFocus() -- Stop rendering there and render here!
ground_check = 0.0 -- reset the ground check if you're running this loop again

if ground then
    print("I found the ground Z coord!", z)
    mapCoords.z = z -- append the object if you want...
end
Natives

SetFocusPosAndVel - FiveM Natives @ Cfx.re Docs
ClearFocus - FiveM Natives @ Cfx.re Docs

Some notes:

This will not work 100% of the time!!! I’ve noticed some issues when it comes to water, if you care to investigate then go for it, otherwise you could make a thread to increase the z coord until the entitiy is out of the water, (or something…)

The clients render distance is a factor in this too, the lower it is, the less likely it is for the loop to find the ground. The way the loop works (if you haven’t looked at it for some reason?) is if no ground is found, it will increase the render Z coord by 40. If you’re rendering a really short distance, you may have to lower that 40 to maybe 20 or 30.

You will notice the map around you will be really shit quality (for less than a second) while it attempts to find the Z coord, obviously because it’s rendering the map elsewhere. I’m sure theres a more efficient way of doing this to minimize the time it takes to find the coord, however my example is bare bones.

3 Likes