Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a way to make my Pathfinding NPC to move more smoothly?

Hello, at the moment my pathfinding NPCs appear to be really jolty and i don't believe it is to do with server lag because when i fire my gun the projectile is moving at a good speed. So i believe it is to do with the path the NPCs are following. Anyone know how i would fix this?

local head = script.Parent.Parent:FindFirstChild("Head")
		
		
while true do
	
	
	local PathfindingService = game:GetService("PathfindingService")
	local NPC = script.Parent.Parent
	local Humanoid = NPC.Humanoid
	local RootPart = NPC.HumanoidRootPart
		
	local Target = workspace.NoirPhoenix:FindFirstChild("HumanoidRootPart")
		
	local Start = RootPart.Position
	local Goal = Vector3.new(Target.Position.X -7,Target.Position.Y, Target.Position.Z) 	
	
	
	local path = PathfindingService:FindPathAsync(Start, Goal)
	local waypoints = path:GetWaypoints()
			
	for waypointIndex, waypoint in pairs(waypoints) do
		local waypointPosition = waypoint.Position
		
		if waypointPosition.Y >= head.Position.Y then
			print("Jump")
			Humanoid.Jump = true
		end
				
		Humanoid:MoveTo(waypointPosition)
		Humanoid.MoveToFinished:Wait()
	end
	wait()
	
	
			
end
		

3 comments

0

If you are using :Wait() for MoveToFinished, try give the NPC network ownership to the server.

0

and how do i do that?

0

Don't create all of your variables inside the while loop. Only put inside the loop what is being looped, otherwise you're creating the same variables probably over thousands of times.

2 answers

✓ Accepted answer

Here. what you can do is use this line before " local path = PathfindingService:FindPathAsync(Start, Goal)"

local frame = (RootPart.CFrame*CFrame.new(0,0,-5)).Position Humanoid:MoveTo(frame)

This basically moves the npc forward until it calculates a path over to the player, stopping the noticeable gaps between the use of pathfinding. ALSO, something I noticed that was a widespread problem with Humanoid:Moveto() is that there is a pausing problem between each step of a waypoint. to fix this, simply put the following code in the beginning of the script after the variables are created:

NPC.PrimaryPart:SetNetworkOwner(nil)

This prevents the network ownership from being tossed from player to player and instead relies on the server for physics functionality, stopping the velocity from decreasing and increasing and making the movement 100% more smoother.

your welcome :D

The path usually isn't the issue for jittery movement with the Pathfinding Service. It (probably) is giving a regular set of points that would allow you to get from start to finish smoothly. If you don't think this is the case - try looping through the points given in your path, and create a new Part instance to indicate each point your AI is moving through.

More often than not, it is the technique you use to move your AI. If you are setting the AI's CFrame to the next point and just Wait()ing, then you would have jittery movement - as opposed to using the :MoveTo() method from Humanoid.

There are many options for smooth movement:

  • As I listed above, MoveTo()
  • You could tween the object to the next position
  • In very few, but occasionally you could use RunService and use the RenderStepped function

I hope this helps.

2 comments

0

I'm using :MoveTo() for my NPCs with pathfinding. I'll update my answer to show you my code for the NPCs

0

Like mattscy said, check out Network Ownership: http://robloxdev.com/articles/Network-Ownership

This discussion is preserved as it was. New answers, comments, and votes are closed.