NPC 跟随该部分,但当我靠近 NPC 时,他们离开该部分并开始向随机方向移动
The NPC follows the part but when i go near the NPC they leave the part and start going to random directions
所以我试图让一个 npc 移动到一个部分,当我点击 npc 时,当我点击它时一切似乎都很好,直到我靠近 npc,那时它离开了这个部分并开始移动到随机方向。有什么问题吗?
脚本:
local value = script.Parent.FollowSurvivor
local value2 = workspace.Values.MoveToValue
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 1000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(0.5)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
if value.Value == true then
script.Parent.Zombie:MoveTo(target.Position, target)
end
if value.Value == false then
script.Parent.Zombie:MoveTo(workspace.MoveToPart.Position)
end
end
end
此脚本获取最近的类人动物并朝它走去。当你走到它旁边时,它发现了你的人形并开始跟着你。
所以我试图让一个 npc 移动到一个部分,当我点击 npc 时,当我点击它时一切似乎都很好,直到我靠近 npc,那时它离开了这个部分并开始移动到随机方向。有什么问题吗?
脚本:
local value = script.Parent.FollowSurvivor
local value2 = workspace.Values.MoveToValue
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 1000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(0.5)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
if value.Value == true then
script.Parent.Zombie:MoveTo(target.Position, target)
end
if value.Value == false then
script.Parent.Zombie:MoveTo(workspace.MoveToPart.Position)
end
end
end
此脚本获取最近的类人动物并朝它走去。当你走到它旁边时,它发现了你的人形并开始跟着你。