How to rotate the camera using :Lerp() or TweenService without the camera getting locked?

Currently trying to use :Lerp() or TweenService to rotate the camera without it “Locking” meaning that the camera still follows the player while also lerping the rotation of it.

1 Like

Could you give a better explanation with thhe code?

Do you want to recreate recoil?

What are you trying to achieve? :coefficients:

Set the CameraType to “Scriptable”.

This completely goes against what the OP wants.
What our OP wants is to be able to tween or lerp the camera retaining player control.
Tween is not possible to my knowledge cause you HAVE to use scriptable.
Lerp may be possible you’d have to experiment…
You may want to look around on the devforum for topics like ‘Rotating Camera Lerp’ or Camera Tween.

1 Like

Yes, what I mean is that I want to lerp/tween the cameras rotation without it styaing in place while lerping (Setting the CameraType to Scriptable locks it).

Example Code:

local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera

local function LerpCameraRotation(targetCFrame, duration)
	local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, true)
	local goal = {CFrame = targetCFrame}

	local tween = TweenService:Create(camera, tweenInfo, goal)
	tween:Play()
end

UserInputService.InputBegan:Connect(function(input, gp)
	if gp then return end 

	if input.KeyCode == Enum.KeyCode.E then
		local currentCFrame = camera.CFrame
		local targetCFrame = currentCFrame * CFrame.Angles(0, 0, math.rad(-15))
		LerpCameraRotation(targetCFrame, 0.4)
	end
end)

Video of the code:

What is happening is you are Tweening the camera’s CFrame in the frame that the player presses E. What you would need to do is find a way to rotate the camera in conjuction with the player’s current camera CFrame.

1 Like

Someone helped me in a server with the implementation and used to spring module to simplify it. Thanks!

Code (Put the Spring Module in the LocalScript in StarterPlayerScripts):

--!strict

local FREQUENCY = 20 --"stiffness"
local DAMPING = 1 --damping of 1 means no bounce (less damping = more bounce)

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

--Just a spring module i'm using for simplification here
local Spring = require(script.Spring)

local camera = workspace.CurrentCamera

--You can create more springs if you need movement on more axes
local spring_zaxis = Spring.create(FREQUENCY, DAMPING)

--Update every frame before render
RunService.PreRender:Connect(function(delta_time)
	local zaxis_position = Spring.step(spring_zaxis, delta_time)
	--zaxis_position is already in radians, so we dont need math.rad here
	
	--Add onto the current camera cframe using cframe multiplication
	--Because every frame the roblox camera scripts set the cframe, we just add onto it each frame before rendering
	camera.CFrame *= CFrame.fromEulerAnglesXYZ(0, 0, zaxis_position)
end)

--Key down
UserInputService.InputBegan:Connect(function(input, sunk)
	if sunk then return end

	if input.KeyCode == Enum.KeyCode.E then
		--Set the spring target position to our desired value
		spring_zaxis.target = math.rad(-15)
	end
end)
--Key up
UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		--Reset spring target position
		spring_zaxis.target = 0
	end
end)
2 Likes