Cant Lerp camera

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I just want to lerp the camera right to left or whatever position

  2. What is the issue? İt doesnt work as it should it stays at the 2. values number(idk how to explain :Lerp(x, 0.5) it stays at the 0.5’s position

  3. What solutions have you tried so far? I checked google and chatgpt but non of them worked

I dont see any reason for it to not work

local player = game.Players.LocalPlayer

local CharacterHandler = require(script.CharacterHandler)
local ThirdPersonCamera = require(script.ThirdPersonCamera)

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

local cameraOffset = CFrame.new(1.5, 0.5, 0)

local characterHandlerInstance 

local function set_camera(char)
	local Camera = ThirdPersonCamera.new(char)

	char:WaitForChild("Humanoid").Died:Once(function()
		Camera:Destroy() 
	end)

	local currentCamera = game.Workspace.CurrentCamera
	RunService.Stepped:Connect(function()
		currentCamera.CFrame = currentCamera.CFrame:Lerp(currentCamera.CFrame * cameraOffset, 0.1)
	end)
end

local function char_added(char)
	set_camera(player.Character)

	characterHandlerInstance = CharacterHandler.new() 

	local inventory = {
		Primary = "",
		Secondary = "Glock",
		Melee = ""
	}
	characterHandlerInstance:loadInventory(inventory)
end

player.CharacterAdded:Connect(char_added)

if player.Character then
	char_added(player.Character)
end

UIS.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if gameProcessedEvent then return end

	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.One then
			if characterHandlerInstance then
				characterHandlerInstance:equipGun("Primary")  
			end
		elseif input.KeyCode == Enum.KeyCode.Two then
			if characterHandlerInstance then
				characterHandlerInstance:equipGun("Secondary")  
			end
		elseif input.KeyCode == Enum.KeyCode.Three then
			if characterHandlerInstance then
				characterHandlerInstance:equipGun("Melee")  
			end
		end
		
		if input.KeyCode == Enum.KeyCode.E then
			cameraOffset = CFrame.new(1.5, 0.5, 0)
		elseif input.KeyCode == Enum.KeyCode.Q then
			cameraOffset = CFrame.new(-1.5, 0.5, 0)
		end
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

try using TweenService instead of Lerp, it will be more guaranteed to work, that and you need the currentCamera’s mode to be correct so it’ll move, e.g set the camera’s CameraType to Enum.CameraType.Scriptable

Thank you for the reply! Im not using tweenService because I will add many positions and I dont want to set them hand by hand. Also I changed the camera type and it seems to do something! It does go to a position but not to the expected position I want

it’s because :Lerp() just gets a point between this position and another position by a fraction, say you have two CFrames, one is at 0, 0, 0 and the other one at 0, 0, 1, doing :Lerp(CFrame2, 0.5) on the first CFrame will most likely get you something like 0, 0, 0.5, now say CFrame2 was at 0, 0, 2, :Lerp() will get you something like 0, 0, 1, and having the second argument over 1 will break alot of stuff i think

But I dont understand should Lerping get something from 0 to 1 (in a loop: 0.1, 0.2, 0.3… 0.9, 1 I just put random numbers for the example) or not

well it just gets an intermediate between two CFrames, still i advise you use tweening

P.S i gotta go sleepers RAHHHHHHHHHHHHHHHHHHHHHHHHHHH :cry:

1 Like

Ok but still it should go to the last position of it because its inside a loop. Also tweening wont work because I cant move the camera with that

well you actually can

local cameraTweenInfo = TweenInfo.new(5, Enum.EasingStyle.Linear) -- linear movement
local targetCameraCFrame = CFrame.new(0, 5, 0) -- this will move it 5 studs upward
local cameraTween = tweenService:Create(camera, cameraTweenInfo, {
     CFrame = targetCameraCFrame
})

camera.CameraType = Enum.CameraType.Scriptable
cameraTween:Play()

Yes as I said Im not able to move the camera and its not usefull because it wont be able to change the position any time I want in the middle of the tween. Thats why Im trying to use lerping but it doesnt do anything. I dont want to use tween service

use RunService.RenderStepped or RunService:BindToRenderStep() instead of RunService.Stepped
your formula is also gonna be framerate-dependant so you might want to incorporate the deltaTime parameter of RunService.RenderStepped somewhere there

additionally, not sure whats going on in ThirdPersonCamera but that’s most likely conflicting with your code

(also, none of the RunService events will be trash collected so you have to manage the event connections by :Disconnect()ing them, likely whenever you create a new RunService connection. memory leaks are a pain :c)

1 Like

Hmm ok I kind of see the point but why it doesnt work? It works on parts but not on a players camera ?

Incorporate this accordingly:

local RunService = game:GetService("RunService")

local camera = workspace.CurrentCamera

RunService:BindToRenderStep("NAME", Enum.RenderPriority.Camera.Value + 1, function(dt)
    camera.CFrame = camera.CFrame:Lerp(targetCFrame, 1 - 0.005 ^ dt)
end)

I did something else thats easier instead of that

local function set_camera(char)
	local Camera = ThirdPersonCamera.new(char)

	char:WaitForChild("Humanoid").Died:Once(function()
		Camera:Destroy()
	end)

	local currentCamera = game.Workspace.CurrentCamera

	local targetCFrame = currentCamera.CFrame:ToWorldSpace(cameraOffset)
	
	RunService.RenderStepped:Connect(function(deltaTime)

		currentCamera.CFrame = PositionLerp.lerpPosition(currentCamera.CFrame, targetCFrame, 0.1)
		targetCFrame = currentCamera.CFrame:ToWorldSpace(cameraOffset)
	end)
end

PositionLerp module I used ( I only wanted the position because camera angle and old angle glithces)

local PositionLerp = {}

function PositionLerp.lerpPosition(currentCFrame, targetCFrame, alpha)

	local currentPosition = currentCFrame.Position
	local targetPosition = targetCFrame.Position

	local lerpedPosition = currentPosition:Lerp(targetPosition, alpha)

	return CFrame.new(lerpedPosition) * currentCFrame.Rotation
end

return PositionLerp

but this time the lerping still doesnt work as it should how normally lerping works

what it should look like :

what it looks like:

lerp is acting like its not in a loop but it goes on the position for 1 time

It’s not easier if the code still doesn’t work… Also like @PuffoThePufferfish said, you should probably be cleaning up the connection if you’re not using BindToRenderStep.

Also your ThirdPersonCamera class may also be causing issues. It’s hard to help whenever there’s a ton of side effects.

Ok but does it really effect the gameplay if I dont clear the connection on the loop? It wont change anything. Also I tested the ThirdPersonClass doesnt change anything with the camera

Yes. Your FPS will lower every time you call the function due to the connections never being cleaned up. You never want to have a memory leak especially if it’s bound to the FPS