Camera moves too fast when mouse nears an edge

  • Apologies in advance if this is messy, this is my first post.

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like the camera to angle itself relative to where the mouse is on screen. (e.g. the mouse moving leftward will angle the camera leftward, moving it up will angle it upward, etc…)

  2. What is the issue? Include screenshots / videos if possible!

See description below for explanation.

  • My camera system uses the position of the mouse and divides it by the screen size to get a “rough location” on where to tilt.
Details
  • The problem is the fact that it is dividing from the screen size. If you are unaware, the right of the screen starts from 0, going all the way up to the max amount of pixels leftward. Due to this, the calculation on the right is much smaller than the left, making the camera tilt in small increments on the right side and exponentially increasing as the mouse moves towards the left, making it move much MUCH faster.
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

*I have tried dividing and clamping. I am not too well versed in math and such.

Details
  • Dividing provides the same problem, just decreasing the amount of movement overall; the problem still persists.

  • Clamping works to an extent, but it really only cuts the issue in half, not fixing it. The camera will suddenly stop as it reaches the max or min limit of the clamp, and the problem with the exponential increase in speed going leftward is still present; the problem still persists

The following code is rather rudimentary - I am aware of that and will fix it later. I am only asking for a solution in regards to the speed increase.

  • One last thing - I am trying to learn and improve my scripting ability and understanding of math. Though it is not required, I ask that you please explain what you’re doing and why - what does (x) mean, why should we be using (x)?

If you are not willing to explain, I ask that you please link a tutorial that you think is good to understand the material - Thank you in advance.

local ps = game:GetService("Players")
local uis = game:GetService("UserInputService")

local plr = ps.LocalPlayer
local mouse = plr:GetMouse()

local pi = math.pi

local cam = workspace.CurrentCamera
repeat wait() cam.CameraType = Enum.CameraType.Scriptable until cam.CameraType == Enum.CameraType.Scriptable
cam.CFrame = workspace:WaitForChild("CameraPosition").CFrame
local ogpos = cam.CFrame

local screen = Vector2.new(mouse.ViewSizeX,mouse.ViewSizeY)

local pos = 0
while true do
	task.wait()
	pos+=1/100
	cam.CFrame = ogpos*CFrame.new(math.sin(pos)/10,math.cos(pos)/10,0)
	local lockmousey,lockmousex = math.clamp(mouse.Y,30, (screen.Y/2)*1.5 ),math.clamp(mouse.X,100, (screen.X/2)*1.5 )
	
	local tpos = cam.CFrame; local x,y = (screen.X/lockmousex)/(pi*2),(screen.Y/lockmousey)/(pi*2)
	cam.CFrame = tpos*CFrame.Angles(y,x,0)
end