【问题标题】:Tower-Defense Enemy AI movement roblox塔防敌人 AI 运动 roblox
【发布时间】:2021-03-25 01:25:10
【问题描述】:

有人可以帮助我演示如何让 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,但我无法移动整个模型。

video showing the problem

【问题讨论】:

  • 如果您使用寻路,移动到是要走的路。我建议您阅读 Roblox 在开发中心中关于寻路的教程。

标签: lua roblox


【解决方案1】:

您遇到了network ownership 的问题。 Roblox 引擎根据围绕谁最接近对象以及谁拥有足够强大的机器来进行计算的一些计算来决定谁负责计算对象的位置。例如,台式电脑和笔记本电脑的影响范围往往比移动设备更广。无论如何,当你走近 NPC 时,就会发生所有权的交接,这会导致 NPC 倒下。因此,要修复它,您需要在 NPC 的 PrimaryPart 上调用 SetNetworkOwner(nil),以便该部分归服务器所有。

npc.PrimaryPart:SetNetworkOwner(nil)

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

local targetPos = 1

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

-- listen for when the NPC arrives at a position    
npc.Humanoid.MoveToFinished:Connect(function(didArrive)
    -- check that the NPC was able to arrive at their target location
    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)
        -- do something to handle this bad case! Delete the npc?
        return
    end

    -- select the next target
    targetPos = targetPos + 1

    -- check if there are any more targets to move to
    if targetPos <= #source then
        move()
    else
        print("Done moving!")
    end
end)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多