Making NPC's head face player correctly no matter their rotation/orientation

YRiNDOVMbt
As you can see, I want the NPC’s neck to rotate so their head is facing the player character. The gif above is how its supposed to look. Now, this works, except when I rotate the entire NPC. When I rotate the NPC, this happens:

yHmKiE18R8
This function works when the NPC is facing a certain way, but if you rotate the NPC it doesn’t take it to account and doesn’t work as intended. I’m not very good at math or cframes so I have no idea the solution to this.

Here is the script.

local CameraDirection = CFrame.new(NPC.PrimaryPart.Position,PlayerCharacter.PrimaryPart.Position).lookVector
local Neck = self.Model:FindFirstChild("Neck", true)
local YOffset = Neck.C0.Y

local Goal = CFrame.new(0, YOffset, 0) * CFrame.Angles(0, -math.asin(CameraDirection.x), 0) * CFrame.Angles(math.asin(CameraDirection.y), 0, 0)

local tweenInfo = TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.Out)
local Tween = TweenService:Create(Neck, tweenInfo, {C0 = Goal})
Tween:Play()
15 Likes

I found the solution after a few hours of GRUELING pain

local CameraDirection = (CFrame.new(self.hrp.Position,self.PC.PrimaryPart.Position):inverse() * self.hrp.CFrame ).lookVector * -1
25 Likes

YOU ARE SUCH A GOD!!!

Thank you wish, u help me after 3 days of slamming my head into CFrame.Angles

5 Likes

Holy crap thank you so much… been banging my massive forehead into my desk for days

2 Likes

Wow bandi learning programming yay.

1 Like

When I use the Goal variable in your original post with this CameraDirection it doesn’t work. Did you change the Goal variable aswell?

1 Like