NPC Pathfinding Issue when rotating HRP

So I’m struggling with TargetUnreachable at the moment when setting the HRP CFrame to lookAt a part.

The thing is, it doesn’t seem that the HRP’s orientation seems to even affect the pathfinding because setting auto rotate to false changes nothing.

However, the issue comes from actually setting the CFrame lookat to face the point.

self.npc.HumanoidRootPart.CFrame = CFrame.lookAt(self.npc.HumanoidRootPart.Position, goalPosition)

The AI errors with TargetUnreachable (I’m using SimplePath) and as a result doesn’t move.

This seems really odd and any help is appreciated,

Here’s a sample of the code that using pathfinding.

function Agent:Patrol()

    if self.patrolPath and not self.patrolling then

        self.patrolling = true

        self.loadedAnimations.AIPatrol:Play()

        local pathParts = self.patrolPath

        print("PATROLING!")

        local function moveTo()

           

            local pathPartsC = pathParts:GetChildren()

            if self.currentWP > #pathPartsC then

                self.currentWP = 1

            end

            local goalPosition = pathParts:FindFirstChild(tostring(self.currentWP)).Position

            self.path:Run(goalPosition)

            --local lookAt = Vector3.new(goalPosition.X, self.npc.HumanoidRootPart.Position.Y, goalPosition.Z)

           -- TweenService:Create(self.npc.HumanoidRootPart, TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {}):Play()

            self.npc.HumanoidRootPart.CFrame = CFrame.lookAt(self.npc.HumanoidRootPart.Position, goalPosition)

            local pathReached

            local waypointReached

            pathReached = self.path.Reached:Connect(function()

                self.currentWP += 1

                pathReached:Disconnect()

                waypointReached:Disconnect()

                moveTo()

            end)

            waypointReached = self.path.WaypointReached:Connect(function()

                if not self.patrolling then

                    self.path:Stop()

                    if waypointReached then

                        waypointReached:Disconnect()

                    end

                    if pathReached then

                        pathReached:Disconnect()

                    end

                    return

                end

            end)

            local errorPath

            errorPath = self.path.Error:Connect(function(err)

                print(err)

                errorPath:Disconnect()

                self.patrolling = false

                return

                task.wait(3)

            end)

        end

        moveTo()

    end

end

My best bet is that while trying to move the NPC, it is being interrupted the moment
you try to set its CFrame, especially considering SimplePath utilizes MoveTo in its inner frameworks. Perhaps modifying the rotation of the HRP and not its CFrame altogether may work.

So, here is a snippet of what I’d do:

npc.HumanoidRootPart.Rotation = Vector3.new(
    CFrame.lookAt(
        self.npc.HumanoidRootPart.Position, goalPosition
    ):ToEulerAnglesXYZ()
)

First things first, we are changing the rotation of the HRP directly, and not its CFrame entirely — which holds both positional and rotational data. Secondly, we obtain the rotational information by extracting it from a CFrame formed by lookAt through the method ToEulerAnglesXYZ, which returns three numbers that are rather self-explanatory. Then, we pass these numbers down as arguments to Vector3.new(), which we then assign to the rotation of the HRP.

Do note that this example was not tested and will have to be edited to suit your needs.

If you were to find a critical flaw with my solution, please let me know.

1 Like

So unfortunately this doesn’t actually work and results in the same issue.

The only thing I’ve noticed is setting the CFrame before calling the path:Run(), since there’s no interruption it ends up fine.

Since there’s probably not a quick fix for this I’ll have to alter a lot of my code to adjust for it, thank you for the post though.