Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my camera shaking/stuttering near the end of this rotation?

Whenever I try to rotate the camera around a part, the camera starts to shake/stutter near the end of the rotation. Any ideas why?

local Part = workspace.Part
local UIS = game:GetService('UserInputService')
local Camera = workspace.CurrentCamera
local newY = 0
local currentY = 0
local isTurning = false

Camera.CameraType = Enum.CameraType.Scriptable
Camera.CameraSubject = Part

game:GetService('RunService').RenderStepped:connect(function()
	Camera.CoordinateFrame = 	  CFrame.new(Part.Position) 
								* CFrame.Angles(0, currentY, 0)
								* CFrame.new(0, 0, 10)
								
	if currentY < newY then
		currentY = currentY + math.rad(1.5)
	elseif currentY > newY then
		currentY = currentY - math.rad(1.5)
	end
end)
	
UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		newY = newY + math.rad(90)
	elseif input.KeyCode == Enum.KeyCode.E then
		newY = newY - math.rad(90)
	end
end)

1 answer

✓ Accepted answer

Because unless newY is a perfect multiple of 1.5, currentY is going to flip back and forth between being above and below newY every frame. Imagine currentY = 59 and newY = 60, so the rotation is almost complete. The next frame, currentY = 60.5. The next frame, since it's now above newY, currentY = 59 again. This will continue, them forever flipping back and forth.

To fix, change your if statements to this:

if (math.abs(currentY-newY) < math.rad(1.5)) then -- If currentY is within 1.5 degrees from newY,
	currentY = newY -- Just set it to newY
elseif (currentY < newY) then
	currentY = currentY + math.rad(1.5);
else
	currentY = currentY - math.rad(1.5);
end

There are probably better ways to do it, but this should work, in the end.

This discussion is preserved as it was. New answers, comments, and votes are closed.