Why does my camera not smoothly tilt?

Hey there! I have made a wallrun script, which works perfectly fine, other than the camera tilting. It does tilt, but it’s very snappy and doesn’t go smoothly.

Here’s the piece of code that makes the camera tilt:

local tiltLoop = game:GetService("RunService").RenderStepped:Connect(function()
	if wallL then
		cam.CFrame = cam.CFrame:Lerp(CFrame.lookAt(cam.CFrame.Position, cam.CFrame.Position + cam.CFrame.LookVector) * CFrame.Angles(0, 0, math.rad(-10)), .3)
	elseif wallR then
		cam.CFrame = cam.CFrame:Lerp(CFrame.lookAt(cam.CFrame.Position, cam.CFrame.Position + cam.CFrame.LookVector) * CFrame.Angles(0, 0, math.rad(10)), .3)
	end
end)

As you can see I’ve tried using Lerp, but it didn’t work. I’ve tried tweening as well (outside of the loop), but that didn’t work either. Any way I can do this? Any help is appreciated!

2 Likes

:Lerp() gets the in-between of two points so it’s not animated whatsoever it just instantly snaps there you could perhaps put it in a for loop


For i=.1,1,.1 do
--:Lerp() goes here, and add a wait or it's just gonna do it in a millisecond 
end

Hopefully this was of help

2 Likes

Hm, that didn’t seem to work.

New code:

local function TiltCamera(bool)
	if bool then
		tiltLoop = game:GetService("RunService").RenderStepped:Connect(function()
			if wallL then
				for i=.1,1,.1 do
					cam.CFrame = cam.CFrame:Lerp(CFrame.lookAt(cam.CFrame.Position, cam.CFrame.Position + cam.CFrame.LookVector) * CFrame.Angles(0, 0, math.rad(-10)), .3)
					wait(.01)
				end
			elseif wallR then
				for i=.1,1,.1 do
					cam.CFrame = cam.CFrame:Lerp(CFrame.lookAt(cam.CFrame.Position, cam.CFrame.Position + cam.CFrame.LookVector) * CFrame.Angles(0, 0, math.rad(10)), .3)
					wait(.01)
				end
			end
		end)
	else
		if tiltLoop then
			tiltLoop:Disconnect()
			tiltLoop = nil
		end
	end
end

I placed it into a function as well. Should I remove the RenderStepped loop?

1 Like

Try tweening the camera’s position instead of setting it, so it doesn’t snap. Make sure the time is short, around 0.25 seconds.

1 Like

Oh it’s on a RenderStepped event, my bad, i think it may be trying to do it every frame, if it is so then a debounce on the for loop may fix it

1 Like

I believe a Heartbeat event would solve it right?
I saw it in a tutorial, seemed to be working out for the guy

1 Like

I don’t think it would fix it but i could be missing something

1 Like

For some reason Heartbeat stops it from working at all.

1 Like

Tweening makes it act very strange, it makes the camera stay in place for a split second

1 Like

Debounce makes the camera tilt and go back to it’s position really quickly, since the renderstepped loop makes sure the rotation gets changed every frame. If I stop the loop, the camera automatically goes back to normal.

The for loop does take .1 seconds to be completed .01 secs is practically the same as it not being there
You can reduce the increase per loop or up the wait, lower the increase smoother it is

I feel like i may be missunderstanding something, let me know if so.

1 Like

So this is what I have done, and it seems to have worked!

local function TiltCamera(bool)
	if bool then
		tiltLoop = game:GetService("RunService").RenderStepped:Connect(function()			
			if wallL then
				cam.CFrame = cam.CFrame:Lerp(CFrame.lookAt(cam.CFrame.Position, cam.CFrame.Position + cam.CFrame.LookVector) * CFrame.Angles(0, 0, math.rad(-tiltL)), .3)
			elseif wallR then
				cam.CFrame = cam.CFrame:Lerp(CFrame.lookAt(cam.CFrame.Position, cam.CFrame.Position + cam.CFrame.LookVector) * CFrame.Angles(0, 0, math.rad(tiltR)), .3)
			end
		end)	 
		
		delay(0, function()
			while wallrunning and tiltL < 10 and tiltR < 10 do
				tiltL += increment
				tiltR += increment
				wait(.01)
			end
		end)
	else
		if tiltLoop then
			while tiltL > 0 and tiltR > 0 do
				tiltL -= 10/3
				tiltR -= 10/3
				wait(.01)
			end
			tiltLoop:Disconnect()
			tiltLoop = nil
		end
	end
end

I basically just increase the tilt in a while loop with a set increment (in my case increment=2). I put in inside of a delay, so the rest of the code after function could run. For some reason the lerp does work in this method and smooths it out.

(Edit: I’ve made exiting the wallrun smoother as well)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.