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.
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: