:Lerp not working?

I am trying to tilt the player’s camera when they press a certain key, I managed to get the camera to rotate and wanted to add a :Lerp function to smooth it out. However, instead of smoothly tilting the camera, it immediately snaps into place.

I’m not very familiar with :Lerping, so I could be doing something completely wrong here.

local Camera = game.Workspace.CurrentCamera

game:GetService("RunService").RenderStepped:Connect(function()
if true then
		local tilting = false
		local x, y, z = Camera.CFrame:ToEulerAnglesXYZ()

		if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.E) then
			tilting = true

--------------Won't Lerp------------------------------------------------------------------------------------------------------------------------
			Camera.CFrame = Camera.CFrame:Lerp(CFrame.new(Camera.CFrame.Position) * CFrame.Angles(x, y, z) * CFrame.Angles(0, 0, math.rad(-5)), 1)
------------------------------------------------------------------------------------------------------------------------------------------------
		end
	end

end)
``

lerp is basically like the steps of interval or something of a part but since im terrible at explaining, ill simply provide an example, if you set the alpha number to 0.5, it would be in the middle of the distance between where it previously was and the set target

if it were to be 0, it would be in the same place as where its at which means it has not reach a point in interpolating towards the target, if it was to be 1 it would be at the exact place of the supposed position but the difference between the two is that tween will be automatic as provided by its tweenInfo but for lerping, you have to make a function which will at a rate increase the alpha number (0 to 1) as an example is

for i = 0, 1, 0.01 do -- starts at 0 and ends at 1, 0.01 adds to the 0 and stacks upon until at 1
   Camera.CFrame = Camera.CFrame:Lerp(CFrame.new(Camera.CFrame.Position) * CFrame.Angles(x, y, z) * CFrame.Angles(0, 0, math.rad(-5)), i) -- i is the starting one, it will be the same as the former 0 but it increases and that's what does the trick
   wait(.1) -- every .1 second, it will repeat thus conceptually, 0 will be added with another 0.01 and so forth until the marked end
end

-- in simpler terms, the for loop will repeat 100 times in 10 seconds so lessen the wait() if you wish for quicker and shorter seconds to wait

more detailed explanation of lerp What is the :lerp function? - #22 by XdJackyboiiXd21

2 Likes

I tried applying this to my code, it resulted in the screen just jittering around.

local Camera = game.Workspace.CurrentCamera

game:GetService("RunService").RenderStepped:Connect(function()
if true then
		local tilting = false
		local x, y, z = Camera.CFrame:ToEulerAnglesXYZ()

		if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.E) then
			a = true
			for i = 0, 1, 0.1 do
				Camera.CFrame = Camera.CFrame:Lerp(CFrame.new(Camera.CFrame.Position) * CFrame.Angles(x,y,z) * CFrame.Angles(0,0,math.rad(-5)), i)
				wait(0.1)
				end
		end
		end
	end

end)

if you do this every frame then its going to create a bunch of overlapping loops lerping the camera resulting in the jittering around.

ultimately i dont think lerping here is good because you would also have to store / keep track of the unlerped cframe.
it would probably be easiest to transform the camera.CFrame every frame based on deltaTime while making sure to clamp the rotation.

1 Like

Sorry for responding a little late, but how would I go about this?

turns out i was mistaken. roblox actually untransforms your camera every frame if you have camera mode set to default, but here is a script

local runservice= game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
local rotation = 0
local rotSpeed = 30
local minRotation = 0
local maxRotation = 70
runservice.RenderStepped:Connect(function(dt)
	local direction = userInputService:IsKeyDown("E") and 1 or -1
	rotation+=rotSpeed*dt*direction
	rotation = math.clamp(rotation, minRotation, maxRotation)

	camera.CFrame *= CFrame.Angles(0, 0, math.rad(rotation))
end)
1 Like