Need help with calculating shoulder offset

I’m working on a shooter game, in which all the tools force you into first person and make your actual characters arms follow the camera. The characters are R6 rigs.

I want to make it clear, that there are NO viewmodels being used, just the real, physical arms of your character that everybody sees.

Some tools are guns and can be aimed with.
When aiming, the left and right shoulders Motor6Ds C0 property should be changed in such a way, that the AimPart, which is inside and welded to the tool, is perfectly aligned with the camera.
While the AimPart is mostly behind the sights of the gun, it shouldn’t matter where it’s positioned. The script should always move the arms, so the AimPart ends up at the camera, using the shoulders parented to the torso.

I’m having trouble calculating the Position of where the shoulders should be in WorldSpace.
I just need the calculated WorldPosition and Rotation. It gets converted into the torsos ObjectSpace afterwards.
Here’s how the code looks like right now:

local origin = rightArm.CFrame * CFrame.new(0, 1, 0)
local aimOffset = aimPart.CFrame:PointToObjectSpace(origin.Position)

local right_offset = camCF * CFrame.new(aimOffset) * CFrame.Angles(aimPart.CFrame:ToObjectSpace(camCF):ToEulerAnglesXYZ())
local left_offset = right_offset * CFrame.new(Vector3.new(-2, 0, 0))

rightShoulder.C0 = rightShoulder.C0:lerp(torso.CFrame:ToObjectSpace(right_offset) * CFrame.Angles(0, math.pi/2, 0), speed)
leftShoulder.C0 = leftShoulder.C0:lerp(torso.CFrame:ToObjectSpace(left_offset) * CFrame.Angles(0, -math.pi/2, 0), speed)

Anything below the declaration of aimOffset is i think fine. It’s just aimOffset and/or origin that needs to be changed.
There could be a solution that also changes the other stuff, so feel free to post it.
left_offset has to have that offset of (-2, 0, 0).
The arms have to point towards the direction your camera is facing.

I have searched for solutions to my problem on the developer forum, but found none that worked.
I have tried calculating it in many different ways, but none worked.

1 Like

Couldn’t you use Humanoid:CameraOffset?
If not, apologies.

You can provide offset vectors from the gun itself, instead of relying on a part, and you can transform that vector easily (Example: A sniper scope on a gun that usually has Iron sights)

You could also pair that with a third-person animation of your character’s head moving into position, mimicking how you would with a real shoulder-fired rifle, instead of having your character’s arms move, potentially avoiding visual glitches.

Personally I would only recommend moving the arms only in a View-Model, but this is just me looking from the outside in.

No i can’t use Humanoid.CameraOffset, since I want the gun move in front of the camera (through the shoulders), and not the camera to the gun. My goal is to move the arms (on the client), so the aimpart, which is welded to the gun that’s welded to the arm, ends up exactly at the camera, through changing C0 properties of the shoulders parented to the Torso. I forgot to mention that i am using R6 rigs.

I know it may be better with a viewmodel, but this is what i’m using right now. If i were to change to viewmodels, i would have to create them for every single tool that exists in my game and make
new animations, which i don’t want to do.