Changing orientation during pathfinding causes change in pos

For the past few days i’ve been trying to code a pathfinding system where when an NPC spots the player through raycast it stops walking, stares at the player and shoots at him

local findingPath = false

function pathFinding(target: Vector3)
	local path = PathfindingService:CreatePath({
		WaypointSpacing = 2
	})

	local waypoints
	local nextWaypointIndex
	local reachedConnection
	local blockedConnection
	
	findingPath = true
	
	
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(mob.PrimaryPart.Position, target)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		
		-- Get the path waypoints
		waypoints = path:GetWaypoints()
		
		-- Detect if path becomes blocked
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			-- Check if the obstacle is further down the path
			if blockedWaypointIndex >= nextWaypointIndex then
				-- Stop detecting path blockage until path is re-computed
				blockedConnection:Disconnect()
				-- Call function to re-compute new path
				pathFinding(plrHrp.Position)
			end
		end)

		-- Detect when movement to next waypoint is complete
		if not reachedConnection then
			reachedConnection = mob.Humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then					
					
					-- Check if target is now visible and stop pathfinding
					if rayHitTarget(hitbox) then
						findingPath = false
						reachedConnection:Disconnect()
						blockedConnection:Disconnect()

						return
					else
						-- Increase waypoint index and move to next waypoint
						nextWaypointIndex += 1
						mob.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
					end
				else
					findingPath = false
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end
		
		-- Initially move to second waypoint (first waypoint is path start; skip it)
		nextWaypointIndex = 2
		mob.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
	else
		warn("Path not computed!", errorMessage)
	end
end	

function trackTarget(target)
	local lookAt = Vector3.new(target.Position.X,mobHrp.Position.Y,target.Position.Z)
	local target = CFrame.lookAt(mobHrp.Position, lookAt)
	
	mobHrp.CFrame = CFrame.lookAt(mobHrp.Position, lookAt)
end

RunService.Heartbeat:Connect(function()
	if findingPath == false then
		if rayHitTarget(hitbox) then
			trackTarget(plrHrp)
			useTool() --Shoot
		else
			pathFinding(plrHrp.Position)
		end
	end
end)

At first glance the code worked fine, until i saw that whenever pathfinding is interrupted due to the player being spotted the NPC suddenly teleports by few studs as shown below:

  • After a lot of testing, i came to the conclusion that changing the orientation of the HumanoidRootPart (through vector3 or CFrame) while the humanoid is performing “:MoveTo” causes this weird glitch.

  • Furthermore, rayHitTarget() should be called only whenever the NPC arrives at a waypoint, and before it starts going to the next, therefore it isn’t supposed to still be performing “:MoveTo”; but despite that, the NPC still walks few studs before stopping after detecting the player.

If someone can explain to me why either of these 2 happen, the code should then work as intended

2 Likes