Camera jumps back to original position after moving it manually

I have this system in my game where the camera follows the mouse wherever it goes.

However, when I move the camera manually (such as holding right-click and looking around) the camera quickly moves back to where it was beforehand once I let go.

ScreenLauncherWindow (gyazo.com)

Here’s the script that makes this work:

local cam = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()

plr.CharacterAdded:Wait()

local MB2 = false

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		MB2 = true
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		MB2 = false
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()

	local hrp = plr.Character:FindFirstChild("HumanoidRootPart")

	if not hrp then
		return
	end

	local mousepos = Mouse.Hit.Position
	local CharacterPosition = hrp.Position

	local MaxLength = 2

	local ChartoMouse = (mousepos - CharacterPosition).unit * math.clamp( (mousepos - CharacterPosition).Magnitude , 0, MaxLength)

	local Lookat = CharacterPosition:Lerp(CharacterPosition+ChartoMouse,0.75)
	
	if UserInputService.MouseBehavior ~= Enum.MouseBehavior.LockCenter and MB2 == false then
		cam.CFrame = cam.CFrame:Lerp(CFrame.new(hrp.Position + Vector3.new(0, 5, 15),Lookat),0.1)
	end
end)
2 Likes

If I’m not mistaken, you can simply remove the two InputBegan functions and the MB2 variable. This is because when you press right click (MouseButton2), it sets MB2 to true. If you look at the bottom of the script, it only moves the camera if MB2 == false. So while holding right click (meaning MB2 is true), the camera won’t move.

local cam = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()

plr.CharacterAdded:Wait()

game:GetService("RunService").RenderStepped:Connect(function()

	local hrp = plr.Character:FindFirstChild("HumanoidRootPart")

	if not hrp then
		return
	end

	local mousepos = Mouse.Hit.Position
	local CharacterPosition = hrp.Position

	local MaxLength = 2

	local ChartoMouse = (mousepos - CharacterPosition).unit * math.clamp( (mousepos - CharacterPosition).Magnitude , 0, MaxLength)

	local Lookat = CharacterPosition:Lerp(CharacterPosition+ChartoMouse,0.75)
	
	if UserInputService.MouseBehavior ~= Enum.MouseBehavior.LockCenter then
		cam.CFrame = cam.CFrame:Lerp(CFrame.new(hrp.Position + Vector3.new(0, 5, 15),Lookat),0.1)
	end
end)
1 Like

The functions and variable are there so it doesn’t lerp when I move the camera manually, as shown.
:pie: Pie Hunt! - Roblox Studio (gyazo.com)
It doesn’t fix the problem either.

1 Like