Extended Camera Range with mouse

Hello, I am trying to make a script that allows the camera to move depending on the mouse position. I would like this to have a max range so you don’t move the whole camera across but just to pan it across 2 axes which makes 2 positions so its 2D movement and not a 3D camera movement. This would lean depending on where the mouse is as in the pictures below: :arrow_down:


So, The mouse is the camera and also the centre of the screen would be always shown even if the mouse is moving to the edges of the screen. I think that all the information.

If you can help me that would be great. :innocent:

  • Thanks for reading this and trying to help me! :smile:

Hi. This is just a brief overview of something you could do.

First, you woud need a Vector2 for the center of the screen.

local displayCenter = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2)

Inside of a function, I’m going to get the position of the mouse x and y coordinate on the screen. I’m dividing by some factor, which will control how much the camera should move with the mouse. The higher the value, the less the camera will move. 500 is a good value, but you can play around with it yourself.

local function CameraMovement()
    local x = -(mouse.X - displayCenter.X) / [SOME FACTOR]
    local y  = -(mouse.Y - displayCenter.Y) / [SOME FACTOR]
end

Constructing a Vector3 to apply to the camera.

local function CameraMovement()
    local x = -(mouse.X - displayCenter.X) / [SOME FACTOR]
    local y  = -(mouse.Y - displayCenter.Y) / [SOME FACTOR]

    local moveVector = Vector3.new(x, y, 0)
end

Constructing a CFrame angle.

local function CameraMovement()
    local x = -(mouse.X - displayCenter.X) / [SOME FACTOR]
    local y  = -(mouse.Y - displayCenter.Y) / [SOME FACTOR]

    local moveVector = Vector3.new(x, y, 0)

    local angle = CFrame.fromEulerAnglesXYZ(math.rad(moveVector.Y), math.rad(moveVector.X), math.rad(moveVector.Z))
end

Lastly, multiplying the calculated CFrame with your default camera CFrame

local function CameraMovement()
    local x = -(mouse.X - displayCenter.X) / [SOME FACTOR]
    local y  = -(mouse.Y - displayCenter.Y) / [SOME FACTOR]

    local moveVector = Vector3.new(x, y, 0)

    local angle = CFrame.fromEulerAnglesXYZ(math.rad(moveVector.Y), math.rad(moveVector.X), math.rad(moveVector.Z))

    camera.CFrame = defaultPosition * angle
end

Connect this to RenderStepped.

local cameraMovementConnection = RunService.RenderStepped:Connect(CameraMovement)

Hope this helps.

1 Like

Hello, Thank you for your time to help me out.
I am guessing I have to put this all in one script. I am new to Lua so I don’t know how to script so I don’t really understand. But Thank you so much for this. :smile:

Hello again, I am not sure if I am doing something wrong or I if there is an error but it seems to have errors in the code (orange lines under some variables)


It’s most likely me which is making a mistake as I am new to coding in Lua.

  • Thanks