✓ Accepted answer
Try a cosine function?
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer.PlayerGui
local Screen = PlayerGui:FindFirstChildOfClass("ScreenGui") or Instance.new("ScreenGui", PlayerGui)
local Camera = workspace.CurrentCamera
local Mouse = LocalPlayer:GetMouse()
local pi = math.pi
local function CharacterAdd(Character)
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
OpenConnection = RunService.RenderStepped:Connect(function()
Camera.CFrame = Character.Head.CFrame
HumanoidRootPart.CFrame = HumanoidRootPart.CFrame * CFrame.Angles(0, math.rad(math.cos(pi * Mouse.X / Screen.AbsoluteSize.X)), 0)
end)
end
local Character = LocalPlayer.Character
LocalPlayer.CharacterAdded:Connect(CharacterAdd)
if Character then
CharacterAdd(Character)
end
EDIT: Upon request, I will explain why it works as best as I can. A cosine function is a wave function that oscillates between -1 and 1.
Here are some (x, y) important values for the cosine function:
(0 , 1)
(0.5*pi, 0)
(pi , -1)
Now if you look at what you want to achieve in terms of X position of mouse and the Rotate value in your first script
(0 , 1)
(0.5 * Screen.AbsoluteSize.X, 0)
(Screen.AbsoluteSize.X , -1)
If you multiply the second set of ordered pairs by pi and divide by Screen.AbsoluteSize.X, you would get the first set, and the inverse of those operations can be applied to the first set to get the second.
I guess that isn't the best way to explain it, but basically, the cosine wave from 0 to pi is stretched to the length of the entire screen. You could have just used a linear function as well, but the cosine curve is rounded before its max and minimum, so it looks smoother because it stops speeding up so much and gets close to maximum speed the closer you move the mouse to the edge of the screen.
1 comment
Actually, to be a little more precise, the camera needs to come to a complete stop by the time the mouse is within the "DeadZone" which is the center of the screeen with a margin of 50.