You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FiveM:如何让玩家距离计算与DrawMarker半径匹配?

解决FiveM中DrawMarker视觉范围与区域检测不匹配的问题

核心问题:DrawMarker参数错误

你当前的DrawMarker写法存在致命错误——第12个参数(scaleZ)被错误设置成了coords.z + radius * 1.0,这个参数的作用是标记在Z轴的缩放比例,而非Z轴终点坐标。这直接导致视觉上的标记区域完全偏离预期,自然和距离计算无法匹配。

正确的DrawMarker设置(模拟圆柱区域)

如果你需要一个XY平面半径为radius,Z轴高度覆盖一定范围的圆柱视觉标记,正确的DrawMarker(类型1,立方体)写法应该是:

local coords = vector3(110.2, -1951.51, 100)
local radius = 100
local zHeight = 20 -- Z轴方向的高度,可根据需求调整

DrawMarker(
    1, 
    coords.x, coords.y, coords.z - zHeight/2, -- 标记的底部中心坐标
    0.0, 0.0, 0.0, -- 旋转角度
    0.0, 0.0, 0.0, -- 旋转轴
    radius * 2.0, radius * 2.0, zHeight, -- X/Y/Z缩放:X/Y是直径(半径*2),Z是高度
    255, 0, 0, 100, -- 颜色与透明度
    false, false, 2, false, nil, nil, false
)

如果你更倾向于圆形平面标记(比如地面的圆形区域),建议使用类型23(圆形区域标记),参数更直观:

DrawMarker(
    23,
    coords.x, coords.y, coords.z - 0.1, -- 标记位置(略低于地面避免穿模)
    0.0, 0.0, 0.0,
    0.0, 0.0, 0.0,
    radius * 2.0, radius * 2.0, 0.5, -- X/Y缩放为直径,Z缩放为厚度
    255, 0, 0, 100,
    false, false, 2, false, nil, nil, false
)

匹配视觉范围的区域检测逻辑

根据你选择的标记类型,检测逻辑分为两种:

  1. 圆柱区域检测(对应类型1的圆柱标记)
    需要同时满足两个条件:XY平面的距离≤radius,且玩家Z坐标在标记的Z范围内:

    local playerCoords = GetEntityCoords(PlayerPedId())
    -- 计算XY平面的距离(忽略Z轴)
    local distXY = #(vector2(playerCoords.x, playerCoords.y) - vector2(coords.x, coords.y))
    -- 检查Z轴范围(假设标记Z中心是coords.z,高度为zHeight)
    local isInZRange = playerCoords.z >= coords.z - zHeight/2 and playerCoords.z <= coords.z + zHeight/2
    
    if distXY <= radius and isInZRange then
        -- 玩家在圆柱区域内
        print("玩家在区域内")
    end
    
  2. 平面圆形区域检测(对应类型23的地面标记)
    只需要计算XY平面的距离≤radius即可(忽略Z轴,因为标记是贴地面的):

    local playerCoords = GetEntityCoords(PlayerPedId())
    local distXY = #(vector2(playerCoords.x, playerCoords.y) - vector2(coords.x, coords.y))
    
    if distXY <= radius then
        -- 玩家在平面圆形区域内
        print("玩家在区域内")
    end
    

额外说明

  • 如果你之前误用了三维距离(#(points - coords)或GetDistanceBetweenCoords的三维计算),这会把Z轴的高度差也计入距离,导致检测范围是一个球体,和视觉上的圆柱/平面圆形自然不匹配。
  • GetDistanceBetweenCoords的最后一个参数设为true时,会使用游戏内的地形距离(考虑高度差),设为false则是直线距离,按需选择即可。

内容的提问来源于stack exchange,提问作者Moxley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.02 00:23:11