My custom camera goes crazy when going backwards

  1. What do you want to achieve?
    Basically I’m making a custom camera for my game, to follow the player’s movement and orientation

  2. What is the issue?
    When the player goes backwards, camera goes crazy due to a loop happening where the camera moves along and the player goes backwards respecting the camera orientation.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried a few solutions but none worked.

This is the piece of code that modifies the camera


_G.Zoom = 20 --just for example

local cameraOffset = Vector3.new(0, _G.Zoom-6, _G.Zoom/2)
local characterPosition = Character.HumanoidRootPart.Position
local characterLookVector = Character.HumanoidRootPart.CFrame.LookVector
local lookAtPoint = characterPosition + Vector3.new(0, 8, 0)

local desiredCameraPosition = characterPosition - (characterLookVector * cameraOffset.Z) + Vector3.new(0, cameraOffset.Y, 0)

if not lastCameraPosition then
	lastCameraPosition = desiredCameraPosition
else			
	if (desiredCameraPosition - lastCameraPosition).Magnitude > 0.3 then
		lastCameraPosition = lastCameraPosition:Lerp(desiredCameraPosition, 0.008)
	end
end

			
Camera.CFrame = CFrame.new(lastCameraPosition, lookAtPoint)

It’s because of the Roblox Movement System,

The MoveDirection is also Relative to the Camera Rotation, and due to how your camera system works, when the Character is Below the Camera, that’s where things get tricky… when your character walks towards the Camera, and it reaches Exactly Below The Camera, your Camera’s Y Axis or Yaw will snap around 180 Degrees to the Front Direction of your Character, and effectively inverting your MoveDirection, so you’ll move the opposite Direction, and get below the Camera Again, which causes this to go on a loop! Your Character then walks Back and Forth, along with your Camera “Snapping”

Easiest way to fix this that i have in mind is to just, only change where the Camera is Pointing to When The Characters walks Forward to the Camera Direction