I’m trying to code a script for my menu GUI that makes it so that your camera follows the mouse…
…and I did it (although it isn’t smooth). But the thing is, how do I make it able to create the effect on other parts’ CFrames? The script I have right now only works for one part, and I have multiple camera parts I’d like to have this effect on.
Problems I have
If I change the CFrame of the camera, it just switches back to the DefaultCFrame.
Making and disconnecting connections just makes it harder.
using the in pairs thing doesn’t work, plus I don’t know how it fully works.
local cameras = workspace.Cameras --folder
local Camera = workspace.CurrentCamera
local DefaultCFrame = cameras.MainCamera.CFrame --camera part inside folder
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
repeat
wait()
Camera.CameraType = Enum.CameraType.Scriptable
until
Camera.CameraType == Enum.CameraType.Scriptable
local maxTilt = 12
local runserv = game:GetService("RunService")
local connection = runserv.RenderStepped:Connect(function() --main effect
Camera.CFrame = DefaultCFrame * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),0)
end)
How do I make it so that I can have this effect, even if I switch/tween to another CFrame?
Unfortunately, this is not possible with run service since it runs so quickly that the script is unable to alter the changes made for the Cframe while maintaining the mouse effect.
so i used while loop instead and Debounce and this actually worked fine for me:
local cameras = workspace.Cameras --folder
local Camera = workspace.CurrentCamera
local DefaultCFrame = cameras.MainCamera.CFrame --camera part inside folder
local CamTwo = cameras["CameraPart2"].CFrame
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
dothis = true
repeat
wait()
Camera.CameraType = Enum.CameraType.Scriptable
until
Camera.CameraType == Enum.CameraType.Scriptable
local maxTilt = 12
local runserv = game:GetService("RunService")
function ChangeCam(Cam)
while true do
task.wait()
if not dothis then
Camera.CFrame = Cam
Camera.CFrame = Cam * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),0)
else
Camera.CFrame = Cam * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),0)
end
end
end
spawn(function()
task.wait()
ChangeCam(DefaultCFrame)
end)
script.Parent:WaitForChild("menu").Change.MouseButton1Click:Connect(function() -- Changeing Cam when clicking a button (just for testing)
dothis = false
spawn(function()
task.wait()
ChangeCam(CamTwo)
end)
end)
All you have to do to Change the Cam’s Cframe is to set dothis to false then after that call the ChangeCam function.
First of all, you should save a mouse “delta” in some variable so it’s easier to read. After that you make an max offset to the left and right, top and bottom (in angles). After that transform the percentage of the mouse offset into the angles.
How do you do that?
Welp, here we go.
local maxR,maxL,maxT,maxB = 20,20,20,20
--don't forget to inverse the cframe so it's translated into normal angles
rs.renderStepped:Connect(function()
local ScreenSize:vector2 = GetScreenSize()
local ScreenCenter:vector2 = GetScreenCenter()
local mousePosition:vector2 = GetMousePosition()
local percentageY = ((ScreenCenter.X-mousePosition.X)/ScreenSize.X)
local percentageX = ((ScreenCenter.Y-mousePosition.Y)/ScreenSize.Y)
local Y = math.rad(if precentageY > 0 then percentageY * maxR else percentageY * maxL end)
local X = math.rad(if precentageX > 0 then percentageX * maxB else percentageX * maxT end) --it's quite different as the top of the screen is set to be a start, that's why I replaced it
cam.CFrame = (CFrame.new(pos) * CFrame.Angles(X,Y,0) * cframeAnglesOffset):Inverse()
end)