NPC Is Jittering (Help!)

Hi all,

I have placed a bunch of nodes around the place and use Pathfinding to make NPC civilians walk around a mall. However, it seems to me that the walk animation is very laggy and jittery. The issue is not the animation itself, as I have tried changing the animation pack of the NPC & still encountered the same issue.

Here’s the important part of the code (Not the full code):

local path = PathfindingService:CreatePath({
	AgentRadius = 5,
	Costs = {
		MarketPath = 0.01
	}
})

local function walkAround()
	local waypointSelected = waypointTable[RND:NextInteger(1, #waypointTable)]
	path:ComputeAsync(RootPart.Position, waypointSelected.Position)
	local waypoints = path:GetWaypoints()
	if path.Status == Enum.PathStatus.Success then
		for _, waypoint in waypoints do
			if waypoint.Action == Enum.PathWaypointAction.Walk then
				npc.Humanoid:MoveTo(waypoint.Position)
				npc.Humanoid.MoveToFinished:Wait()
			else
				npc.Humanoid.Jump = true
			end
		end
	end
end

while task.wait() do
	walkAround()
end

I have tried setting a WaypointSpacing for the path, but that made no difference to the animation bug. How could I solve this? Thanks in advance.

2 Likes

I’d suggest you just handle it in the same script by listening to humanoid.Running event,

local WalkAnim = npc.Humanoid.Animator:LoadAnimation(WalkAnimHere)

npc.Humanoid.Running:Connect(function(speed)
	if speed > 0 and not WalkAnim.IsPlaying then
		WalkAnim:Play()
	elseif speed == 0 and WalkAnim.IsPlaying then
		WalkAnim:Stop()
	end
end)
2 Likes

Unfortunately, that made no difference at all:

How are you handling animations? I made a little bit of a security bot NPC a while back and I ran its animations through the movement control functions. Here’s some of its code in case it’s helpful:

local function LoadAnim(AnimationName)
	return Main.Humanoid:LoadAnimation(Main.Animations:WaitForChild(AnimationName))
end

local Animations = {
	Idle = LoadAnim("Idle"),
	Walk = LoadAnim("Walk"),
	Jump = LoadAnim("Jump")
}

function Movement:WalkToPoint(EndVector) 
	local Path = pfs:CreatePath(self.Info.AgentParams)
	Path:ComputeAsync(Main.HumanoidRootPart.Position,EndVector)
	local Points = Path:GetWaypoints()
	if Path.Status == Enum.PathStatus.Success then
		Animations.Idle:Stop() --// Get the walking animation going here
		Animations.Walk:Play()
		self.Info.Walking = true
		local Distance = nil
		for i,v in pairs(Points) do
			if self.Info.Walking == false then break end
			local PointPos = v.Position
			if v.Action == Enum.PathWaypointAction.Jump then
				self:Jump()
			end
			Main.Humanoid:MoveTo(PointPos)
			repeat
				Distance = (PointPos - Main.HumanoidRootPart.Position).Magnitude
				wait()
			until Distance <= 5 or self.Info.Walking == false
		end
		Animations.Walk:Stop()
		Animations.Idle:Play() --// Return to idle animation here after the bot has finished walking
		self.Info.Walking = false
	else
		warn("Bot could not compute path to destination.")
		self:ReturnToPost()
	end
end
2 Likes

you disabled the animate script in the npc right?

1 Like

Oh, that wouldn’t be what I am looking for. I still want to keep the other animations (Idle, jump, run/walk, fall, etc)

Then try to removing the part where it plays walk animation in the animate script, pretty sure the issue is in that

Even though the run part has been removed from the animate script, the running animation is still jittery (at the end of the video)

Okay so instead of going with npc.Humanoid.MoveToFinished:Wait(), I tried your approach:

repeat
					Distance = (waypoint.Position - npc.HumanoidRootPart.Position).Magnitude
					task.wait()
				until Distance <= 5 or npc.Humanoid.WalkToPoint ~= waypoint.Position

And it worked flawlessly.

1 Like

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