I did have a similar issue few days ago:Animation Ruins The Player Head Rotation!
This time i encountered another issue. When i have a more complex animation the head wont aim towards the part well.
The game prints the angle that is between the head look direction and the part.
I have 2 codes but none of them work. the original one was used on the video but it doesnt work. which is:
self = script.Parent
local ha = self.Head.Neck
local haC0 = ha.C0
while task.wait() do
local b = -(vector.angle(self.Head.CFrame.RightVector,(workspace.target.Position-self.HumanoidRootPart.Position)* vector.create(1,0,1) ) - math.rad(90)) < 0 and -1 or 1
local s = -script.Parent.HumanoidRootPart.CFrame:PointToObjectSpace(workspace.target.Position).X > 0 and 1 or -1
local d = vector.angle(self.Head.CFrame.LookVector,(workspace.target.Position - self.Head.Position)*vector.create(1,0,1))
local hHor = s * vector.angle(self.HumanoidRootPart.CFrame.LookVector* vector.create(1,0,1), (workspace.target.Position-self.HumanoidRootPart.Position)* vector.create(1,0,1))
local _,RootPart_Y = self.HumanoidRootPart.CFrame:ToOrientation()
local _,Torso_Y= self.UpperTorso.CFrame:ToOrientation()
local difference = RootPart_Y-Torso_Y
ha.C0 = ha.C0:Lerp(haC0 * CFrame.Angles(0,hHor-(d*b),0) * CFrame.Angles(0,difference,0),0.1)
print(math.deg(d))
end
and the other one does slightly better but still does not hit close to 0 degrees:
self = script.Parent
local ha = self.Head.Neck
local haC0 = ha.C0
while task.wait() do
local b = -(vector.angle(self.Head.CFrame.RightVector,(workspace.target.Position-self.HumanoidRootPart.Position)* vector.create(1,0,1) ) - math.rad(90)) < 0 and -1 or 1
local s = -script.Parent.HumanoidRootPart.CFrame:PointToObjectSpace(workspace.target.Position).X > 0 and 1 or -1
local d = vector.angle(self.Head.CFrame.LookVector,(workspace.target.Position - self.Head.Position)*vector.create(1,0,1))
local hHor = s * vector.angle(self.HumanoidRootPart.CFrame.LookVector* vector.create(1,0,1), (workspace.target.Position-self.HumanoidRootPart.Position)* vector.create(1,0,1))
local _,RootPart_Y = self.HumanoidRootPart.CFrame:ToOrientation()
local _,LTorso_Y= self.UpperTorso.CFrame:ToOrientation()
local _,UTorso_Y= self.UpperTorso.CFrame:ToOrientation()
local _,Head_Y= self.Head.CFrame:ToOrientation()
local difLowerTorso = RootPart_Y-LTorso_Y
local difUpperTorso = LTorso_Y-UTorso_Y
local difHead = UTorso_Y-Head_Y
ha.C0 = ha.C0:Lerp(haC0 * CFrame.Angles(0,hHor-(d*b)+difLowerTorso+ difUpperTorso+difHead,0),0.1)
print(math.deg(d))
end
Here is the file:
Place (1).rbxl (77.9 KB)