I'm making a game where you walk around a forest and hunt bears. As it is a forest, there are trees that get in the way. The problem with NPC's is that they walk into the wall and do not notice barriers. I really need help on this. Are there any people out there who know how to use AI on Roblox it would help me so much!
I understand that this is a lot. But thanks!
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() -- this is the function that would locate the nearest human; we will constantly be spamming this. 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 -- u could remove this if u want ur bear to attack other bears 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) -- ignore list if hit:IsDescendantOf(head.Parent) then hum:MoveTo(head.Position) else hum:MoveTo(head.Position) -- less choppy movement; a lot of the following is also for such wait(.05) path = game:GetService("PathfindingService"):FindPathAsync(root.Position, head.Position) -- get the Pathfinding service 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) -- 2 as a safety precaution if v.Action == Enum.PathWaypointAction.Jump then -- if the bear is required to jump then we make the bear jump 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
Closed as Not Constructive by Gey4Jesus69, yHasteeD, TheluaBanana, zblox164, and Leamir
This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.
Why was this question closed?