Clamping a CFrame

I’m currently working on some gun system and I’m not really able to correctly limit the cframe or more precisely its angles in order to make it look realistic.

I’ve been thinking about using CFrame:ToOrientation in order to do that, but I think that it would be problematic since when you get past 180 degrees it goes to -180.

So there are actually two questions : How to properly determine if a CFrame should be clamped, and based on the solution to this question, how to apply this clamp because it doesn’t look obvious cframe-speaking so I assume I’ll deal with vectors.

I don’t know if it can help but here is the code for the aiming so far, barely nothing.

local function equip()
	armWeld.Enabled = true
	runService:BindToRenderStep("aim",Enum.RenderPriority.Character.Value,function()
		local armCFrame = CFrame.new(char.UpperTorso.Position,mouse.Hit.Position)*CFrame.Angles(math.pi/2,0,0)
		armWeld.C0 = armOffset*char.UpperTorso.CFrame:ToObjectSpace(armCFrame)
		
		local x,y,z = armCFrame:ToOrientation()
		local charX,charY,charZ = char.PrimaryPart.CFrame:ToOrientation()
		
		print(math.deg(y),math.deg(charY))
	end)
end

I’m not that good with CFrames, but you could add a check if it’s below a specific angle and if yes, then clamp. Sorry, it’s really not detailed, but that’s basically how you could do it, hope someone else can answer better than me.

I’m pretty sure you should be able to use some trig, because it autoclamps itself

Video of what I’m talking about, you should be able to implement what he did in your system:

credit @okeanskiy

1 Like