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.