NPC head rotations get messed up with animations

So recently I added a feature in my game where when you talk to NPCs they will look at you, here is an example: https://gyazo.com/bd75c201ef528b88dab53eabe45c825b

However if the NPC is playing certain animations that move the head lower (e.g sitting down) it can mess up the head rotations
Example of the error: https://gyazo.com/fb65c866e126bbf8fde585bcea0e9297

Here is my code to rotate the heads

if source:FindFirstChild("Head") and source.Head:FindFirstChild("Neck") then
			
			if endingHeadRotation == true then
				repeat
					runService.Heartbeat:Wait()
				until endingHeadRotation == false or interaction.Value ~= source
				
				if interaction.Value ~= source then
					return
				end
			end
			
			local neck = source.Head.Neck
			local NPC = source

			local cframe0 = neck.C0

			repeat
				runService.Heartbeat:Wait()
				local is_in_front = NPC.Head.CFrame:ToObjectSpace(char.Head.CFrame).Z < -0.8
				if is_in_front and endingHeadRotation == false then
					local unit = -(NPC.Head.CFrame.p - char.Head.Position).unit

					local tween = tweenService:Create(neck,TweenInfo.new(0.5),{C0 = cframe0 * CFrame.new(Vector3.new(0, 0, 0), unit)})
					tween:Play()
				end
			until interaction.Value ~= source or dead

			endingHeadRotation = true

			local tween = tweenService:Create(neck,TweenInfo.new(0.5),{C0 = cframe0})
			tween:Play()

			task.wait(0.5)

			endingHeadRotation = false
		end

How can I make it take into account the head position based on animations? All help is greatly appreciated thanks in advance!

1 Like

Animations are applied in a motor6ds .Transform. Here are some ideas you can do:

So you can either set it to CFrame.new() on .Stepped as recommended by the documentation to cancel the animations.

Or you can try to add an Transform:Inverse() on the C0 to cancel out the transform animation.

Another idea is if you still want the effect of animations you can :Lerp() it with CFrame.new() to reduce the amount of rotations applied in the .Transform.

1 Like

I dont want to cancel out the animation though. I want it to work with the animation.

How would I go about doing the :Lerp part? I want to keep the animation whilst still having the head rotations work properly.

Still having this issue a week later with no solution found yet. Would an align orientation fix this issue? I’m unsure if an align orientation would even work on a rigged head to begin with.

Don’t know the code for it but you want to take into account the head relative to the torso/upper torso. Here is a video that shows you how to do it:

R15:

R6 Functionality + Replication:

Not sure if you’ll need the replication but if you’re R15, watch the first video. If you’re R6, watch the first then the second because it builds off the first.

1 Like

Would this take into account the head being rotated by animations? Offsetting by the upper torso would definitely improve the head rotations but im unsure if this would fix the problem altogether.

As stated here, you can do this to essentially get rid of head animations. I’m not entirely sure what you mean. You either:

  1. You want the head to face the direction and if the animation makes the head look right, the head won’t look away. You can fix this by doing what’s listed above by dthecoolest.
  2. You want the head to face the direction and if your character (upper torso) gets rotated by an animation, the head will accommodate for this change. This can be fixed with my response above.
1 Like

I believe I want both if im understanding correctly.
How exactly do I apply the Transform:Inverse() ? I am unfamiliar with what “transform” implies.

This solution actually solved my problem. Ty for the help you offered regardless.