PAYATHON 2026

FiveM 中如何让玩家距离判定与 DrawMarker 的圆形范围一致?

数据小许

判断玩家是否处于地面上的圆形范围内,可以计算玩家到圆心的 X-Y 二维距离,再与 radius 比较。对于这里未旋转的默认 type 1 圆柱标记,水平尺寸按直径设置,即 scaleX = scaleY = radius * 2.0。如果还要限制高度,需要另外检查 Z 范围。

原代码混用了立方体、球体和圆柱的判定方式。另外,scaleZ 接收的是缩放值,不能把它当作顶部的世界坐标。

为什么几种距离计算会得到不同结果?

DrawMarker(1, ...) 使用的是 MarkerTypeVerticalCylinder,也就是竖直圆柱,水平截面为圆形。FiveM 标记类型说明

原代码中的算法对应不同形状:

  • dx <= radius and dy <= radius and dz <= radius:检查轴对齐立方体。
  • math.max(dx, dy, dz) <= radius:同样检查轴对齐立方体。
  • math.sqrt(dx * dx + dy * dy + dz * dz) <= radius:检查球体。
  • GetDistanceBetweenCoords(..., true) <= radius:包含 Z 差值,检查球体。
  • #(points - coords) <= radius:计算三维向量长度,也是球体判定。

后三种距离在数学上等价,不应因为换了函数就出现明显差异。FiveM 的参考说明区分了包含 Z 的三维距离和忽略 Z 的二维距离。GetDistanceBetweenCoords 说明

例如,水平距离为 90、高度差为 50 时,三维距离为:

sqrt(90² + 50²) ≈ 102.96

半径为 100 时,这个位置在水平圆内,却在球体外。因此,玩家看起来可能已经进入圆圈,判定结果却仍在范围外。

只检查圆形范围时,使用二维距离

可以直接计算二维距离:

local points = GetEntityCoords(PlayerPedId())
local coords = vector3(110.2, -1951.51, 100)
local radius = 100.0

local distance = #(points.xy - coords.xy)
local inside = distance <= radius

也可以使用 native,并把最后一个参数设为 false:

local distance = GetDistanceBetweenCoords(
    points.x, points.y, points.z,
    coords.x, coords.y, coords.z,
    false
)

local inside = distance <= radius

如果只需要布尔结果,可以比较距离平方,省去开平方:

local dx = points.x - coords.x
local dy = points.y - coords.y
local inside = dx * dx + dy * dy <= radius * radius

这三种写法表达的是同一个水平圆形范围,选择一种即可,不需要用 or 混合多种算法。

让绘制参数和区域判定共用同一组配置

DrawMarker 的位置参数和缩放参数用途不同:posZ 决定绘制位置,scaleZ 控制 Z 轴缩放。coords.z + radius 是世界坐标形式的表达式,用它表示高度尺寸会让标记尺寸随海拔变化。DrawMarker 参数说明

原例中 coords.z = 100、radius = 100,因此 coords.z + radius 恰好等于 radius * 2.0。这是数值巧合,不能作为通用计算方式。

下面的示例保留原来的圆心和水平半径,并明确设置高度上下界,范围为 coords.z ± radius。实际项目可以独立配置高度:

local coords = vector3(110.2, -1951.51, 100)
local radius = 100.0
local diameter = radius * 2.0

local minZ = coords.z - radius
local maxZ = coords.z + radius
local markerHeight = maxZ - minZ

CreateThread(function()
    while true do
        Wait(0)

        local points = GetEntityCoords(PlayerPedId())
        local dx = points.x - coords.x
        local dy = points.y - coords.y
        local insideCircle = dx * dx + dy * dy <= radius * radius

        DrawMarker(
            1,
            coords.x, coords.y, minZ,
            0.0, 0.0, 0.0,
            0.0, 0.0, 0.0,
            diameter, diameter, markerHeight,
            255, 0, 0, 100,
            false, false, 2, false, nil, nil, false
        )

        if insideCircle then
            -- Handle the player being inside the horizontal circle.
        end
    end
end)

这里的 insideCircle 只判断水平范围,不受玩家高度影响。DrawMarker 需要在显示期间逐帧调用,所以绘制放在 Wait(0) 循环中;玩家坐标也在循环内更新。DrawMarker 调用要求

如果需要完整圆柱区域,再增加高度检查

如果玩家站在区域上方的桥梁或另一层楼时不应算进入,可以在二维圆形条件之外增加上下界:

local insideHeight = points.z >= minZ and points.z <= maxZ
local insideCylinder = insideCircle and insideHeight

这样判定的区域是半径固定、具有独立高度范围的圆柱。直接改回三维距离会得到球体,其水平截面在接近顶部和底部时会缩小,无法表达同样的圆柱范围。

边界仍有偏差时检查什么?

先检查圆心水平坐标、半径和绘制尺寸是否来自同一组配置,再检查玩家坐标是否持续更新。如果只调用一次 GetEntityCoords(PlayerPedId()),后续使用的仍是那次采样的位置。

对于当前未旋转、scaleX = scaleY 的 type 1 标记,可以用圆心沿 X、Y 方向各偏移 radius 的位置检查边缘。默认 type 1 的直径设置方式不应直接推广到所有 marker 类型;官方参数说明只将这些参数定义为各轴缩放,不保证不同图形拥有相同的几何边界。

还要区分玩家实体坐标和角色模型的可见轮廓。上述代码检查的是 GetEntityCoords 返回的位置点,而不是角色身体是否与区域相交。斜坡和地形遮挡也可能影响看到的标记边缘,排查时应先在平坦、无遮挡的位置核对。

备注:内容仅供参考。