Pathfinding makes unnessesory waypoints at the start

So used to use Humanoids to move my character but for flexibility reasons I switched to ControllerManager but as soon as I did so the pathfinding that worked perfectly fine just stopped working all of a sudden.
Now whenever a new path is created it creates a few waypoints at the beggining that makes the NPC go backwards first or sideways first. It does sometimes work as intended but not enough for anyone to be able to ingnore it.
Here the video showcasing the issue:

Here is the code.

local Diff = GetTargetPositionDiff(Entity);
local Data = Entity.AIData;
if Diff.Magnitude > Data.WalkToRange then
	local Position = Entity.Rig.PrimaryPart.Position;

	--Create a path if the NPC doesn't have one already
	if not Data.Path then
		Data.Path = PathfindingService:CreatePath({
			WaypointSpacing = 12,
		});
	end

	local TargetFloor = items.GetFloor(Data.Target.Rig, true);
	local Floor = items.GetFloor(Entity.Rig, false);

	--New waypoints are only created when the NPC is on the floor,
	--target is not too high above the ground and
	--the NPC doesn't have any waypoints defined or the target it's following moved 8 studs,
	if TargetFloor and Floor and (Data.Waypoints == nil or #Data.Waypoints == 0 or ((TargetFloor.Position - Data.Waypoints[#Data.Waypoints].Position).Magnitude > 8)) then
		Data.Path:ComputeAsync(Floor.Position, TargetFloor.Position);
		Data.WaypointIndex = 1;
		Data.Waypoints = Data.Path:GetWaypoints();

		--[[]]
		--Reset the waypoint markers
		for _, Attachment in game.Workspace.Waypoints:GetChildren() do
			Attachment.Visible = false;
			Attachment.Color3 = Color3.new(0, 1, 1);
		end
		--Create waypoint markers and put them on the waypoints
		for i, v in Data.Waypoints do
			local Attachment = game.Workspace.Waypoints:FindFirstChild(tostring(i));
			if Attachment == nil then
				Attachment = Instance.new("SphereHandleAdornment");
				Attachment.Parent = game.Workspace.Waypoints;
				Attachment.Name = tostring(i);
				Attachment.Adornee = Attachment.Parent;
				Attachment.Color3 = Color3.new(0, 1, 1);
				Attachment.Radius = 0.5;
			end
			Attachment.Visible = true;
			Attachment.CFrame = CFrame.new(v.Position);
		end

	end

	if Data.Waypoints then
		local Waypoint = Data.Waypoints[Data.WaypointIndex];
		if Waypoint then --Found a waypoint
			local HorizontalMag = ((Waypoint.Position - Position) * Vector3.new(1, 0, 1)).Magnitude;

			--[[]]
			--Color the already reached waypoints
			local Attachment = game.Workspace.Waypoints:FindFirstChild(tostring(Data.WaypointIndex));
			Attachment.Color3 = Color3.new(1, 1, 0);

			--Jumping
			if HorizontalMag < 2 and Floor and math.abs(Floor.Position.Y - Waypoint.Position.Y) > 1.5 then
				items.ActivateAction(Entity.Item, "Jump");
			else
				items.DeactivateAction(Entity.Item, "Jump");
			end

			--Turns the NPC towards the target waypoint
			Entity:Turn("Base", 1, {
				Mode = "AlignTo",
				Target = (Waypoint.Position - Position),
				Speed = 720,
			});
			if HorizontalMag > 1 then
				--Move the NPC to the target waypoint
				Entity:Move("Base", 1, {
					Direction = (Waypoint.Position - Position),
					Speed = Entity.Item.Stats.Body.Movement.Speed,
				});
			else
				--Waypoint reached so stop the movement for now
				Data.WaypointIndex += 1;
				Entity:Move("Base", 1, {
					Direction = Vector3.zero,
					Speed = 0,
				});
			end
		else --No next waypoint
			--Turn the NPC towards the character it's following
			Entity:Turn("Base", 1, {
				Mode = "LookAt",
				Target = Entity.AIData.Target.Rig.PrimaryPart,
				Speed = 720,
			});
			--Move the NPC directly towards the character it's following
			Entity:Move("Base", 1, {
				RelativeTo = Entity.Rig.PrimaryPart,
				Direction = Vector3.new(0, 0, -1),
				Speed = Entity.Item.Stats.Body.Movement.Speed,
			});
		end
	end
else
	--Got close to the target the NPC is following so end the action
	Data.Waypoints = nil;
	EndBehaviour(Entity);
end

If you have any idea on how I could fix this issue please let me know, and thank you so much for your time.

1 Like

although i can’t see much in the code, i’m noticing a pattern here

the start of the path and the end of the path are similar, sometimes identical, with the first and last 3 (or more) waypoints being extremely cramped and for whatever reason do not go directly to the player, but make a weird 90 degree turn

it probably has something to do with the way you’re calculating direction in this code snippet

yeah this wasn’t very helpful, but it’s the thought that counts i think

I can’t imagine that the way I move the character to the waypoints, would have any effect on the caculation of the waypoints though…
Maybe the issue stems from the fact that I have no idea how pathfinding and NavMesh works, so I might be using the wrong vectors to calculate the waypoints?

1 Like

I figured it out, I tried turning off the CanCollide of the HumanoidRootPart (I’m using the HumanoidRootPart as the collider instead of the Torso) and it worked perfectly like it used to.

I think what happend was that the torso with a Humanoid automaticly get’s ignored when pathfinding but when I switched to ControllerManager and deleted the Humanoid, the service tried to pathfind a way to get in the gap between the HumanoidRootPart and the ground and resulted in well… that.

But I do need to have HumanoidRootPart’s CanCollide on otherwise the character goes through walls so I put a PathfindingModifier with PassThrough enabled and it worked exactly how I wanted it to.