I want to achieve the characters head and arms rotating where the player is looking no matter how is the torso rotated.
It works fine when: the torso isnt rotated by animation so its looking forward, the gun is being held and aimed in the direction where the torso is facing if that makes sense.
I suck at this math so i tried getting help from AI and checked different posts, didnt find a solution
video of whats happening:
local function getNeck(character, humanoid)
if not character or not humanoid then return end
if humanoid.RigType == Enum.HumanoidRigType.R6 then
local torso = character:FindFirstChild("Torso")
return torso and torso:FindFirstChild("Neck")
else
local head = character:FindFirstChild("Head")
return head and head:FindFirstChild("Neck")
end
end
local function getShoulders(character, humanoid)
if not character or not humanoid then return end
if humanoid.RigType == Enum.HumanoidRigType.R6 then
local torso = character:FindFirstChild("Torso")
return torso and torso:FindFirstChild("Left Shoulder"), torso and torso:FindFirstChild("Right Shoulder")
else
local leftUpperArm = character:FindFirstChild("LeftUpperArm")
local rightUpperArm = character:FindFirstChild("RightUpperArm")
return leftUpperArm and leftUpperArm:FindFirstChild("LeftShoulder"), rightUpperArm and rightUpperArm:FindFirstChild("RightShoulder")
end
end
local function getDefaultNeckCFrame(humanoid)
if humanoid.RigType == Enum.HumanoidRigType.R6 then
return CFrame.new(0, 1, 0, -1, 0, 0, 0, 0, 1, 0, 1, -0)
else
return CFrame.new(-5.96046448e-08, 0.800017118, 1.1920929e-07, 1, 0, 0, 0, 1, 0, 0, 0, 1)
end
end
local function getDefaultShoulderCFrames(humanoid)
if humanoid.RigType == Enum.HumanoidRigType.R6 then
return CFrame.new(-1, 0.5, 0) * CFrame.Angles(0, -1.55, 0), CFrame.new(1, 0.5, 0) * CFrame.Angles(0, 1.55, 0)
else
return CFrame.new(-1, 0.56301713, 1.1920929e-07, 1, 0, 0, 0, 1, 0, 0, 0, 1), CFrame.new(0.99999994, 0.56301713, 1.1920929e-07, 1, 0, 0, 0, 1, 0, 0, 0, 1)
end
end
local function getShoulderAimCFrame(player, humanoid)
local defL, defR = getDefaultShoulderCFrames(humanoid)
local defNeck = getDefaultNeckCFrame(humanoid)
local isAiming = player == LocalPlayer and (WeaponData[player] and WeaponData[player].Aiming) or player.Character:GetAttribute("Aiming")
local angle = isAiming and math.rad(10) or 0
local aimTilt = CFrame.Angles(0, angle, 0)
local stnL, stnR = defL:Inverse() * defNeck, defR:Inverse() * defNeck
return stnL * aimTilt * stnL:Inverse(), stnR * aimTilt * stnR:Inverse()
end
function CharacterLookController.updatePlayer(player, lookDirection, headLook, armsLook, lerpWeight, dt)
local character = player.Character
if not character then return end
local humanoid = character:FindFirstChild("Humanoid")
local hrp = character:FindFirstChild("HumanoidRootPart")
local torso = character:FindFirstChild("Torso") or character:FindFirstChild("UpperTorso")
local neck = getNeck(character, humanoid)
local lS, rS = getShoulders(character, humanoid)
if not (humanoid and neck and lS and rS and hrp and torso) then return end
local moveData = DirectionalMovement.GetOffsets(character, dt)
local rootJoint = hrp:FindFirstChild("RootJoint")
if rootJoint and moveData then
rootJoint.C0 = moveData.RootC0
end
local localLookDir = torso.CFrame:VectorToObjectSpace(workspace.CurrentCamera.CFrame.LookVector)
local verticalAngle = math.asin(localLookDir.Y)
local neckBase = getDefaultNeckCFrame(humanoid)
local moveNeck = moveData and moveData.NeckTilt or CFrame.new()
local targetNeckC0 = neckBase * moveNeck
if headLook then
local isAiming = player == LocalPlayer and (WeaponData[player] and WeaponData[player].Aiming) or character:GetAttribute("Aiming")
local aimTilt = isAiming and math.rad(15) or 0
local lookCFrame = CFrame.Angles(humanoid.RigType == Enum.HumanoidRigType.R6 and -verticalAngle or verticalAngle, -aimTilt, 0)
targetNeckC0 = neckBase * moveNeck * lookCFrame
end
neck.C0 = neck.C0:Lerp(targetNeckC0, lerpWeight)
local defL, defR = getDefaultShoulderCFrames(humanoid)
local moveShoulder = moveData and moveData.ShoulderTilt or CFrame.new()
local targetL, targetR = defL * moveShoulder, defR * moveShoulder
if armsLook then
local AimL, AimR = getShoulderAimCFrame(player, humanoid)
local transL, transR
if humanoid.RigType == Enum.HumanoidRigType.R6 then
transL, transR = CFrame.Angles(0, 0, -verticalAngle), CFrame.Angles(0, 0, verticalAngle)
end
targetL = defL * moveShoulder * AimL * transL
targetR = defR * moveShoulder * AimR * transR
end
lS.C0 = lS.C0:Lerp(targetL, lerpWeight)
rS.C0 = rS.C0:Lerp(targetR, lerpWeight)
end
Any help is appreciated im really stuck on this