Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local npc = script.Parent
- local humanoid = npc:WaitForChild("Humanoid")
- local root = npc:WaitForChild("HumanoidRootPart")
- local corner = workspace:WaitForChild("corner")
- local finish = workspace:WaitForChild("finish")
- -- Скорость рыцаря
- humanoid.WalkSpeed = 10
- -- Расстояние, при котором считаем точку достигнутой
- local REACH_DISTANCE = 2
- -- Поворачивает рыцаря лицом к нужной точке
- local function faceTarget(target)
- local direction = target.Position - root.Position
- direction = Vector3.new(direction.X, 0, direction.Z)
- if direction.Magnitude > 0.01 then
- root.CFrame = CFrame.lookAt(
- root.Position,
- root.Position + direction
- )
- end
- end
- -- Идти к точке
- local function walkTo(target)
- -- Сначала разворачиваемся в сторону точки
- faceTarget(target)
- humanoid:MoveTo(target.Position)
- -- Ждём, пока NPC дойдёт
- while true do
- local distance = (root.Position - target.Position).Magnitude
- if distance <= REACH_DISTANCE then
- break
- end
- -- Если NPC почему-то перестал двигаться,
- -- повторяем команду MoveTo
- humanoid:MoveTo(target.Position)
- task.wait(0.2)
- end
- -- Полностью останавливаемся
- humanoid:MoveTo(root.Position)
- end
- -- Бесконечный патруль
- while true do
- -- corner -> finish
- walkTo(finish)
- -- Небольшая пауза
- task.wait(0.5)
- -- Разворот и движение обратно
- walkTo(corner)
- -- Небольшая пауза
- task.wait(0.5)
- end
Advertisement
Add Comment
Please, Sign In to add comment