function CameraModule.new(c,r,p,n)local x={}x.Position=c x.Rotation=r x.CFrame=CFrame.new(c)*CFrame.Angles(math.rad(r.X),math.rad(r.Y),math.rad(r.Z))x.Target=Vector3.new(1,0,0)x.PressedKey=nil x.fpsController={direction=0;pitch=0}x.Sensitivity=p Mouse.KeyDown:Connect(function(c)x.PressedKey=c end)Mouse.KeyUp:Connect(function()x.PressedKey=nil end)function x.Movement(c)local r=x.PressedKey if r=="\101"then x.CFrame+=Vector3.new(0,.4,0)elseif r=="\113"then x.CFrame-=Vector3.new(0,.4,0)elseif r=="\119"then x.CFrame+=Vector3.new(.4*math.cos(-x.Rotation.Y),0,.4*math.sin(-x.Rotation.Y))elseif r=="\115"then x.CFrame-=Vector3.new(.4*math.cos(-x.Rotation.Y),0,.4*math.sin(-x.Rotation.Y))elseif r=="\097"then x.CFrame+=Vector3.new(.4*math.sin(-x.Rotation.Y),0,-0.4*math.cos(-x.Rotation.Y))elseif r=="\100"then x.CFrame+=Vector3.new(-0.4*math.sin(-x.Rotation.Y),0,.4*math.cos(-x.Rotation.Y))end task.synchronize()x:Pan((UserInput:GetMouseDelta()).X,(UserInput:GetMouseDelta()).Y)task.desynchronize()end function x.Pan(p,c,r)x.fpsController.direction=math.max(math.min(x.fpsController.direction-r*x.Sensitivity,math.pi*.5),math.pi*-0.5)x.fpsController.pitch=x.fpsController.pitch-c*x.Sensitivity local n=CFrame.Angles(0,x.fpsController.pitch,0)*CFrame.Angles(x.fpsController.direction,0,0)local A=CFrame.new(x.CFrame.Position)x.CFrame=A*n end function x.Update(c)x.Position=x.CFrame.Position local r,p,A=x.CFrame:ToOrientation()x.Rotation=Vector3.new(r,p+math.rad(90),0)if n==true then x:Movement()end end return x end return CameraModule


function CameraModule.new(Position, Rotation, Sensitivity, move)
	local Camera = {}
	
	Camera.Position = Position
	Camera.Rotation = Rotation
	Camera.CFrame = CFrame.new(Position) * CFrame.Angles(math.rad(Rotation.X), math.rad(Rotation.Y), math.rad(Rotation.Z))
	Camera.Target = Vector3.new(1,0,0)
	Camera.PressedKey = nil

	Camera.fpsController = {
		direction = 0,
		pitch = 0,
	}

	Camera.Sensitivity = Sensitivity
	
	Mouse.KeyDown:Connect(function(key)
		Camera.PressedKey = key
	end)

	Mouse.KeyUp:Connect(function()
		Camera.PressedKey = nil
	end)

	function Camera:Movement()
		local key = Camera.PressedKey

		if key == "e" then
			Camera.CFrame += Vector3.new(0,0.4,0)
		elseif key == "q" then
			Camera.CFrame -= Vector3.new(0,0.4,0)
		elseif key == "w" then
			Camera.CFrame += Vector3.new(0.4*math.cos(-Camera.Rotation.Y), 0, 0.4*math.sin(-Camera.Rotation.Y))
		elseif key == "s" then
			Camera.CFrame -= Vector3.new(0.4*math.cos(-Camera.Rotation.Y), 0, 0.4*math.sin(-Camera.Rotation.Y))
		elseif key == "a" then
			Camera.CFrame += Vector3.new(0.4*math.sin(-Camera.Rotation.Y), 0, -0.4*math.cos(-Camera.Rotation.Y))
		elseif key == "d" then
			Camera.CFrame += Vector3.new(-0.4*math.sin(-Camera.Rotation.Y), 0, 0.4*math.cos(-Camera.Rotation.Y))
		end
		
		task.synchronize()
		Camera:Pan(UserInput:GetMouseDelta().X, UserInput:GetMouseDelta().Y)
		task.desynchronize()
	end

	function Camera:Pan(dx, dy)
		Camera.fpsController.direction = math.max(math.min(Camera.fpsController.direction - dy * Camera.Sensitivity, math.pi * 0.5), math.pi * -0.5)
		Camera.fpsController.pitch = Camera.fpsController.pitch - dx * Camera.Sensitivity
		
		local rotationCFrame = CFrame.Angles(0, Camera.fpsController.pitch, 0) * CFrame.Angles(Camera.fpsController.direction, 0, 0)
		local positionCFrame = CFrame.new(Camera.CFrame.Position)

		Camera.CFrame = positionCFrame * rotationCFrame
	end
	
	function Camera:Update()
		Camera.Position = Camera.CFrame.Position
		local xRotation, yRotation, _ = Camera.CFrame:ToOrientation()
		Camera.Rotation = Vector3.new(xRotation, yRotation + math.rad(90), 0)
		
		if move == true then
			Camera:Movement()
		end
	end

	return Camera
end

return CameraModule
