有没有人可以帮我演示一下如何像tds游戏中那样通过路径移动NPCs?我已经试过了
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,但我无法移动整个模型。
发布于 2021-03-23 03:30:11
您遇到了network ownership的问题。Roblox引擎根据谁离对象最近以及谁拥有足够强大的机器来计算对象的位置,来决定谁负责计算对象的位置。例如,台式计算机和笔记本电脑往往比移动设备具有更广泛的影响范围。无论如何,当你走近NPCs时,就会发生所有权交接,这会导致NPCs倒下。因此,要修复它,您需要在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)
https://stackoverflow.com/questions/66741217
复制相似问题