✓ Accepted answer
roblox has a pathfinding service such that u dont need to waste ur time learning one(not that i discourage). Place this in a script in the Bear:
local root = script.Parent.HumanoidRootPart
local hum = script.Parent.Humanoid
function findTarget()
local dist = 200
local target = nil
for i, v in pairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local head = v:FindFirstChild("Head")
if human and head and v.Name ~= "Bear" then
local mag = (head.Position - root.position).Magnitude
if mag < dist and human.Health > 0 then
dist = mag
target = head
end
end
end
return target
end
while wait(.1) do
local head = findTarget()
if head then
local ray = Ray.new(root.Position, (head.Position - root.Position).Unit * 200)
local hit, pos = workspace:FindPartOnRay(ray, script.Parent, true, false)
if hit:IsDescendantOf(head.Parent) then
hum:MoveTo(head.Position)
else
hum:MoveTo(head.Position)
wait(.05)
path = game:GetService("PathfindingService"):FindPathAsync(root.Position, head.Position)
points = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i, v in pairs(points) do
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait(2)
if v.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
if (points[#points].Position - head.Position).Magnitude > 15 then
break
end
end
else
hum:MoveTo(head.Position)
end
end
end
end
2 comments
This is not a request site. Read here to learn how to do this independently, and come back here when you need help with your code. http://wiki.roblox.com/index.php?title=Pathfinding
Thanks!