shared
GetDistanceBetweenCoords
FiveM shared native documentation with a Lua example.
Summary
Returns the distance between two 3D points. Useful, though #(vec1 - vec2) is cheaper for simple distance checks in loops.
Returns
float — the distance.
Parameters
x1,y1,z1floatFirst point.x2,y2,z2floatSecond point.useZbooleanWhether to include the Z axis.Example
local d = GetDistanceBetweenCoords(x1,y1,z1, x2,y2,z2, true)
-- cheaper alternative in tight loops:
local d2 = #(vector3(x1,y1,z1) - vector3(x2,y2,z2))Common pitfalls
- For per-frame checks, prefer #(a - b) — it's faster.
- Set useZ true when height matters (e.g. multi-floor buildings).
Related natives
Related guides
FiveM scripts done right
Optimized, dual-framework scripts for QBCore and ESX from Viper Development — escrow protected, delivered instantly to your Cfx portal.
Browse Scripts →