I made a script that rotates the character head but when animation plays it affects the head rotation. I made line that should rotate the head based on the angle gap it has, to rotate it back to the target but it does only halfway there.
here is the code:
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))
ha.C0 = ha.C0:Lerp(haC0 * CFrame.Angles(0,hHor-(d*b),0),0.1)
print(math.deg(d))
end
add Torso.CFrame:Inverse() to the neck joint CFrame (or head cframe) so you cancel out the torso rotation from the animation and then you can apply your own head rotation
Its not working, it does literally the same here is the code:
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