Cframe.Angles() not setting Y to 0 properly

I’m trying to make a HeadMovement system specifically for First-Person. I’m trying to set the neck joint orientation to the cameras orientation. Its working great in Third-Person if the body’s Y axis (Rotation) isn’t changing, but when i go in First-Person if i look behind me my head turns 180 degrees. This does look pretty funny but its not ideal for what i’m trying to do.
Script: (Server, Local isn’t important)

local event = script.CameraPos
local char = script.Parent
local neck = char:WaitForChild("Head").Neck
local ts = game:GetService("TweenService")
local tsinfo = TweenInfo.new(0.2,Enum.EasingStyle.Sine)

script.CameraPos.OnServerEvent:Connect(function(plr,cf)
	local sx, sy, sz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = cf:GetComponents()

	local X = math.atan2(-m12, m22)

	local Y = math.asin(m02)

	local Z = math.atan2(-m01, m00)
	
	--neck.C0 = CFrame.Angles(X,0,Z) + neck.C0.Position
	ts:Create(neck,tsinfo,{C0 = CFrame.Angles(X,0,Z) + neck.C0.Position}):Play() -- MOST LIKELY WHERE THE PROBLEM IS!!
end)

I’ll be glad if this could get fixed.

Also the value “cf” is the camera’s full CFrame.

from what i’ve picked up after reading and i am guessing, does it move because the whole body is rotating in first-person and then the head rotates also? now, i could be wrong but is that maybe the case?

i would update it to set the position to be relative to the HumanoidRootPart if that is the cause.

Yes, its rotating relative from the Hrp’s rotation. I was also thinking that. Ill try code that. Ill update over more problems.

1 Like

May be the most stupid fix but it works, and i cant think of anything else.

Updated Code:

local event = script.CameraPos
local char = script.Parent
local neck = char:WaitForChild("Head").Neck
local ts = game:GetService("TweenService")
local tsinfo = TweenInfo.new(0.2,Enum.EasingStyle.Sine)

local attachment = Instance.new("Attachment")
attachment.Parent = char:WaitForChild("Head")
attachment.Name = "NeckAttachment"

script.CameraPos.OnServerEvent:Connect(function(plr,cf)
	local sx, sy, sz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = cf:GetComponents()

	local X = math.atan2(-m12, m22)

	local Y = math.asin(m02)

	local Z = math.atan2(-m01, m00)
	
	attachment.WorldCFrame = CFrame.Angles(X,Y,Z)
	
	local rotationcframe = attachment.CFrame.Rotation
	
	local fullcframe = rotationcframe + neck.C0.Position
		
	ts:Create(neck,tsinfo,{C0 = fullcframe}):Play()
end)
1 Like

I probably would’ve done this now that I thought of it (this might or might not work, i cant test it in studio right now)

script.CameraPos.OnServerEvent:Connect(function(plr, cf)
    local char = script.Parent
    local neck = char:WaitForChild("Head").Neck
    local ts = game:GetService("TweenService")
    local tsinfo = TweenInfo.new(0.2, Enum.EasingStyle.Sine)

    -- Get the character's torso (or root part) orientation
    local root = char:WaitForChild("HumanoidRootPart")
    local rootCFrame = root.CFrame
    local relativeCFrame = rootCFrame:ToObjectSpace(cf)

    -- Decompose the relative CFrame to angles
    local sx, sy, sz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = relativeCFrame:GetComponents()
    local X = math.atan2(-m12, m22)
    local Y = math.asin(m02)
    local Z = math.atan2(-m01, m00)

    local targetC0 = CFrame.Angles(X, Y, Z) + neck.C0.Position
    ts:Create(neck, tsinfo, {C0 = targetC0}):Play()
end)

but after all, this is just an improvement to this

or at least just a change

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.