Animations messing with head rotation script

  1. What do you want to achieve? Keep it simple and clear!
    I want to make it so the head rotation (head looks where camera is looking) is unaffected by animations

  2. What is the issue? Include screenshots / videos if possible!
    The animations in my game do mess with the rotation, for example when you look to the side while running, the head looks up like crazy, and on the idle I can only turn my head left, not right.

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    I tried removing the head keyframes from the animations, which did work, but the animations felt super bland and some require some level of animation for the head.

This is the script I use for the head rotation.

local YOffset = Neck.C0 -- original neck offset

-- settings
local HEAD_ROTATION_ALPHA = 0.15
local REPLICATION_DELAY = 1 / 8
local lastReplication = 0

RunService.RenderStepped:Connect(function(dt)
	dt = math.min(dt * 60, 1)

	if Player:GetAttribute("HeadMovementEnabled") then
		local camDir = Root.CFrame:ToObjectSpace(Camera.CFrame).LookVector
		local yaw = math.asin(camDir.X)     -- left/right
		local pitch = math.asin(camDir.Y)   -- up/down

		local targetCFrame = YOffset * CFrame.fromEulerAnglesYXZ(pitch * -1,0, yaw * -1)
		Neck.C0 = Neck.C0:Lerp(targetCFrame, HEAD_ROTATION_ALPHA * dt)

		if os.clock() - lastReplication > REPLICATION_DELAY then
			lastReplication = os.clock()
			ReplicatedStorage.Remotes.Player.HeadMovement:FireServer(targetCFrame)
		end
	else
		Neck.C0 = YOffset
	end
end)


1 Like