Hello, I’ve been making a restaurant game, and have been wondering how to make camera follow a mouse CFrame. Hopefully someone can help me because I’ve had no luck. With the restrictions, I’m wondering how to make it stop at a certain angle.
Reference: https://gyazo.com/64ea43e66e236887fe600d8e1ac32d2b
Thank you for any help.
1 Like
You would want to get the position of the mouse on the screen in relation to the center of the screen and based on how big the entire screen is adjust the CFrame of the camera accordingly.
1 Like
Just like what @RoloTheDevBunny said, get the mouse position relative to the center of the screen. Then rotate the camera in a pivot.
I made this code, but it is untested.
local run = game:GetService'RunService'
local tween = game:GetService'TweenService'
local uis = game:GetService'UserInputService'
local cam = workspace.Camera
local plr = game.Players.LocalPlayer
local screenX = cam.ViewportSize.X
local screenY = cam.ViewportSize.Y
local MOVE_SPEED = 4
local MAX_ANGLE_X = math.rad(45)
local MAX_ANGLE_Y = math.rad(20)
local followConnect:RBXScriptConnection
local function followMouse(Pivot: CFrame)
local t = tween:Create(cam,TweenInfo.new((cam.CFrame.p-Pivot.Position).Magnitude/10,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut))
t:Play()
t.Completed:Wait()
task.wait(1)
local pos = uis:GetMouseLocation()
followConnect = run.RenderStepped:Connect(function(dt)
pos = pos:Lerp(uis:GetMouseLocation(),dt*MOVE_SPEED)
local x = pos.X/screenX*2-1
local y = pos.Y/screenX*2-1
cam.CFrame = Pivot*CFrame.Angles(0,x*MAX_ANGLE_X,0)*CFrame.Angles(y*MAX_ANGLE_Y,0,0)
end)
end
followMouse( CFrame.new() ) -- Pass a CFrame where the camera is locked
followConnect:Disconnect() -- Stop mouse follow
There are three variables you can edit.
Use the last line to stop the mouse follow.
2 Likes
I’m aware this was a while ago, but it’s now saying it doesn’t work.
Error MSG: Argument 3 missing or nil - Client - LocalScript:17
It did work at the time but now it doesn’t. I’ve lost all knowledge of scripting now and have no idea what went wrong lol.
Ty for the help.