local function u(u)for m=1,#KEY_MAPPINGS[u],1 do if UIS:IsKeyDown(KEY_MAPPINGS[u][m])then return true end end return false end local m do local x=.016666666666667 RS.RenderStepped:Connect(function(u)x=u end)function m()local m=camera.CFrame local d=(u(DIRECTION_RIGHT)and 1 or 0)-(u(DIRECTION_LEFT)and 1 or 0)local c=(u(DIRECTION_UP)and 1 or 0)-(u(DIRECTION_DOWN)and 1 or 0)local I=(u(DIRECTION_BACKWARD)and 1 or 0)-(u(DIRECTION_FORWARD)and 1 or 0)local g=(d*d+c*c)+I*I if g>1e-15 then g=((UIS:IsKeyDown(Enum.KeyCode.LeftShift)or UIS:IsKeyDown(Enum.KeyCode.RightShift))and.25 or 1)/math.sqrt(g)d=d*g c=c*g I=I*g end local A=d+gp_x local h=(c+gp_r1)-gp_l1 local b=I+gp_z velSpring.t=Vector3.new(A,h,b)*SpeedModifier rotSpring.t=panDeltaMouse+panDeltaGamepad fovSpring.t=Clamp(fovSpring.t+(x*rate_fov)*FVEL_GAIN,5,120)local R=fovSpring:Update(x)local n=velSpring:Update(x)*LVEL_GAIN local y=rotSpring:Update(x)*((RVEL_GAIN*math.tan((R*math.pi)/360))*NM_ZOOM)rate_fov=0 panDeltaMouse=Vector2.new()stateRot=stateRot+y stateRot=Vector2.new(Clamp(stateRot.x,-1.5,1.5),stateRot.y)local s=((CFrame.new(m.p)*CFrame.Angles(0,stateRot.y,0))*CFrame.Angles(stateRot.x,0,0))*CFrame.new(n)camera.CFrame=s camera.Focus=s*FOCUS_OFFSET camera.FieldOfView=R end end
------------------------------------------------

local function IsDirectionDown(direction)
	for i = 1, #KEY_MAPPINGS[direction] do
		if UIS:IsKeyDown(KEY_MAPPINGS[direction][i]) then
			return true
		end
	end
	return false
end

local UpdateFreecam
do
	local dt = 1 / 60
	RS.RenderStepped:Connect(function(_dt)
		dt = _dt
	end)

	function UpdateFreecam()
		local camCFrame = camera.CFrame

		local kx = (IsDirectionDown(DIRECTION_RIGHT) and 1 or 0) - (IsDirectionDown(DIRECTION_LEFT) and 1 or 0)
		local ky = (IsDirectionDown(DIRECTION_UP) and 1 or 0) - (IsDirectionDown(DIRECTION_DOWN) and 1 or 0)
		local kz = (IsDirectionDown(DIRECTION_BACKWARD) and 1 or 0) - (IsDirectionDown(DIRECTION_FORWARD) and 1 or 0)
		local km = (kx * kx) + (ky * ky) + (kz * kz)
		if km > 1e-15 then
			km = ((UIS:IsKeyDown(Enum.KeyCode.LeftShift) or UIS:IsKeyDown(Enum.KeyCode.RightShift)) and 1 / 4 or 1)
				/ math.sqrt(km)
			kx = kx * km
			ky = ky * km
			kz = kz * km
		end

		local dx = kx + gp_x
		local dy = ky + gp_r1 - gp_l1
		local dz = kz + gp_z

		velSpring.t = Vector3.new(dx, dy, dz) * SpeedModifier
		rotSpring.t = panDeltaMouse + panDeltaGamepad
		fovSpring.t = Clamp(fovSpring.t + dt * rate_fov * FVEL_GAIN, 5, 120)

		local fov = fovSpring:Update(dt)
		local dPos = velSpring:Update(dt) * LVEL_GAIN
		local dRot = rotSpring:Update(dt) * (RVEL_GAIN * math.tan(fov * math.pi / 360) * NM_ZOOM)

		rate_fov = 0
		panDeltaMouse = Vector2.new()

		stateRot = stateRot + dRot
		stateRot = Vector2.new(Clamp(stateRot.x, -3 / 2, 3 / 2), stateRot.y)

		local c = CFrame.new(camCFrame.p)
			* CFrame.Angles(0, stateRot.y, 0)
			* CFrame.Angles(stateRot.x, 0, 0)
			* CFrame.new(dPos)

		camera.CFrame = c
		camera.Focus = c * FOCUS_OFFSET
		camera.FieldOfView = fov
	end
end
