Animation Ruins The Player Head Rotation!

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

here is what it looks like in game:

The file:
Place.rbxl (77.5 KB)

You can add an offset angle based on the animation using the .Transform property to compensate.

How will that work on my script?

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

You can get the angular difference between the RootPart and Torso like so:

local _,RootPart_Y = HumanoidRootPart.CFrame:ToOrientation()
local _,Torso_Y= Torso.CFrame:ToOrientation()

local difference = RootPart_Y-Torso_Y

Then add (or subtract) the difference from the Neck’s C0.

Its not working. still does the same

Can you show me your updated code?

You should do something like this:

ha.C0 = ha.C0:Lerp(haC0 * CFrame.Angles(0,hHor-(d*b),0) * CFrame.Angles(0,difference,0),0.1)

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

Can you try printing the difference? (Do convert it into degrees using math.deg())

Please send the output after printing.

it prints 19.998657976026294 degrees with animation player. 0 degrees without animation played

So it does change… Hmm.

Are you really SURE it doesn’t work? Because with this result, it really isn’t possible for it not to work.

Unless the rest of your code is causing the issue. Can you explain it to me? (Can’t understand much because of the abbreviations.

It does not. I will post the project but since how roblox handles animations you will have to upload the animation in order to use it, hold on

The file is in the original post. please check it out

Alright. I’ll let you know.

And I apologize for the delayed reply. I thought I had replied to you but it seems that my post was accidentally sent to drafts.

1 Like

I don’t know about you but:

I just enabled one script and it works.
Place.rbxl (77.5 KB)

So I think we know the issue. The issue was that you probably did not enable the script.

Hi, i have a similar issue with more complex animation can you help me?
its here: Animation Ruins The Player Head Rotation!(PART 2)