如何让 roblox 中的 NPC 正常走动并在看到玩家后追踪他们

标题已经解释了一切,我试图让 roblox 中的 NPC 正常地围绕迷宫走动,然后当它看到玩家时,它开始追着玩家去杀它,我有 NPC,我有杀人部分,我只需要 NPC 正常行走的代码和 NPC 检测到玩家后追着他们的代码。谢谢! :D

编辑: 这是我的代码:

debugMode = false
targetNPCs = false

--

h = script.Parent.Parent:WaitForChild("Humanoid")
pathService = game:GetService("PathfindingService")
targetV = script.Parent:WaitForChild("Target")

function closestTargetAndPath()
 local humanoids = {}
 if targetNPCs then
 local function recurse(o)
 for _,obj in pairs(o:GetChildren()) do
 if obj:IsA("Model") then
 if obj:findFirstChild("Humanoid") and obj:findFirstChild("Torso") and obj.Humanoid ~= h and obj.Humanoid.Health > 0 and not obj:findFirstChild("ForceField") then
 table.insert(humanoids,obj.Humanoid)
 end
 end
 recurse(obj)
 end
 end
 recurse(workspace)
 else
 for _,v in pairs(game.Players:GetPlayers()) do
 if v.Character and v.Character:findFirstChild("HumanoidRootPart") and v.Character:findFirstChild("Humanoid") and v.Character.Humanoid.Health > 0 and not v:findFirstChild("ForceField") then
 table.insert(humanoids,v.Character.Humanoid)
 end
 end
 end
 local closest,path,dist
 for _,humanoid in pairs(humanoids) do
 local myPath = pathService:ComputeRawPathAsync(h.Torso.Position,humanoid.Torso.Position,500)
 if myPath.Status ~= Enum.PathStatus.FailFinishNotEmpty then
 -- 现在我们有了一个成功的路径,我们需要弄清楚我们实际上需要走多远才能到达这个点。
 local myDist = 0
 local previous = h.Torso.Position
 for _,point in pairs(myPath:GetPointCoordinates()) do
 myDist = myDist + (point-previous).magnitude
 previous = point
 end
 if not dist or myDist < dist then -- 如果为真,则这是到目前为止最近的路径。
 closest = humanoid
 path = myPath
 dist = myDist
 end
 end
 end
 return closest,path
end

function goToPos(loc)
 h:MoveTo(loc)
 local distance = (loc-h.Torso.Position).magnitude
 local start = tick()
 while distance > 4 do
 if tick()-start > distance/h.WalkSpeed then -- 可能出了些问题。只需要 break 掉即可。
 break
 end
 distance = (loc-h.Torso.Position).magnitude
 wait()
 end
end

while wait() do
 local target,path = closestTargetAndPath()
 local didBreak = false
 local targetStart
 if target and h.Torso then
 targetV.Value = target
 targetStart = target.Torso.Position
 roaming = false
 local previous = h.Torso.Position
 local points = path:GetPointCoordinates()
 local s = #points > 1 and 2 or 1
 for i = s,#points do
 local point = points[i]
 if didBreak then
 break
 end
 if target and target.Torso and target.Health > 0 then
 if (target.Torso.Position-targetStart).magnitude < 1.5 then
 local pos = previous:lerp(point,.5)
 local moveDir = ((pos - h.Torso.Position).unit * 2)
 goToPos(previous:lerp(point,.5))
 previous = point
 end
 else
 didBreak = true
 break
 end
 end
 else
 targetV.Value = nil
 end
 if not didBreak and targetStart then
 goToPos(targetStart)
 end
end
点赞
用户2858170
用户2858170

我不会提供任何代码,因为你还没有展示出解决这个问题的努力。

www.google.com 中输入 "roblox npc"。点击第二个链接:

https://developer.roblox.com/en-us/articles/Moving-NPCs-Between-Points

这篇文章涵盖了四处行走的部分并且还链接了这篇文章

https://developer.roblox.com/en-us/articles/Pathfinding

这篇文章涵盖了更复杂的动作。

看见目标的部分是通过投射光线实现的

https://developer.roblox.com/en-us/articles/Raycasting

如果你能从NPC向玩家方向投射一道光线,就可以计算出到达玩家的路径并进行攻击。

2021-03-27 19:19:19