Making a fixed camera with a bit of movement

Hello.

I am creating a phone dial system for my horror game and for that, I want to have the camera tween to the fixed camera position when a part is touched.

I already know how to tween and how to connect touch to function.

However, what I want to do next is the hard part.

Basically, I want to make a fixed camera, where the mouse is free and moving the mouse moves the camera, not fully though. I only want the camera to rotate as far as the mouse goes.

I imagine this would be done with connecting a tween with mouse movement, but i’m not quite sure how this would be done.

1 Like

Do you mind referencing any games that include this mechanic, or a video might be helpful.

1 Like

Yeah, let me try and dig one up.

I know i’ve seen it in a game but I don’t necessarily remember which game it was.

1 Like

I remembered that the game in this video had a similar thing to what i’m trying to create.

You can see that he zooms out and eventually the momentum stops.

I want to create this, but on a smaller scale.

1 Like

Here is some starter code:

local centerCFrame -- CFrame looking at the phone

-- Degrees the mouse movement can move the camera
-- Might want to define per-axis so X and Y have different sensativities
local maxRotateAngle = 40 
-- How fast the camera moves to the target, between [0, 1]
local toTargetSpeed = 0.1

local function getTarget()
    local mouseXFromCenter -- TODO: subtract the center of the screen X from the mouse X
    local screenWidth = camera.ViewportSize.X
    local YRotation = mouseXFromCenter / screenWidth * maxRotateAngle

    local mouseYFromCenter -- TODO: subtract the center of the screen Y from the mouse Y
    local screenHeight = camera.ViewportSize.Y
    local XRotation = mouseYFromCenter / screenHeight * maxRotateAngle

    local target = centerCFrame * CFrame.angles(0, math.rad(YRotation), 0) * CFrame.angles(math.rad(XRotation), 0, 0)
end

-- Make sure to `startCamera` *after* setting the camera to scriptable
local cameraConnection
local function startCamera()

    camera.CFrame = getTarget()
    cameraConnection = RunService.RenderStepped:Connect(function()
        camera.CFrame = camera.CFrame:Lerp(getTarget(), toTargetSpeed)
    end
end

local function endCamera()
    if cameraConnection then
        cameraConnection:Disconnect()
    end
end

You might also want to look into springs for the smooth movement. Using lerp isn’t frame independent, so it goes faster or slower based on the frame rate. Springs are also more natural feeling. Here is a spring module I use by Quenty: