local npcRoot
local plrRoot
game:GetService("RunService").Heartbeat:Connect(function()
local cfRot = CFrame.new(npcRoot.Position, plrRoot.Position).Rotation
npcRoot.CFrame *= cfRot
end)
This would make the primary part of the NPC face towards the player’s primary part. I’m not sure if it’ll prevent the Humanoid instance from automatically rotating but you can try it.
If you’re using :MoveTo() something could be anchored, or there could be another problem. You can try removing the code I gave but I don’t think it’ll matter cause it’s only for rotation.
Once your NPC is moving just change the CFrame of the npc’s root part once in a while to make it face the player
edit: I was scrolling and the Humanoid has a property called AutoRotate, try turning that off and just change the cf of the npc root part once in a while
I don’t really know. If you made edits to your pathfinding code or whatever then have a quick look again. It won’t have nothing to do with humanoids because your npc was fine moving backwards.
Or, you could use AlignOrientation. It locks the NPC to a certain rotation, whilst still allowing it to move around freely. It acts like a CFrame, except it only uses the rotational values of the CFrame.
Here’s how you would use it for this instance:
-- RootPart is a child of the NPC.
-- We're also assuming the AlignOrientation is already created and set up.
RunService.Heartbeat:Connect(function()
local diff = PlayerPos - RootPart.Position
local angle = math.atan2(diff.X, diff.Z) -- we just want the RootPart to orientate on one axis.
local AlignOrientation = RootPart:FindFirstChild("AlignOrientation")
AlignOrientation.CFrame = CFrame.Angles(0, angle, 0)
end)
If you need more information on setting this up, you should click on the website linked to the instance in question. Let us know how it goes.
AlignOrientation works only if there’s attachment inside one part and assigned from a property, You have to set Responsiveness to 200 and the MaxTorque higher to make it rotate properly
id recommend setting from TwoAttachment to OneAttachment so it only uses one part to look at player
you can still use CFrame.lookat on AlignOrientation just like setting a part to look at but it won’t get updated unless it’s in a loop or RunService
Maybe I should’ve also explained how this worked and how you should’ve set this up. … I apologize.
math.atan2 is simply just a function that uses trigonometry to get a radian angle from the direction made up by x and y, typically used for getting the angle orientation of an axis without doing too much work. Just think of it as finding the angle from a LookVector, except its space is within the world.
My code above could’ve been better if diff was turned into a unit, which would made this example much easier to understand because the unit always results in a magnitude of 1. But, the idea is that you’re getting the angle from tan0, which is the quotient of sin0 / cos0, which x and y represents.
Like, for example, if x = 1 and y = 0, then the angle produced by math.atan2 would be pi * 2, or 0 degrees. If it were the opposite, (x = -1; y = 0) then it would just be pi, or 180 degrees. (Free note: pi = 180 deg) One more example is if x = 0.5; y = 0.5), then it would give us pi / 4, or 45 degrees. The negated version would’ve given us 135 degrees.
There’s this video that explained this better than I did, but that was my attempt at trying to give a simplified way of looking at math.atan2’s functionality. That aside, glad that you got it to working. I wish you the best of luck in your project.