Humanoid:MoveTo() never moves NPC to exact position

I want to make NPC WALK to EXACT position that was put in Humanoid:MoveTo().

From what I’ve seen so far NPC walks, but ALWAYS decides to stop just a moment before completing the path as showcased in the video. Those red and green parts have CanCollide, CanQuery and CanTouch disabled, being for debugging purposes only. Green shows where NPC should stop.

Here’s the code for making NPC move (Server):

local r_storage = game:GetService("ReplicatedStorage")
local s_storage = game:GetService("ServerStorage")
local r_events = r_storage:WaitForChild("Events")
local pathfiding = game:GetService("PathfindingService")

local v_soldier = workspace:WaitForChild("Soldier")

r_events:WaitForChild("Units"):WaitForChild("Move").OnServerEvent:Connect(function(player, pos)
	print(player, pos)
	local t_node = Instance.new("Part")
	t_node.Size = Vector3.new(0.5, 0.5, 0.5)
	t_node.Material = Enum.Material.Neon
	t_node.Color = Color3.fromRGB(255, 74, 77)
	t_node.Anchored = true
	t_node.CanCollide = false
	t_node.CanQuery = false
	t_node.CanTouch = false
	t_node.Parent = workspace
	t_node.Position = pos
	
	local t_path = pathfiding:CreatePath({
		AgentCanJump = false,
		AgentCanClimb = false,
		AgentRadius = 0.2,
	})
	local t_waypoints
	local success, errorMsg = pcall(function()
		t_path:ComputeAsync(v_soldier:WaitForChild("Node").Position, pos)
	end)
	
	if success and t_path.Status == Enum.PathStatus.Success then
		print("foundPath")
		t_waypoints = t_path:GetWaypoints()
	end
	
	if t_waypoints then
		local t_currentWaypoint = 0
		local t_limit = table.maxn(t_waypoints)
		
		repeat
			t_currentWaypoint+=1
			v_soldier.Humanoid:MoveTo(t_waypoints[t_currentWaypoint].Position)
			v_soldier:WaitForChild("Humanoid").MoveToFinished:Wait()
		until t_currentWaypoint == t_limit
	end
	t_node:Destroy()
end)

Is there any way to fix it or atleast different method that will allow to achieve the same thing?

Sadly I think this is the inherent problem with moveto (correct me if I am wrong)

luckily, it roughly stops the same distance each time, so we can simply move the target of the moveto a bit more in that direction
(adding (Vector3)(TargetPosition - NPCposition) to the target position)

2 Likes

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