Im trying to make an npc that will just walk behind the player and follow them around. I tried using pathfinding to do this but it seems to be really janky and chopy. Heres the code for it :
--Services local PathFindingService = game:GetService("PathfindingService") --Variables local targetCharacter = script.Parent.Parent.Parent local target = targetCharacter:WaitForChild("HumanoidRootPart") local localCharacter = script.Parent.Parent local localHumanoid = localCharacter:WaitForChild("Humanoid") local localRoot = localCharacter:WaitForChild("HumanoidRootPart") --Functions function moveTo() print("Moving to player") local pos = target.Position + target.CFrame.LookVector * -2 + target.CFrame.RightVector * 2 localHumanoid:MoveTo(pos) end function drawWaypoint(point) local part = Instance.new("Part", workspace) part.BrickColor = BrickColor.new("White") part.Shape = "Ball" part.Material = "Neon" part.Transparency = 0.25 part.Anchored = true part.Locked = true part.CanCollide = false part.Size = Vector3.new(1,1,1) part.Position = point.Position game:GetService("Debris"):AddItem(part,1) end function generatePath() print("Generating Path") local path = PathFindingService:CreatePath() path:ComputeAsync(localRoot.Position ,target.Position + target.CFrame.LookVector * -2 + target.CFrame.RightVector * 2) return path end function moveAlongWaypoints(path) print("Moving along waypoints") local wayPoints = path:GetWaypoints() for _,waypoint in pairs(wayPoints)do print("Next waypoint") drawWaypoint(waypoint) localHumanoid:MoveTo(waypoint.Position) localHumanoid.MoveToFinished:Wait() end end function main() local path = generatePath() if path ~= nil and path.Status == Enum.PathStatus.Success then print("Path was Successfully made") moveAlongWaypoints(path) else warn("Failed to generate path") end end --Loops main function while true do print("Running Main") main() game:GetService("RunService").Stepped:Wait() end
If some one could fix the code to make the movement smoother or just tell me a better way I would apreaciate it alot :)
https://gyazo.com/3e0e075f6b6d6f740ba2e5d302e55b8a