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:
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()