Hello, I have been working on a system that modifies the C0 of a player’s character to make a gun aim exactly at the mouse, however, I have a bug I can’t find a fix for:
It happens only when I aim down
This is the main part of the code:
--Putting the position behind the character to prevent bugs
local handleCF = handle.Attachment.WorldCFrame:ToWorldSpace(CFrame.new(0, 0, 30))
--Calculating the needed value of C0 to point at the target
local targetInHandle = handleCF:ToObjectSpace(CFrame.new(mouse.Hit.Position))
local targetInC0 = waistAtt.C0:ToWorldSpace(targetInHandle).Position
local cf = CFrame.lookAt(waistAtt.C0.Position, targetInC0) --result
--Limiting the rotation down and up
local x, y, z = cf:ToOrientation()
local limitedX = math.clamp(x, -1.37, 1.3)
waistAtt.C0 = CFrame.new(cf.Position) * CFrame.fromOrientation(limitedX, y, z)
Note: the same thing happens when I don’t use :ToOrientation() to limit the cframe, and a lot more glitchy
All help is appreciated