Smooth camera. Lerp and springs

Hi everyone, just yesterday I posted a post about how I want to make a smooth third-person camera, with a fixed cursor in the center of the screen. At this stage, I am interested in the smoothness of the camera. By smoothness, I mean what you can see in the examples of my previous post
POST: Problem with the smooth camera

In the comments, I was answered and advised to watch a video about springs, but although the answer lay on the surface, for me this material seemed difficult. :frowning_face:

I also want to show you exactly where I would like to implement such a camera:


As you can see, the camera is very sharp and unnatural. I’d like to make camera heavy and a bit laggy

I would like to hear the opinion of people who understand the properties of the camera, how best to make the smoothness. I also wanted to know how Lerp and springs work.

1 Like
local humanoidpart = character.HumanoidRootPart
local CamPos,TargetCamPos = Camra.CoordinateFrame.p,Camra.CoordinateFrame.p 
local AngleX,TargetAngleX = 0,0
local AngleY,TargetAngleY = 0,0

local Sensetivity = 3.5
local Smooth = 0.7
local MaxAngle = 85

InputS.InputChanged:Connect(function(Int)
	if Int.UserInputType == Enum.UserInputType.MouseMovement then
		local delta = Vector2.new(Int.Delta.x/Sensetivity,Int.Delta.y/Sensetivity)

		local X = TargetAngleX - delta.y 
		TargetAngleX = (X >= MaxAngle and MaxAngle) or (X <= -MaxAngle and -MaxAngle) or X 
		TargetAngleY = (TargetAngleY - delta.x) % 360 
	end	
end)

game["Run Service"].RenderStepped:Connect(function(dt)
	InputS.MouseBehavior = Enum.MouseBehavior.LockCenter
	CamPos = CamPos + (TargetCamPos - CamPos) *0.28 
	AngleX = AngleX + (TargetAngleX - AngleX) *Smooth
	local dist = TargetAngleY - AngleY 
	dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist 
	AngleY = (AngleY + dist *Smooth) %360
	Camra.CameraType = Enum.CameraType.Scriptable

	Camra.CoordinateFrame = CFrame.new(humanoidpart.Position + Vector3.new(0,1.5,0))
		* CFrame.Angles(0,math.rad(AngleY),0) 
		* CFrame.Angles(math.rad(AngleX),0,0)
		* CFrame.new(0,0,0)

	humanoidpart.CFrame=CFrame.new(humanoidpart.Position)*CFrame.Angles(0,math.rad(AngleY),0)
	character.Humanoid.AutoRotate = false
end)

this should partially fix your problem, though you should tweak this to fit your type of camera

5 Likes

or you could always have a target position you want to camera to go to and lerp the current cframe
like cframe:lerp()

1 Like

Oh, I have a problem. The camera seems to be stuck in the player

Camra.CoordinateFrame = CFrame.new(humanoidpart.Position + Vector3.new(0,1.5,0))
		* CFrame.Angles(0,math.rad(AngleY),0) 
		* CFrame.Angles(math.rad(AngleX),0,0)
		* CFrame.new(0,0,0) -- Change the cframe value to change the value of the camera
1 Like

I changed the coordinates, but it looks a bit like this… strange…


Is it possible to achieve this effect? https://www.roblox.com/games/3907722122/SCP-Anomaly-Breach

instead of that you can get the Camera.CFrame.LookVector.Unit
then multiply that by anything and that should get the effect you want except in the 3rd person view

Camra.CoordinateFrame = CFrame.new(humanoidpart.Position + Vector3.new(0,1.5,0))
* CFrame.Angles(0,math.rad(AngleY),0)
* CFrame.Angles(math.rad(AngleX),0,0)
* Camera.CFrame.LookVector.Unit * 6

1 Like

OH GOD! Sorry pls. I just rechecked your script, tweaking it a bit, and it turned out to work exactly as I wanted it to! I am incredibly grateful! Thanks

I’d like to ask. How can I better change the camera’s CFrame in the future, such as when a key is pressed?

What do you mean by this?
Like offset / camera shake or cutscene?

when pressing ctrl, I want the camera to go down a little bit. I already have the form

local isCrawling = false

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		if not isCrawling then
      ---lower the camera slightly
			isCrawling = true
		else
   ---restore the original CFrame
			isCrawling = false
		end
	end
end)
 local Offset = Vector3 or cframe
 local isCrawling = false
 
-- in the camera cframe code stuff just add the vector or * the cframe


 uis.InputBegan:Connect(function(input, gamepro)
 	if input.KeyCode == Enum.KeyCode.LeftControl and not gamepro then
 		if not isCrawling then
       ---lower the camera slightly
-- tween the vector or cframe or something
 			isCrawling = true
 		else
    ---restore the original CFrame
    -- tween the vector or cframe or something
 			isCrawling = false
 		end
 	end
 end)