Tower-Defense 敌人 AI 移动 roblox

有人可以帮我吗,怎么展示NPC沿着路径移动,就像 TDS 游戏中一样?我已经尝试过这样做

local function move()
        -- source is an array with all the positions it has to go to
    for i=1,#source,1 do
        -- This is in case the MoveTo takes more than 8 seconds
        local itreached = false
        while itreached == false do
            npc.Humanoid:MoveTo(source[i].Position)
            wait()
            npc.Humanoid.MoveToFinished:Connect(function()
                itreached = true
            end)
        end
    end
end

它在某种程度上有效,但当我接近 NPC 时,它会掉落和卡顿,否则如果没有玩家,它就可以正常运行。还有其他技术,像lerp或tween吗?我尝试使用 lerp,但我无法移动整个模型。

视频展示问题

点赞
用户2860267
用户2860267

你遇到了一个与网络所有权相关的问题。 Roblox引擎根据一些计算来决定谁负责计算物体的位置,这些计算是基于谁离物体最近以及谁的机器足够强大来进行的。例如,桌面计算机和笔记本电脑往往比移动设备具有更大的影响范围。无论如何,当你走近NPC时,就会发生所有权的移交,这会导致NPC倒下。因此,为了解决这个问题,你需要在NPC的主要部分上调用SetNetworkOwner(nil),以便该部分归服务器所有。

npc.PrimaryPart:SetNetworkOwner(nil)

此外,如果你想清理代码,你可以让它完全由事件驱动。一旦你告诉它开始移动,它就会在到达时选择下一个目标。

local targetPos = 1

local function move()
    npc.Humanoid:MoveTo(source[targetPos].Position)
end

--监听NPC到达位置的事件
npc.Humanoid.MoveToFinished:Connect(function(didArrive)
    --检查NPC是否能够到达目标位置
    if not didArrive then
        local errMsg = string.format("%s could not arrive at the target location : (%s, %s, %s)", npc.Name, tostring(targetPos.X), tostring(targetPos.Y), tostring(targetPos.Z))
        error(errMsg)
        --处理这种情况!删除NPC?
        return
    end

    --选择下一个目标
    targetPos = targetPos + 1

    --检查是否还有其他目标要移动
    if targetPos <= #source then
        move()
    else
        print("移动完成!")
    end
end)
2021-03-22 19:30:10