[Not Fixed] (HOW) Fixed Lerp for Camera's Movement to CameraPart

Continuing the discussion from Smooth Vehicle Camera:

I discovered the problems of why my vehicle’s camera was having a “glitch” effect, I need help on now create a type of Fixed Lerp to just stay in the same position but always go into another position even if it depends on the Distance, basically, when I am moving my vehicle from one point to another (moving constantly), the lerp that the camera did already finished from the point A, but now it tries to finish to the point B ( where is the next step of the vehicle ) and makes that “glitch” effect, because the camera is always trying to get fixed on the CameraPart’s position but cant do it because of the car that keeps moving.

This is the line of the code:

local CameraPart = Body.CameraPart

		local lastPhysicsDeltaTime = 0
		local lastFrameDeltaTime = 0

		local function getAccurateAlpha(scale)
			return 1 - ((scale and scale or 1) / 1e+12) ^ math.clamp((lastPhysicsDeltaTime - lastFrameDeltaTime), 0.005, 0.05)
		end
		
		RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value - 1, function(deltaTime)
			if DynamicCamera then
				if Camera.CameraType ~= Enum.CameraType.Scriptable then
					Camera.CameraType = Enum.CameraType.Scriptable
				end
				
				local distance = (Camera.CFrame.Position - CameraPart.Position).Magnitude
				
				Camera.CFrame = Camera.CFrame:Lerp(CameraPart.CFrame, getAccurateAlpha())
				Camera.Focus = CameraPart.CFrame
				
				lastFrameDeltaTime = deltaTime / (distance / 1.7)
				
			else
				if Camera.CameraType ~= Enum.CameraType.Custom then
					Camera.CameraType = Enum.CameraType.Custom
				end
			end
		end)

		coroutine.wrap(function()
			while true do
				if ChassisHandler and ChassisHandler:FindFirstChild("IsOn") and ChassisHandler.IsOn.Value then
					local discard
					discard, lastPhysicsDeltaTime = RunService.Stepped:Wait()
				else
					task.wait()
					
					break
				end
			end
		end)()
1 Like

Hey, did you ever find a solution? I’m working on a similar problem but for a very different game, lerp is effectively speeding up the camera and slowing it down as it gets closer to the part making the jittery effect. I think lerping by a fixed amount may fix the issue but I haven’t found much help on the forum.

what about you? did you find a solution? because i am facing the same issue

You can create a fixed Lerp by using a constant alpha value instead of calculating it based on the distance. Here’s how you can modify your script to achieve this:

local CameraPart = Body.CameraPart
local LERP_CONSTANT = 0.1  -- Change this value to adjust the speed of the camera movement

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value - 1, function(deltaTime)
	if DynamicCamera then
		if Camera.CameraType ~= Enum.CameraType.Scriptable then
			Camera.CameraType = Enum.CameraType.Scriptable
		end
		
		Camera.CFrame = Camera.CFrame:Lerp(CameraPart.CFrame, LERP_CONSTANT)
		Camera.Focus = CameraPart.CFrame
	else
		if Camera.CameraType ~= Enum.CameraType.Custom then
			Camera.CameraType = Enum.CameraType.Custom
		end
	end
end)

In this script, the LERP_CONSTANT is the alpha value for the Lerp function. By setting it to a constant value, the camera will always move at the same speed, regardless of the distance. Adjust this value to change the speed of the camera movement.

This does give bad results, never works. Even if i’m not moving, does happen a glitchy effect.

I might be wrong but i think you should use Heartbeat instead.

I’ve tried everything, but gives the same result, tried BindToRenderStep, Heartbeat, Stepped, RenderStepped, while do loops.

A fixed alpha wouldn’t give you a fixed amount of distance. I believe the lerp is the fraction from one cframe to another, so if cframes are farther apart, the same alpha value produces a farther cframe than the last time it was lerped.

I think for others looking for fixes, I would consider just having the camera locked on the object, and manually tween to to an offset back or left/right based on the user input as a potential hacky solution. I’m not doing a racing game, so I ended up accepting the slight amount of jitter because of how much simpler and smoother the camera movement is for my use case. With the fixed camera + offset solution, abrupt changes in speed will still move the camera abruptly and it didn’t look right for my game. This post may also help and may have solved the problem for those who want to further pursue this issue. How can I make the camera follow the player smoothly? [Check Video Example To Understand]

The problem shouldn’t be heartbeat. I believe the problem is essentially caused by the distance between cframes and therefore the magnitude the camera is moving by changing. The only way to get it to move at a sort of constant magnitude would be to have a variable alpha based on the magnitude of the difference between the camera and the position it needs to move to, but with the complexity of this solution I believe it’s probably better to explore other options than lerping. It can also be attempted to only take the object being lerped to’s position on a fixed interval (ex. 0.2 seconds) but still update the camera position every renderstep, although when trying this, it’s slight lack of responsiveness didn’t fit the use case I was trying to achieve.

1 Like