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)