Improve my math to make it more accurate please?

I am trying to make the character’s arms follow the CurrentCamera with SetDesiredAngle and some fake arms. I made some gimmicky formula thingy to set the angle correctly, but sometimes the math is off (Camera looks down, arms go up). Could someone improve my formula? I’ve heard that trigonometry could solve the problem but I’m not exactly sure how to get side lengths (my only experience with trig is basically 5 lessons of Khan Academy).

repeat wait() until game.Players.LocalPlayer

frames = setmetatable({},{})
player = game.Players.LocalPlayer
character = player.Character
cam = Workspace.CurrentCamera

frames.Default = {
    Right = CFrame.new(1.5,-1,-1.5)*CFrame.Angles(math.rad(90),0,0);
    Left = CFrame.new(-1.5,-1,-1.5)*CFrame.Angles(math.rad(90),0,0)
    }

frames.Gun = {
    Right = CFrame.new(1,-1,-1)*CFrame.Angles(math.rad(90),0,0);
    Left = CFrame.new(-0.5,-1,-1.5)*CFrame.Angles(math.rad(90),0,math.rad(30))
}

getmetatable(frames).__index = {}
getmetatable(frames).__index.CurrentFrame = frames.Default

rarm = character["Right Arm"]
larm = character["Left Arm"]
frarm = rarm:Clone()
flarm = larm:Clone()

frarm.Parent = cam
flarm.Parent = cam

player.CameraMode = "LockFirstPerson"

game:GetService("RunService").RenderStepped:connect(function()
    frarm.CFrame = cam.CoordinateFrame*frames.CurrentFrame.Right
    flarm.CFrame = cam.CoordinateFrame*frames.CurrentFrame.Left
    character.Torso["Right Shoulder"]:SetDesiredAngle(math.rad(frarm.Rotation.X)-(math.rad(frarm.Rotation.X)-math.rad(90))*2) -- Math for Right Arm
    character.Torso["Left Shoulder"]:SetDesiredAngle(math.rad(-flarm.Rotation.X)+(math.rad(flarm.Rotation.X)-math.rad(90))*2) -- Math for Left Arm
end)

You should probably clamp the values so that it doesn’t get near 90 degrees.
I believe I’ve heard the term gimbal lock describing camera/CFraming values if they get to 90 degrees causing the CFrame to flip around.

Sort of a side note, but what’s with all the metatables and improper uses?

1 Like