Why in this code my camera inverted

local run = game:GetService'RunService'
local uis = game:GetService'UserInputService'

local cam = workspace.Camera
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local mult = 180/math.pi
local clamp = math.clamp

local current = Vector2.zero
local targetX, targetY = 0, 0

local speed = 2 -- Set the speed of the animation
local sensitivity = 1 -- Set the sensitivity of the rotation
local releaseDecel = .5 -- Set how much it slows down when you release

uis.MouseDeltaSensitivity = 0.01
cam.CameraType = Enum.CameraType.Custom

run:BindToRenderStep("SmoothCam", Enum.RenderPriority.Camera.Value-1, function(dt)
	local delta = uis:GetMouseDelta()*sensitivity*100
	targetX += delta.X
	targetY = clamp(targetY+delta.Y,-90,90)
	current = current:Lerp(Vector2.new(targetX,targetY), dt*speed)
	cam.CFrame = CFrame.fromOrientation(current.Y/mult,current.X/mult,0)
end)
mouse.Button2Up:Connect(function()
	local new = Vector2.new(targetX,targetY):Lerp(current, releaseDecel)
	targetX = new.X
	targetY = new.Y
end)

You’re supposed to be subtracting the delta from the target instead of adding it. Also, clamping at -90,90 seems to cause some camera glitching, so I recommend clamping at -80,80 (roblox uses -80,80). Fixed code:

local run = game:GetService'RunService'
local uis = game:GetService'UserInputService'

local cam = workspace.Camera
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local mult = 180/math.pi
local clamp = math.clamp

local current = Vector2.zero
local targetX, targetY = 0, 0

local speed = 2 -- Set the speed of the animation
local sensitivity = 1 -- Set the sensitivity of the rotation
local releaseDecel = .5 -- Set how much it slows down when you release

uis.MouseDeltaSensitivity = 0.01
cam.CameraType = Enum.CameraType.Custom

run:BindToRenderStep("SmoothCam", Enum.RenderPriority.Camera.Value-1, function(dt)
	local delta = uis:GetMouseDelta()*sensitivity*100
	targetX -= delta.X
	targetY = clamp(targetY-delta.Y,-80,80)
	current = current:Lerp(Vector2.new(targetX,targetY), dt*speed)
	cam.CFrame = CFrame.fromOrientation(current.Y/mult,current.X/mult,0)
end)
mouse.Button2Up:Connect(function()
	local new = Vector2.new(targetX,targetY):Lerp(current, releaseDecel)
	targetX = new.X
	targetY = new.Y
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.