I am currently working on an AI that chases the closest player. If it can see the player with a ray, it doesn’t pathfinding. If it cant see the player (which is when it would need to jump), it pathfinds.
My issue: The AI does not jump and just walks into the obstacle until it eventually gives up (for much longer than it should be, my MoveToFinished:Wait is set to 2 seconds, yet it still waks into the wall for around 10 seconds), or ends up jumping.
The part of the code that is giving me issues:
while wait() do
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target then
--ray to see if straight line, if not, pathfind
local ray = Ray.new(AI.HumanoidRootPart.Position, (target.Position - AI.HumanoidRootPart.Position).Unit * 200)
local hit, position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
if hit then
if hit:IsDescendantOf(target.Parent) then
AI.Humanoid:MoveTo(target.position)
else --pathfinding part!!!!!
AI.Humanoid:MoveTo(target.Position)
path = pfs:FindPathAsync(AI.HumanoidRootPart.Position, target.Position)
points = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i,v in pairs(points) do
AI.Humanoid:MoveTo(v.Position)
AI.Humanoid.MoveToFinished:Wait(2)
if v.Action == Enum.PathWaypointAction.Jump then
AI.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
if (points[#points].Position - target.Position).magnitude > 15 then
break
end
end
end
end
end
end
end