I can’t make my camera turning like smooth. It rotates quickly and linearly. I watched a few videos but not as much as I tried and it didn’t work. It would be bad if I found it from the toolbox, but it is not already in the tool
Heres the script:
-- Feel free to change the name of the script and also mess around with the settings and stuff like that
-- Hopefully this helps you learn a little more about scripting
-- or atleast helps for the game you're making
-- Enjoy -Brownsage
---- Variables
local Config = { -- Camera settings
sensitivity = 1, -- How fast the camera moves (No real need to change this because Roblox sensitivity effects it as well)
snap = math.rad(85), -- Vertical rotation limit
offset = Vector3.new(2, 2, 10) -- Offset from player
}
local moveX, moveY = 0, 0 -- Variables used for moving the camera in 'mouseMove' function below
---- Objects
local Character = script.Parent
local Root = Character:WaitForChild("HumanoidRootPart")
local Camera = game.Workspace.CurrentCamera
---- Functions
function focus()
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter -- Locks the mouse to the center of the screen
end
function mouseMove(name, state, inputObject)
moveX = moveX + (-inputObject.Delta.x*Config.sensitivity/100) -- Adds mouse movement and applies sensitivity settings
moveY = moveY + (-inputObject.Delta.y*Config.sensitivity/100)
moveY = math.clamp(moveY, -Config.snap, Config.snap) -- If 'moveY' is a higher value than 'Config.snap' we set it to 'Config.snap' and the other way around for negative values
end
---- Connecting functions
game:GetService("UserInputService").WindowFocused:Connect(function() -- Runs function when the window is selected
focus() -- Locks mouse
end)
game:GetService("ContextActionService"):BindActionToInputTypes("MouseMove", mouseMove, false, Enum.UserInputType.MouseMovement) -- Connects 'mouseMove' function to 'MouseMove' event (This event runs any time the mouse moves)
---- Setup
Camera.CameraType = "Scriptable" -- This is done so we can move the Camera without problems (Roblox scripts trying to move it)
focus() -- Needed so we can lock the Camera without needing to reselect the window
---- Run
game:GetService("RunService").RenderStepped:Connect(function() -- Runs connected function every frame
Camera.CFrame = CFrame.new(Root.Position) * CFrame.Angles(0, moveX, 0) * CFrame.Angles(moveY, 0, 0) -- Sets Camera position to the players root part and applys rotation
Camera.CFrame = Camera.CFrame * CFrame.new(Config.offset) -- Offsets the Camera based on Config
local clipCorrection = 0.2 -- The amount to move the Camera away from whatever it hits so you can't move the Camera through walls.. though you can kinda see through walls, but it works well on ground
local rayStart = Root.Position + Vector3.new(0, Config.offset.Y, 0) -- Position for the start of the ray
local rayEnd = Camera.CFrame * CFrame.new(0, 0, clipCorrection).p -- Position for the end of the ray
local ray = Ray.new(rayStart, rayEnd - rayStart) -- Casts a ray from 'rayStart' to 'rayEnd'
local part, position = workspace:FindPartOnRay(ray, Character) -- Finds out if the ray hits anything and returns the part it hit and the position it hit at
if (part) then -- If anything is hit
Camera.CFrame = CFrame.new(position) * (Camera.CFrame - Camera.CFrame.p) * CFrame.new(0, 0, -clipCorrection) -- When a part is hit we set the position of the Camera to where we hit the part, then we add back the rotation the Camera had and apply 'clipCorrection' to it
end
end)