Gun inverts as character rotates

See video:

The problem is rather obvious.

game:GetService("RunService").RenderStepped:Connect(function()
	if currentanimidle and char then -- just to check if the player is holding a gun and if it exists in the first place
		
		local origin = char.Head.NeckRigAttachment.WorldPosition
		local x,y,z = CFrame.new(origin,mouse.Hit.p):ToEulerAnglesXYZ()
		
		print(math.deg(x))
		
		local char = plr.Character
		char.LeftUpperArm.LeftShoulder.C0 = CFrame.new(char.LeftUpperArm.LeftShoulder.C0.p) * CFrame.Angles(x,0,0)
		char.RightUpperArm.RightShoulder.C0 = CFrame.new(char.RightUpperArm.RightShoulder.C0.p) * CFrame.Angles(x,0,0)
	end
end)

Help is appreciated :slight_smile:

With euler angles, a single orientation can be represented in multiple ways

For example, 180,0,180 is technically same as 0,180,0

When looking at just the X value of the euler angle, the program will think 180,0,180 is pointing straight backwards and upside down, and it will think 0,180,0 is completely flat at 0 degrees, even though theyre the same angle

I believe your problem is originating from the use of ToEulerAnglesXYZ, which calculates the x, or pitch value before anything else
While this may seem like what you want because youre focusing only on the X value, it actually isnt
You want to use a Y-first function like ToEulerAnglesYXZ, because it will calculate the side-to-side angle of the gun before it calculates the up and down

Thinking back to the original example, you can see how
“180,0,180” is calculated with that X first mentality, resulting in the mesh being completely backwards
“0,180,0” calculated the Y first, resulting in the game realizing the gun was just turned around, not tilted 180 degrees backwards

2 Likes