Custom camera doesn't work with gamepad

Hello, I was trying to make this camera script work with a controller, but this one works differently than a mouse and so far, I have only managed to detect when the right thumbstick is moving.

1.What do you want to achieve?

My goal is to be able to get a first-person camera that works with a mouse and a controller.

2.What is the issue?

The camera works very badly with the remote and can only move around a certain area, to move the camera more you have to repeatedly hit the thumbstick in one direction.

3.What solutions have you tried so far?

I tried to detect when the player releases the thumbstick so that the camera does not return to the original position, without success. I have also tried to add the delta value of the thumbstick with the previous delta value so that the camera can move in all directions, without success.

This is the script:

repeat wait() until game:GetService("Players").LocalPlayer.Character ~= nil
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

Sentivity = 0.1
Smoothness = 0.025
FieldOfView = 70

local camera = game.Workspace.Camera
local player = Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Icon = "http://www.roblox.com/asset/?id=569021388"
local character = player.Character or player.Character:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")

local head = character:WaitForChild("Head")
local CamPos, TargetCamPos = camera.CoordinateFrame.p,camera.CoordinateFrame.p
local AngleX,TargetAngleX = 0,0
local AngleY,TargetAngleY = 0,0

function updateChar()
	for _,v in pairs(character:GetChildren()) do
		if v.Name == "Head" then
			v.LocalTransparencyModifier = 1
			v.CanCollide = false
		end
		if v:IsA("Accessory") then
			v:FindFirstChild("Handle").LocalTransparencyModifier = 1
			v:FindFirstChild("Handle").CanCollide = false
		end
		if v:IsA("Hat") then
			v:FindFirstChild("Handle").LocalTransparencyModifier = 1
			v:FindFirstChild("Handle").CanCollide = false
		end	
	end

	for _,v in pairs(character:GetDescendants()) do
		if v:IsA("ParticleEmitter") then
			v.Enabled = false
		end
	end
end

local moving = false

local function Move(name,state,input)
	
	if input.UserInputType ~= Enum.UserInputType.Gamepad1 then
		
		local delta = Vector2.new(input.Delta.x/Sentivity,input.Delta.y/Sentivity) * Smoothness
		
		local X = TargetAngleX - delta.y
		TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
		TargetAngleY = (TargetAngleY - delta.x) %360
	else
		if input.KeyCode == Enum.KeyCode.Thumbstick2 and input.Position.magnitude > 0.15 and not moving then
			task.spawn(function()
				moving = true
				--Moving
				repeat
					local delta = Vector2.new((input.Delta.x*100)/Sentivity,-(input.Delta.y*100)/Sentivity) * Smoothness
					local X = TargetAngleX - delta.y
					TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
					TargetAngleY = (TargetAngleY - delta.x) %360
					wait()
				until input.Position.magnitude <= 0.15 or input.UserInputType ~= Enum.UserInputType.Gamepad1
				--Ended
				moving = false
			end)
		end
	end
	
end

ContextActionService:BindAction("MoveCamera",Move,false,Enum.UserInputType.MouseMovement,Enum.UserInputType.Touch,Enum.UserInputType.Gamepad1)

RunService.RenderStepped:Connect(function()

	local CT = tick()

	CamPos = CamPos + (TargetCamPos - CamPos) *0.28
	AngleX = AngleX + (TargetAngleX - AngleX) *0.35
	local distance = TargetAngleY - AngleY
	distance = math.abs(distance) > 180 and distance - (distance / math.abs(distance)) * 360 or distance
	AngleY = (AngleY + distance *0.35)%360
	camera.CameraType = Enum.CameraType.Scriptable

	camera.CoordinateFrame = CFrame.new(head.Position)
		* CFrame.Angles(0,math.rad(AngleY),0)
		* CFrame.Angles(math.rad(AngleX),0,0)
		* CFrame.new(0,0.75,0)

	if humanoid.MoveDirection.Magnitude > 0 then
		local BobbleX = math.cos(CT*5)*0.25
		local BobbleY = math.abs(math.sin(CT*5))*0.25
		local Bobble = Vector3.new(BobbleX,BobbleY,0)
		camera.CoordinateFrame = camera.CoordinateFrame:lerp(camera.CoordinateFrame * CFrame.new(Bobble),0.25)
	end

	humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position)*CFrame.Angles(0,math.rad(AngleY),0)

	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

end)

updateChar()

camera.FieldOfView = FieldOfView

did you find a solution to this?

1 Like

What I did is disable the custom camera when connecting a controller and use the default Roblox camera. The problem is that the camera looks weird if you play with a controller so I’m still looking for a better solution.

1 Like

okay so instead of using (delta/amount changed) I found that there is a position property for the thumbstick.

		if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.Focus or (input.UserInputType == Enum.UserInputType.Gamepad1 and input.KeyCode == Enum.KeyCode.Thumbstick2) then
			if input.UserInputType == Enum.UserInputType.Gamepad1 and input.KeyCode == Enum.KeyCode.Thumbstick2 then
				GamePadControlEnabled = true	
			 ThumbsticX = input.Position.X
			 ThumbsticY = input.Position.Y

This fixed my problem. btw if you need any more assistance on this let me know

2 Likes

Thank you, I have fixed the problem. I’ll put that as a solution. This is the code I have changed in case anyone needs it:

local function Move(name,state,input)
	
	if input.UserInputType ~= Enum.UserInputType.Gamepad1 then
		
		local delta = Vector2.new(input.Delta.x/Sensitivity,input.Delta.y/Sensitivity) * Smoothness

		local X = TargetAngleX - delta.y
		TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
		TargetAngleY = (TargetAngleY - delta.x) %360
		
	elseif input.UserInputType == Enum.UserInputType.Gamepad1 and input.KeyCode == Enum.KeyCode.Thumbstick2 and input.Position.magnitude > 0.15 and not ThumbstickMoving then
		task.spawn(function()
			ThumbstickMoving = true
			--Moving
			repeat
				local delta = Vector2.new((input.Position.x*ThumbstickSpeed)/Sensitivity,-(input.Position.y*ThumbstickSpeed)/Sensitivity) * Smoothness --ThumbstickSpeed = 25
				local X = TargetAngleX - delta.y
				TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
				TargetAngleY = (TargetAngleY - delta.x) %360
				wait()
			until input.Position.magnitude <= 0.15 or input.UserInputType ~= Enum.UserInputType.Gamepad1
			--Ended
			ThumbstickMoving = false
		end)
	end
end
1 Like

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