【问题标题】:How to make NPC in roblox walk around normally then when it sees a player it runs after them如何让roblox中的NPC正常四处走动,然后当它看到玩家追赶他们时
【发布时间】:2021-06-24 07:42:47
【问题描述】:

标题说明了一切,我试图让一个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
 -- Now that we have a successful path, we need to figure out how far we need to actually travel to reach this point.
 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 -- if true, this is the closest path so far.
 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 -- Something may have gone wrong. Just 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

【问题讨论】:

  • 嘿嘿,StackOverflow 旨在帮助您调试代码中的问题。你试过什么让你的NPC四处走动?什么不工作?请使用您尝试过的代码更新您的问题,我们可以帮助您调试问题。
  • 请阅读How to Ask

标签: lua roblox


【解决方案1】:

我不会提供任何代码,因为您没有表现出任何解决该问题的努力。

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 向你的玩家投射光线,请计算出该玩家的路径并进行攻击。

【讨论】:

  • 也可以使用幅度。
猜你喜欢
  • 2018-12-16
  • 2021-07-24
  • 2019-02-17
  • 2021-07-27
  • 1970-01-01
  • 2022-06-21
  • 1970-01-01
  • 2021-01-13
  • 2021-04-28
相关资源
最近更新 更多