How can I make the camera move with the mouse

So, I’ve been tasked with creating a main menu and I think this effect where the camera follows the mouse looks kinda cool.
Problem is, I don’t know where to start. Any idea?
(Reference GIF)

3 Likes

I have a script, I hope it helps!

local Mouse = game.Players.LocalPlayer:GetMouse()
local Camera = game.Workspace.CurrentCamera

local DefaultCFrame = workspace.CurrentCamera.CFrame
local Scale = 1000

function Update()
	local Center = Vector2.new(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)
	local MoveVector = Vector3.new((Mouse.X-Center.X)/Scale, (Mouse.Y-Center.Y)/Scale, 0)
	Camera.CFrame = DefaultCFrame + CFrame.new(DefaultCFrame.p + MoveVector)
end

game:GetService("RunService").RenderStepped:Connect(Update)
1 Like

Thank you but it doesn’t work, it just spams this error

Players.QuaternionIdentity.PlayerScripts.LocalScript:10: invalid argument #2 (Vector3 expected, got CFrame)

It should be inserted in a ScreenGui, not in StarterPlayerScripts.

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local camPart = nil -- Your cam part

local rotateAmount = 5

local function enable()    
    camera.CameraType = Enum.CameraType.Scriptable
    camera.CFrame = camPart.CFrame
    game:GetService("RunService"):BindToRenderStep("CameraLookAtMouse", Enum.RenderPriority.Camera.Value + 1, function()
        camera.CFrame = camPart.CFrame * CFrame.Angles(
            math.rad((mouse.Y - mouse.ViewSizeY) / mouse.ViewSizeY * -rotateAmount),
            math.rad((mouse.X - mouse.ViewSizeX) / mouse.ViewSizeX * -rotateAmount),
            math.rad(0)
        )
    end)
end

local function disable()
    game:GetService("RunService"):UnbindFromRenderStep("CameraLookAtMouse")
    camera.CameraType = Enum.CameraType.Custom
end

It’s not possible to add a CFrame onto another CFrame.

You can only multiply.

Camera.CFrame = DefaultCFrame + MoveVector

This will work but it still isn’t the way I would achieve it. I would make a Rotation CFrame(You can use normal angles or Quaternions but you have to remember that CFrames ARE NOT COMMUTATIVE) and multiply it with a Original CFrame for the effect instead.

1 Like

Sorry for the huge reviving but I have decided to use this script.
However, I’d do some changes to it.

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local camPart = nil -- Your cam part

local rotateAmount = 5

local function enable()    
    camera.CameraType = Enum.CameraType.Scriptable
    camera.CFrame = camPart.CFrame
    game:GetService("RunService"):BindToRenderStep("CameraLookAtMouse", Enum.RenderPriority.Camera.Value + 1, function()
        camera.CFrame = camPart.CFrame * CFrame.Angles(
            -- Divide ViewSize values by 2 so the screen is centered when the mouse is at the middle, not at the top left corner
            math.rad((mouse.Y - mouse.ViewSizeY/2) / (mouse.ViewSizeY/2) * -rotateAmount),
            math.rad((mouse.X - mouse.ViewSizeX/2) / (mouse.ViewSizeX/2) * -rotateAmount),
            math.rad(0)
        )
    end)
end

local function disable()
    game:GetService("RunService"):UnbindFromRenderStep("CameraLookAtMouse")
    camera.CameraType = Enum.CameraType.Custom
end

This should make the mouse move at the center of the screen, and not at the top left corner.
Thanks a lot!