Guidance with camera pan/rotation with mouse

Hello! I have been looking for a solution for quite a while for this specific issue. Have you ever been in a game with a loading/main menu screen with a camera that moves depending on where your mouse is on the screen?

I basically want to recreate this, however I only need help with working out how to pull that off (not any UI stuff). I already have the camera and everything set up but just want to know how I would do it.

After consulting ChatGPT, this forum and several other sources the only solutions I can find are ones that have an external camera, rotating a DIFFERENT part with the mouse. Instead, I want to rotate the camera part (on 2 axis: x,y).

Any questions and help would be appreciated!

1 Like

are you looking for something like this?

1 Like

I actually tried that exact video, and after recreating it, it moves the camera instead of rotating it! I don’t know how to adapt it either because I’m pretty new when it comes to vector3, lookat, angles and stuff like that.

1 Like

your response was a bit vague, but this should rotate the camera instead of moving it.

local cam = workspace.CurrentCamera
local cam_part = workspace["CameraPart"]
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

repeat
    task.wait()
    cam.CameraType = Enum.CameraType.Scriptable
until cam.CameraType == Enum.CameraType.Scriptable

local smoothness = 0.1

local max_tilt = 10
local last_cf = cam.CFrame
game:GetService("RunService").RenderStepped:Connect(function()
    local delta_x = (mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX
    local delta_y = (mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY

    local rotation_x = CFrame.Angles(0, math.rad(delta_x * max_tilt), 0)
    local rotation_y = CFrame.Angles(math.rad(-delta_y * max_tilt), 0, 0)

    local target_cf = cam_part.CFrame * rotation_x * rotation_y
    cam.CFrame = last_cf:lerp(target_cf, smoothness)
    last_cf = cam.CFrame
end)

1 Like

Thanks for the help, but the x-axis is inverted! How do I fix this?

1 Like

try inverting the “rotation_x” variable manually by placing a minus sign in front of “math.rad(delta_x * max_tilt)”, like so:

local rotation_x = CFrame.Angles(0, -math.rad(delta_x * max_tilt), 0)
1 Like

Hey, I’m sorry for the late reply and the lack of clarity in my responses, I have another issue if you’d be so kind to help with:

It works ALMOST as intended, except the camera stops after reaching a certain point on the screen. I was wanting something that continuously moved in the direction of the mouse until the mouse had returned to the position of the screen.

Thanks!

This is still a bit vague, but I will try my best to understand.

Are you trying to keep the camera always rotating in the direction of the mouse?

Absolutely yes! Thanks for helping <3 - I’ll try and improve my responses.

Okay. Here is the issue with doing it that way. If the mouse is hovered off-center for too long the camera will just continue to slowly pan, resulting in the camera being able to rotate 360 degrees. Is this what you want? Or do you want the camera to stop after a certain point?

I want the camera to do the full 360, yeah. Thanks for your understanding.

Well, I am not sure. I have tried a lot of things and have had no good results. Sorry!