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)
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.
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