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.
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)
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