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!