Custom NPC spins randomly

I made a custom npc model, but the model starts spinning or turns too much each time it goes through a turn.


I’ve tried adjusting the hip height and rerigging, but both don’t help.

And I might be wrong about this but I don’t think its because of the pathfinding method I’m using since it works with perfectly with normal R15 and R6 rigs.

Here is a portion of the pathfinding module I’m using though

function Pathfinder:MoveTo(targetPosition)
	self:Cancel()
	self.Cancelled = false

	local path = PathfindingService:CreatePath({
		AgentRadius = 1,
		AgentHeight = 5,
		AgentCanJump = false,
		WaypointSpacing = 2,
		Costs = {
			Blockage = math.huge,
			Doorway = 1,
		}
	})

	path:ComputeAsync(self.HRP.Position, targetPosition)

	if path.Status ~= Enum.PathStatus.Success then
		return false
	end

	local waypoints = path:GetWaypoints()

	for i, waypoint in ipairs(waypoints) do
		
		if self.Cancelled then
			return false
		end

		self.Humanoid:MoveTo(waypoint.Position)

		local reached = false
		self.CurrentConnection = self.Humanoid.MoveToFinished:Connect(function(success)
			reached = success
		end)

		local timeout = 5
		local elapsed = 0
		while not reached and elapsed < timeout do
			if self.Cancelled and self.CurrentConnection then
				self.CurrentConnection:Disconnect()
				self.CurrentConnection = nil
				return false
			end
			RunService.Heartbeat:Wait()
			elapsed += RunService.Heartbeat:Wait()
		end

		if self.CurrentConnection then
			self.CurrentConnection:Disconnect()
			self.CurrentConnection = nil
		end

		if not reached then
			if self.AI:GetAttribute('IsChasing') == false then
				Reset.resetAI(ServerStorage:GetAttribute('Level'), self.AI, self.AI.Orgin.Value, self.AI.Waypoints.Value, 'AI got stuck')
			end
			return false
		end
	end
	
	return true
end

any help is appreciated, thanks!

1 Like

You may want to look at your pathfinding parameters and make the AgentRadius parameter bigger

function Pathfinder:MoveTo(targetPosition)
    self:Cancel()
    self.Cancelled = false

    local path = PathfindingService:CreatePath({
        AgentRadius = 1,
        AgentHeight = 5,
        AgentCanJump = false,
        WaypointSpacing = 2,
        Costs = {
            Blockage = math.huge,
            Doorway = 1,
        }
    })

    path:ComputeAsync(self.HRP.Position, targetPosition)

    if path.Status ~= Enum.PathStatus.Success then
        return false
    end

    local waypoints = path:GetWaypoints()

    for i, waypoint in ipairs(waypoints) do
        if self.Cancelled then
            return false
        end

        self.Humanoid.AutoRotate = false
        self.Humanoid:MoveTo(waypoint.Position)
        self.Humanoid.CFrame = CFrame.lookAt(self.Humanoid.Position, waypoint.Position)

        local reached = false
        self.CurrentConnection = self.Humanoid.MoveToFinished:Connect(function(success)
            reached = success
        end)

        local timeout = 5
        local elapsed = 0
        local lastTime = tick()

        while not reached and elapsed < timeout do
            if self.Cancelled and self.CurrentConnection then
                self.CurrentConnection:Disconnect()
                self.CurrentConnection = nil
                return false
            end
            RunService.Heartbeat:Wait()
            elapsed = tick() - lastTime
            lastTime = tick()
        end

        if self.CurrentConnection then
            self.CurrentConnection:Disconnect()
            self.CurrentConnection = nil
        end

        if not reached then
            if self.AI:GetAttribute('IsChasing') == false then
                Reset.resetAI(ServerStorage:GetAttribute('Level'), self.AI, self.AI.Orgin.Value, self.AI.Waypoints.Value, 'AI got stuck')
            end
            return false
        end
    end

    return true
end

1 Like

Let them be a ballerina dont ruin their dreams

4 Likes

tried this but its not the issue sadly

1 Like

no luck… just took his dancing a little more seriously

1 Like

have you tried changing the mass with CustomPhysicalProperties on the baseparts in the rig? could be the mass doing some wonky stuff.

2 Likes
function Pathfinder:MoveTo(targetPosition)
    self:Cancel()
    self.Cancelled = false

    local path = PathfindingService:CreatePath({
        AgentRadius = 1,
        AgentHeight = 5,
        AgentCanJump = false,
        WaypointSpacing = 2,
        Costs = {
            Blockage = math.huge,
            Doorway = 1,
        }
    })

    path:ComputeAsync(self.HRP.Position, targetPosition)

    if path.Status ~= Enum.PathStatus.Complete then
        return false
    end

    local waypoints = path:GetWaypoints()

    self.Humanoid.AutoRotate = true

    for i, waypoint in ipairs(waypoints) do
        if self.Cancelled then
            return false
        end

        self.Humanoid:MoveTo(waypoint.Position)

        local reached = false
        self.CurrentConnection = self.Humanoid.MoveToFinished:Connect(function(success)
            reached = success
        end)

        repeat
            if self.Cancelled then
                if self.CurrentConnection then
                    self.CurrentConnection:Disconnect()
                    self.CurrentConnection = nil
                end
                return false
            end
            RunService.Heartbeat:Wait()
        until reached

        if self.CurrentConnection then
            self.CurrentConnection:Disconnect()
            self.CurrentConnection = nil
        end

        if not reached then
            if self.AI:GetAttribute('IsChasing') == false then
                Reset.resetAI(ServerStorage:GetAttribute('Level'), self.AI, self.AI.Orgin.Value, self.AI.Waypoints.Value, 'AI got stuck')
            end
            return false
        end
    end

    return true
end

1 Like

i believe the issue is your humanoid is trying to reach the point on the ground and the humanoid position is to high so it circles it.. there are a few ways to fix this try setting the AgentHeight to the actual npc height

or add an if statement in your while loop to check distance of the humanoid to the position if less than say 3 or 4 you will have to test the threshold then break the loop (do some test prints on the distance the npc HRP is to the point to get your threshold)

or add to Y on the waypoint position like below

this is offset for the height of the custom npc

self.Humanoid:MoveTo(waypoint.Position + Vector3.new(0,3,0))  -- you can add height here to the Y
1 Like

Problem was I forgot to set a part to massless. Turning it on fixed the problem, I appreciate all the help though

1 Like

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