Simple Character Waist Rotation

Howdy. I’m looking for the shortest most simple way to rotate the character towards the camera from the waist with limits. I’m terrible with CFrame and math and was wondering if this function could be better? I’m not the original maker. I added the bottom lines. Is any of the code unnecessary?

local chr = script.Parent
local waist = chr:WaitForChild("UpperTorso"):WaitForChild("Waist")
local ev = chr.Cam

local maxAngle = math.rad(45)

function step()
	local look, origin = workspace.CurrentCamera.CFrame, chr.HumanoidRootPart.CFrame
	look = look:ToWorldSpace(CFrame.new(0, 1.5, 0))
	local x,y,z = look:ToEulerAnglesXYZ()
	local rx,ry,rz = origin:ToEulerAnglesXYZ()
	
	x = math.clamp(x, -72, 72)
	local newLook = CFrame.Angles(x,y,z)
	local newOrig = CFrame.Angles(rx,ry,rz)
	newLook = newOrig:ToObjectSpace(newLook)
	
	--ev:FireServer(newLook)
	--waist.C0 = waist.C0:lerp(CFrame.new(waist.C0.p) * newLook, 0.1)
	
	
	--I added all this to limit the rotation
	local lookAt = CFrame.new(waist.C0.p) * newLook

	local x,y,z = lookAt:ToOrientation()
	waist.C0 = waist.C0:Lerp(CFrame.new(lookAt.p) * CFrame.fromOrientation(x,math.clamp(y,-maxAngle,maxAngle),z),0.1)
end

game:GetService'RunService'.RenderStepped:connect(step)

Everything works good now, I’m just looking to improve!

Doesn’t look like there is any issue so far, but you might watch out for problems such as Player.Character suddenly disappearing(and entire console log growling angrily with error messages). Most commonly due to falling off into or flinging into void.

I noticed removing “x = math.clamp(x, -72, 72)” doesn’t effect anything. Do you think it was an attempt from the original maker to limit the waist rotation?

Try looking up and down, you will see how far you can go limbo!!! Naturally, the clamping is just to not “butcher” characters into unrealistic movement. It’s like a design choice. :slight_smile:

Changing the clamping numbers doesn’t actually effect how far you can bend. That’s why I suspect it to be an unfinished feature.

Never mind, it’s just because the natural camera angle limitation is in parity with those numbers, so it would never do that.

Here’s a reference image of a ramp with 72° angle. I moved my camera down as far as I could.