Trouble with AI Pathfinding/Animations

Hey everyone,

I’m experiencing an issue with my NPC pathfinding system within a tunnel. This system includes two parts (Destination and Destination2), where the NPC sets a waypoint.

The problem I’m facing relates to the animation side of things, where the animation of the NPC noticeably staggers as it makes its way towards Destination2 (and slightly as it makes its way to Destination at the beginning).

When thinking of a solution, keep in mind that I’ve tried the following to no avail:

  • Tried replacing the floor of the tunnel as a flat part

  • Replacing all four sides of the tunnel as flat parts

  • Removing the blue path

  • Placing the system on the Baseplate (blue path included)

  • Just placing the base system on the Baseplate

  • Checked for any errors in the Output window (none)

Video:

Script:

local ServerStorage = game:GetService("ServerStorage")
local CueEncounter = ServerStorage:FindFirstChild("CueEncounter")

local PathfindingService = game:GetService("PathfindingService")

local NPC = game.Workspace.ShadowSelf
local Humanoid = NPC.Humanoid
local LowerTorso = NPC.LowerTorso

local function GetPath(Destination)
	local Path = PathfindingService:CreatePath({
		AgentRadius = 1.5,
		AgentHeight = 4,
		AgentCanJump = false,
		Costs = {
			Plastic = 1,
			SmoothPlastic = 1,
			Sandstone = math.huge,
			Basalt = math.huge,
			Neon = math.huge
		}
	})

	Path:ComputeAsync(LowerTorso.Position, Destination.Position)

	return Path
	
end

local function WalkTo(Destination)
	local Path = GetPath(Destination)

	for i, Waypoint in ipairs(Path:GetWaypoints()) do
		Humanoid:MoveTo(Waypoint.Position)
		Humanoid.MoveToFinished:Wait()

		local AnimationId = "rbxassetid://16572772560"
		local Animation = Instance.new("Animation")
		Animation.AnimationId = AnimationId

		AnimationRun = Humanoid:LoadAnimation(Animation)
		AnimationRun:Play()

	end
end

local function PlayAnimation()
	local AnimationId = "rbxassetid://16573138859" 

	local Animation = Instance.new("Animation")
	Animation.AnimationId = AnimationId

	local AnimationKnifeDraw = Humanoid:LoadAnimation(Animation)
	AnimationKnifeDraw:Play()

	task.wait(2.5)

	local HeldKnife = NPC.RightHand.HeldKnife
	HeldKnife.Transparency = 0

	AnimationKnifeDraw:Stop()
end

local function Encounter()
	local Destination = workspace.Waypoints.Destination
	local Destination2 = workspace.Waypoints.Destination2

	Humanoid.WalkSpeed = 6

	WalkTo(Destination)
	AnimationRun:Stop()

	PlayAnimation()

	task.wait(1)

	Humanoid.WalkSpeed = 15

	WalkTo(Destination2)
	
	
end

CueEncounter.Event:Connect(function()
	Encounter()
end)
2 Likes

I’m just adding this second video here to show that this animation is problematic on both flat and rough terrain:

Also, here is a short video of the animation itself:

Is it possible that the frames of the animation aren’t level, or does this issue relate to the WalkSpeed of the Humanoid?

The issue isn’t pathfinding or the actual animation. It’s the way you’re playing it. Right now you’re loading and running the “run” animation everytime the NPC reaches a waypoint. This results in that staggering/laggy effect. You just need to play the walk animation once (make sure it’s looped in the animation editor) before you start pathfinding.

Like so:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.