Pathfinding and MoveTo questions

I have created a Pathfinding script that calculates a path between the “Unit” that is following and the Player “Target”, then the Unit follows the path with the Humanoid:MoveTo() function. I have it iterating through the waypoints within the path using the MoveToFinished connection and incrementing whenever a waypoint is reached. My main question is whenever I decrease the waypoint spacing to anything below 4 the speed of the Unit seems to not reflect the walkspeed set in the humanoid object. I have made sure that MoveTo is only being called once per waypoint (ie not being spam called). Is this behavior normal? I read somewhere that the more waypoints there are the slower the perceived speed of traversal. Is that true? If so is there another way to preserve the walkspeed of the humanoid regardless of waypoint spacing or should I just remember to not have the spacing small?

Thank you for reading,
Zackary

Does the character stop moving for a bit each waypoint? If that’s the case you should use Humanoid:Move() instead, and only use MoveTo when you detect that the waypoint the character will move to is the last waypoint in the table

1 Like

It doesn’t stop moving at each waypoint. Whats the difference between Move() and MoveTo()?
Thanks!

Move() takes in an Unit vector; basically a Vector3 that represents a direction instead of a fixed position to walk to. This allows for the character to never stop moving until the movement is interrupted
MoveTo() takes in a specific position to walk towards, and causes the character to stop walking as soon as it reaches the provided destination

1 Like

This is the code I have for the MoveToFinished() function. I’m using OOP within module scripts. You can assume that any MoveTo function calls is just using the direct Humanoid:MoveTo function.

function BasePath:ReachedPath(Reached)
	
	if Reached == true then
		
		
		if self:GetCurrentWaypointIndex() + 1 <= #self.Waypoints then

			self:SetCurrentWaypointIndex(self:GetCurrentWaypointIndex() + 1)
			

			self.Unit:MoveTo(self:GetCurrentWaypoint().Position)

			
		else
			--End of path reached
			print("End Reached: "..  tostring(self:GetCurrentWaypointIndex()) .. " Of: " .. tostring(#self.Waypoints))
			self:EndOfPath()

		end
		
		
	else


		
		print("Reached is false")
		

	end
	
end

Would it be better then to use the Move function? Does the move function have any event connections like MoveTo has with MoveToFinished?

Try to use Move().
And no, it doesn’t have any events for MoveToFinished since Move() doesn’t set a target position. You have to calculate the distance between the character and the waypoint, and if they are close enough, that meansthe character has reached the waypoint

2 Likes

As @Giftdenz said, here’s how it should be done

function BasePath:ReachedPath(Reached)
	
	if Reached == true then
		
		
		if self:GetCurrentWaypointIndex() + 1 <= #self.Waypoints then

			self:SetCurrentWaypointIndex(self:GetCurrentWaypointIndex() + 1)
			
            local moveTick = tick()

            local direction = self:GetCurrentWaypoint().Position - self.Unit.Parent.HumanoidRootPart.Position

            while (direction - Vector3.new(0, direction.Y, 0)).magnitude > 1 and tick() - moveTick < 8 do

                self.Unit:Move(direction.Unit)

                task.wait()

                direction = self:GetCurrentWaypoint().Position - self.Unit.Parent.HumanoidRootPart.Position

            end

            if tick() - moveTick >= 8 then

                -- failed to move to current waypoint

            end
		else
			--End of path reached
			print("End Reached: "..  tostring(self:GetCurrentWaypointIndex()) .. " Of: " .. tostring(#self.Waypoints))
			self:EndOfPath()

		end
		
		
	else


		
		print("Reached is false")
		

	end
	
end
1 Like