Need help with smooth camera movement

I am making a camera script for my game, but don’t know how to make it smooth.

RunService.RenderStepped:Connect(function(delta)
	for i = 0, 1, 1 - CAMERA_LERP_SMOOTHNESS do
		camera.CFrame = camera.CFrame:Lerp(
			CFrame.lookAt(
				(platform.CFrame * CFrame.new((player.Position / 3).X, 0, (player.Position / 1.5).Z)).Position + Vector3.new(
					CAMERA_OFFSET_X,
					CAMERA_OFFSET_Y,
					CAMERA_OFFSET_Z
				),
				(platform.CFrame * CFrame.new((player.Position / 2).X, 0, (player.Position / 2).Z)).Position
			),
			i
		)
	end
end)

if I added delay to the for – loop, it would start to lag.
Any idea how to smoothly lerp the camera?

Help appreciated!

1 Like

What do you want to achieve exactly? I am guessing that you want to interpolate the camera from one CFrame to another.

You can use this spring module ‘NevermoreEngine/Spring.lua at version2 · Quenty/NevermoreEngine · GitHub’ to smoothly interpolate the position and rotation of the camera if that is what you are looking for.

Here is a function that moves the camera to a certain CFrame smoothly over a certain amount of time in seconds.

local moveCamera = function(destination, moveTime)
   local camera = workspace.CurrentCamera
   camera.CameraType = Enum.CameraType.Scriptable
   local destination = {CFrame = moveTime}
   local tweenInfo = TweenInfo.new(moveTime, Enum.EasingStyle.Linear)
   local cameraTween = game:GetService('TweenService'):Create(camera, tweenInfo, destination)
   cameraTween:Play()
end

Note that if you want the camera to move from one place to another, you will need to set the camera CFrame beforehand and then run that function. You can modify the function to what you would like it to be. If you want, you can change the EasingStyle to Enum.EasingStyle.Quad, but I find that Linear works best for the camera.

I hope this helps! :slight_smile:

5 Likes

That’s clever. Well written. I actually didn’t know about the order there so I appreciate it

1 Like

No, I am trying to make a smooth camera that moves behind a part, but when the part stops, it takes time for the camera to slow down.

Oh, so it follows the part but instead of following it perfectly it kinda lags behind smoothly?

Yeah, something like that. Just imagine as if the camera were a helium baloon attached to the part. It would be a bit behind the part and when the part stops it would keep on moving forward for a few seconds.

Just lerp it an add a little delay so it’s smooth using :Lerp() function of a CFrame

:Lerp(GoalCFrame, TimeToLerp)

How would I lerp it seamlessly every frame though?

I really have no idea how to do it.

RS.RenderStepped:Connect(function(deltaTime) -- use deltaTime if you want it to depend on player FPS
    workspace.CurrentCamera.CFrame:Lerp(workspace.CurrentCamera.CFrame, .2)
end)

BE warn tho I am not sure this will work but try!

Okay, I will try it and let you know if it works.

1 Like

Doesn’t work :confused: Would it be okay to use TweenService?

I would go with @Brickman808’s method.

Example:
https://gyazo.com/c264c966ea2f5460e25d347a79971f8f

Take a look at the comments in the code snippet below~

local Spring = require(script.Spring) 
local RunService = game:GetService("RunService")

local part = game.Workspace:WaitForChild("Part")
local camera = game.Workspace.CurrentCamera

--[[
    Initialise the spring and set it's dampening and speed value to some value.
]]--
local spring = Spring.new(Vector3.new())
spring.d = 10 -- Dampening
spring.s = 5 -- Speed

--[[
    Update function sets the spring target to the part's current position, and 
    adds some offset so the camera isn't sitting directly inside of the part.
    
    Sets the camera CFrame to the current position of the spring.
]]--
local function update()
	spring.t = part.Position + Vector3.new(0, 2, 10)
	
	camera.CFrame = CFrame.new(spring.p)
	
	return 
end

--[[
    Connect update function to RenderStepped so that the camera is updated 
    each frame.
]]--
RunService.RenderStepped:Connect(update)

Here is an open-source spring module you can use:
Spring.lua (3.2 KB)

This doesn’t take into account the orientation of a part, only the position, but it’s a jumping off point for you.

10 Likes

Okay, thanks! I will try it later.

How do you code “spring” in FIrst Person Camera Version? I wanted to achieve smooth camera movement.