Thumbstick Control with the Camera (OTS Camera)

Hello,
I’m trying to make an over-the shoulder camera with support for multiple platforms. Here’s what I have so far:

-------------
--- INDEX ---
-------------

--Services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

--Player & Character
local localPlayer = game:GetService("Players").LocalPlayer
local Mouse = localPlayer:GetMouse()

--Other
local Camera = game:GetService("Workspace").CurrentCamera
local ZoomValuePointer = game.ReplicatedFirst.CameraSettings.Zoom

--------------
--- VALUES ---
--------------

local xAngle = 0
local yAngle = 0
local CamOffset = 3.5
--local cameraPos = Vector3.new(3.5,0,10)
--===============ABOVE LINE MOVED INTO THE RENDERSTEPPED FUNCTION.====================--
----------------------
--- INITIALIZATION ---
----------------------

wait(1)

script.Parent.Equipped:connect(function()
	Equ = true
	Camera.CameraType = Enum.CameraType.Scriptable
	userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
end)
	--[[game.Players.LocalPlayer.DevEnableMouseLock = Enum.ControlMode.MouseLockSwitch
	UserSettings().GameSettings.ControlMode = Enum.ControlMode.MouseLockSwitch]]

-----------------
--- FUNCTIONS ---
-----------------

userInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
		xAngle = xAngle-input.Delta.x*0.4
		--Clamp the vertical axis so it doesn't go upside down or glitch.
		yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80)
	elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
		--no idea at all
	end
end)

Mouse.WheelForward:Connect(function()
	ZoomValuePointer.Value = ZoomValuePointer.Value - 1
end)
Mouse.WheelBackward:Connect(function()
	ZoomValuePointer.Value = ZoomValuePointer.Value + 1
end)



script.Parent.Unequipped:connect(function()
	Equ = false
	game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
	--[[game.Players.LocalPlayer.DevEnableMouseLock = Enum.ControlMode.Classic
	UserSettings().GameSettings.ControlMode = Enum.ControlMode.Classic]]
	Camera.CameraType = Enum.CameraType.Custom
end)


runService.RenderStepped:Connect(function()
	if Equ then
		--userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		local cameraPos = Vector3.new(game.ReplicatedFirst.CameraSettings.Offset.Value,0,ZoomValuePointer.Value)
		local Character = localPlayer.Character
		local rootPart = Character:FindFirstChild("HumanoidRootPart")
		if Character and rootPart then
			--Set rotated start cframe inside head
			local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)
			
			--Set camera focus and cframe
			local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(cameraPos)
			local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
			
			--Character:FindFirstChild("Humanoid").CameraOffset = 
			Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
		end
	end
end)

Sorry I didn’t clean it up at all, it’s still a project :slight_smile:
Anyways, the problem I’ve got right now is that I have no idea how to implement controller support. The maximum a delta can be on a controller is 1.0, and to be honest I have no idea how to make the camera continue rotating when the player holds the joystick down. Anyone got any ideas? I’m sure it’s been done before.

Someone else actually made almost all of that code you see here, as I’m not experienced with camera scripting enough to successfully code it myself.

5 Likes