Need help with weapon "drifting" away from arms

Ayo. I’ve been working on a gun system for a survival game I’m making, and made a post a while ago about it asking for help with arm movement here, but I’ve since moved on from where I was going with that (many thanks to CoderQwerty for his help though) and recently managed to find a better alternative.

The method I stumbled upon changes the character’s shoulder Motor6Ds in the torso to make their arms point relative to where their mouse is. I thought I’d combine this with my gun’s Motor6D to achieve the effect I wanted, but the gun seems to drift the further away it is from the default animation position, being the middle.

I don’t think I would run into this problem if I had it Motor6D’d to the arm, but unfortunately the Motor6D is located in the HumanoidRootPart, which is required for my animations to work.

The original code wasn’t mine (I edited it a little with my miniscule knowledge about math and CFrame) and didn’t know the solution for the gun drifting, so I’m asking here. Thanks!

Code
local RunService = game:GetService("RunService")
local character = game.Players.LocalPlayer.Character
local mouse = game.Players.LocalPlayer:GetMouse()

RunService.RenderStepped:Connect(function()
	character.Torso["Left Shoulder"].C0 = CFrame.new(-1, 0.5, 0) * CFrame.Angles(math.asin((mouse.Hit.p - mouse.Origin.p).unit.y), -1.55, 0)
	character.Torso["Right Shoulder"].C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(math.asin((mouse.Hit.p - mouse.Origin.p).unit.y), 1.55, 0)
	character.HumanoidRootPart.Handle.C0 = CFrame.new(0, 0, 0) * CFrame.Angles(math.asin((mouse.Hit.p - mouse.Origin.p).unit.y), 0, 0)
end)
4 Likes

Not sure what you are trying to get rid of, I will assume that you want to get rid of that gap between the gun and your left arm.

image

From what I can see, it looks like the gun isn’t directly connected to the arms, but rather, the humanoid root?
image

I think there’s several solutions to this.
You can connect the weapon’s Motor6D to your right arm instead of your humanoid root.
You can limit the upper and lower angles of your character aim.
You can do some CFrame offset components to correct for this slight deviation.

Although, I’m not sure what the setup is like, so I’m not sure which one is best for this. Can you send a screenshot of the explorer hierarchy of your character model and a GIF of you in first person with the gun looking up and down where it deviates?

1 Like

It’s actually a lot easier to see the problem in first-person, probably should’ve included that originally.

As for the right arm Motor6D, I had tried that with better results, but the arms rotate at an angle when looking up or down.

Here’s the character’s hierarchy:

I think the reason is because their center of rotation is not aligned to the same point. The shoulder C0 coordinates are given as CFrame.new(-1, 0.5, 0) and CFrame.new(1, 0.5, 0), but the root part at the handle is given as CFrame.new(0, 0, 0)

image

Even though they share the same angles, rotating them will cause slight deviations in the gun’s ‘forward’ and ‘up’ direction.

Although, in the code, your gun should be inside your root part since I’m not seeing any offsets for it. Are you running an animation on the gun?

image

Assuming you are running an animation on it to move the gun forward, on the line:

character.HumanoidRootPart.Handle.C0 = CFrame.new(0, 0, 0) * CFrame.Angles(math.asin((mouse.Hit.p - mouse.Origin.p).unit.y), 0, 0)

You should change
CFrame.new(0, 0, 0)
to
CFrame.new(0, 0.5, 0)

and update your animation accordingly.

Could you show me the script you used to move your arms with the camera? I’m making a first-person game with fake arms, and I want others to see where I’m pointing, you would help me a lot if you passed me that script.