Character spins when the waist's C0 is changed to make the character look down

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 :slight_smile:

I don’t have much experience with altering C0s but I think your main issue would be the fact that you are updating it based on the current position instead of basing off of a set position

Changing it based on, say, a previous C0 would create a feedback loop like that where it continually adds trying to achieve an unreachable C0, but if you based it off like the original C0 then it might fix that problem.

I don’t specifically know the code that would give you that result though, sorry

1 Like