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?

11 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
81 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.

5 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

I’ve found that making loading screens and menus with similar systems to this are best done with surface guis and some particle emitters around with a blank background or curved photo acting as a skybox.

local Camera = workspace.Camera
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Scale = 200 -- you might want to change this
function Update()
	local Center = Vector2.new(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)
	--local z = math.max(math.abs(Center.X-Mouse.X)/Scale, math.abs(Center.Y-Mouse.Y)/Scale)
	local z = (Center.X-Mouse.X)/Scale/4
	local MoveVector = Vector3.new((Center.X-Mouse.X)/Scale*2, (Center.Y-Mouse.Y)/Scale*2, z)
	--Camera.CFrame = DefaultCFrame * CFrame.new(DefaultCFrame.p + MoveVector)
	Camera.CFrame = DefaultCFrame*CFrame.Angles(math.rad(MoveVector.Y), math.rad(MoveVector.X), math.rad(MoveVector.Z))
end

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

If anyone ever wants it I made a script to make the camera rotate. I originally made it move back on the Z with the absolute value of x but decided to change it to rotation. I am using this for a camera system I am making and it adds a nice touch. Thanks to @EmeraldSlash for the base I am using here!

1 Like