Making camera move slightly with your mouse?

At the moment, I am attempting to make a main menu. I feel though that if I just have a camera staring at a vignette with nothing else, it may feel too bland - which is why I want to throw in some unnecessary effects, like the camera moving around slightly when the mouse is moved. Any ideas as to how I could get started on creating such a system?

9 Likes

A main menu shouldn’t be bland. If it’s bland, you’re probably doing something wrong.
Backgrounds on their own are almost always bland – which is main menus never have backgrounds on their own.

The main menu should capture the player’s attention with things like logos, buttons, etc.


Anyway, if you want to look into making camera effects, you’d want to:

  • Detect mouse movement with UserInputService (or ContextActionService)
  • Use the mouse object to update the camera every frame
  • Move camera from the default position based on how far the mouse moves from the center of the screen

Here’s an example of the latter logic:

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

local DefaultCFrame = Camera.CFrame
local Scale = 200

function Update()
    -- get the center of the screen
    local Center = Vector2.new(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)
    -- get the movement (it's in studs, and the mouse properties are in pixels, so you want to divide it by your scale to not move the camera really really far)
    local MoveVector = Vector3.new((Mouse.X-Center.X)/Scale, (Mouse.Y-Center.Y)/Scale, 0)
    -- CFrame * CFrame makes it work the same regardless of rotation, where addition just makes it work for one direction
    Camera.CFrame = DefaultCFrame * CFrame.new(DefaultCFrame.p + MoveVector)
end

game:GetService("RunService").RenderStepped:Connect(Update) -- update every frame
69 Likes

I’ll see what I can do. This is more or less what I had in mind in terms of how to code it.

4 Likes

How can i do this but with a gui object ( Make it slightly follow the mouse)

1 Like

I’m not a mod but you might want to consider starting a new thread with more information.

That being said, it would work in the same sort of way. You basically have the anchor position for the GUI (it’s actual/starting location) then move the element towards the mouse but scaled down.

5 Likes