首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >塔-防御敌人AI运动roblox

塔-防御敌人AI运动roblox
EN

Stack Overflow用户
提问于 2021-03-22 14:26:05
回答 1查看 270关注 0票数 0

有没有人可以帮我演示一下如何像tds游戏中那样通过路径移动NPCs?我已经试过了

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

发布于 2021-03-23 03:30:11

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

代码语言:javascript
运行
复制
npc.PrimaryPart:SetNetworkOwner(nil)

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

代码语言:javascript
运行
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66741217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档