Roblox 接受 Xbox 和 PlayStation 控制器等 USB 遊戲手柄的輸入。遊戲控制器有不同的種類,因此您需要遵循額外的設定來確認玩家的遊戲控制器輸入在體驗中是否可用。
若要設定遊戲控制器輸入,您可以使用 或 來 偵測連接到玩家裝置的遊戲控制器輸入 ,確認與 Roblox 相容的支持輸入 ,接收輸入 等等。
當綁定遊戲控制器輸入時,請參閱普通控制模式來為玩家創建一致的遊戲控制器體驗。輸入設定後,您可以通過包含觸覺反饋在支持的控制器上來增強玩家的體驗。
偵測遊戲板
您可以使用 UserInputService.GamepadEnabled 屬性檢測玩家的裝置目前是否有遊戲控制器啟用。
偵測遊戲控制器
local UserInputService = game:GetService("UserInputService")if UserInputService.GamepadEnabled thenprint("Player has gamepad enabled...")end
您可以通過 UserInputService.GamepadConnected 和 UserInputService.GamepadDisconnected 事件檢查連接的遊戲控制器。當裝置連接或分離時,這些事件會發生,且兩個事件都會將 Enum.UserInputType 傳給連接的函數,表示哪個遊戲手柄造成了事件。在大多數情況下,連接的遊戲手柄是 Gamepad1 。
檢查連線和斷線
local UserInputService = game:GetService("UserInputService")
UserInputService.GamepadConnected:Connect(function(gamepad)
print("User has connected controller: " .. tostring(gamepad))
end)
UserInputService.GamepadDisconnected:Connect(function(gamepad)
print("User has disconnected controller: " .. tostring(gamepad))
end)
您也可以使用 UserInputService:GetGamepadConnected() 方法查詢特定控制器是否連接。這需要一個 Enum.UserInputType 作為參數,只接受 Gamepad1 的值通過 Gamepad8 。
查詢特定遊戲控制器連線
local UserInputService = game:GetService("UserInputService")if UserInputService:GetGamepadConnected(Enum.UserInputType.Gamepad1) thenprint("Gamepad1 is connected")elseif UserInputService:GetGamepadConnected(Enum.UserInputType.Gamepad2) thenprint("Gamepad2 is connected")end
驗證支持的輸入
遊戲控制板可以有不同的輸入集,因此您應該檢查哪些輸入支持 UserInputService:GetSupportedGamepadKeyCodes() 。此方法接受 Enum.UserInputType 作為參數,並返回包含指定控制器所有可用輸入列表的表。
驗證支持的輸入
local UserInputService = game:GetService("UserInputService")local availableInputs = UserInputService:GetSupportedGamepadKeyCodes(Enum.UserInputType.Gamepad2)print("This controller supports the following controls:")for _, control in availableInputs doprint(control)end
接收輸入
對於綁定控件到遊戲手柄和其他輸入來源,例如 滑鼠和鍵盤輸入或 手機觸摸屏按鈕,或對多個功能綁定到單個按鈕輸入在任何裝置上,都有用。例如,下面的代碼示例將OpenSpellBook綁定到遊戲控制器的ButtonR2和鍵盤的B。
上下文行動服務綁定行動
local ContextActionService = game:GetService("ContextActionService")
local function openSpellBook(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
-- 開啟法術書
end
end
ContextActionService:BindAction("OpenSpellBook", openSpellBook, false, Enum.KeyCode.ButtonR2, Enum.KeyCode.B)
或者,您可以使用 UserInputService 從游戏手柄直接綁定控件。當通過此服務偵測遊戲控制器事件時,使用 InputBegan 事件偵測按鈕最初按下時間,以及 InputEnded 偵測按鈕釋放時間。在處理功能中, InputObject.UserInputType 屬性指示哪個遊戲發射了事件, InputObject.KeyCode 指示發射它的特定按鈕或棒。
使用者輸入服務按鈕壓測偵測
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.ButtonA then
print("Button A pressed on Gamepad1")
end
end
end)
遊戲控制器狀態
您可以使用 UserInputService:GetGamepadState() 方法檢測遊戲控制器上所有按鈕和棒子的當前狀態。如果您需要在體驗中檢查當前游戏手柄輸入時,例如檢查角色是否按下特定按鈕時觸碰物物件,這很有用。
檢查遊戲控制器輸入狀態
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local leftFoot = character:WaitForChild("LeftFoot")
-- 當左腳接觸到某物時,檢查遊戲控制器輸入狀態
leftFoot.Touched:Connect(function(hit)
local state = UserInputService:GetGamepadState(Enum.UserInputType.Gamepad1)
for _, input in state do
-- 如果按鈕 R2 目前被保持,則列印一個訊息
if input.KeyCode == Enum.KeyCode.ButtonR2 and input.UserInputState == Enum.UserInputState.Begin then
print("Character's left foot touched something while holding right trigger")
end
end
end)
壓力觸發
您可以通過檢查輸入扳機的 Position.Z 來偵測游戏手柄觸發器所受到的壓力。
測試扳機壓力
local UserInputService = game:GetService("UserInputService")
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.ButtonL2 then
print("Pressure on left trigger has changed:", input.Position.Z)
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
print("Pressure on right trigger has changed:", input.Position.Z)
end
end
end)
常見控制模式
遊戲控制板有各種形狀和尺寸。與玩家輸入的任何方法一樣,最好在不同遊戲和體驗中創建一些一致性。

以下是常見的輸入綁定,可讓玩家立即感覺熟悉和舒適地使用遊戲控制器:
輸入 | 常見使用案例 |
---|---|
ButtonA | 接受玩家提示或GUI選擇。也可用於主要動作,例如跳躍。 |
ButtonB | 取消玩家提示或GUI選擇。也可用於次要動作,例如躲避、滾動或衝刺。 |
Thumbstick1 | 一般與角色移動相關。 |
Thumbstick2 | 一般與相機移動相關。 |
ButtonL2 , ButtonR2 | 一般用於主要行動,例如射擊。 |
ButtonL1 , ButtonR1 , ButtonX , ButtonY | 重新載入、瞄準或存取庫存或小地圖等次動作。 |
觸感反回饋
許多遊戲控制器內建有馬達,用於提供觸覺反回饋。添加隆隆聲和振動可以大幅提升玩家的體驗,並提供超出視覺或聲音訊的微妙反饋。您可以使用 HapticService 來 驗證振動支持 之前 啟動發動機。
振動支協助
不是所有控制器都有馬達,因此在嘗試使用觸覺馬達之前,必須檢查是否有支持。要查詢指定控制器是否具有振動協助,請呼叫 HapticService:IsVibrationSupported() 。
檢查震動支持
local HapticService = game:GetService("HapticService")local gamepad = Enum.UserInputType.Gamepad1local isVibrationSupported = HapticService:IsVibrationSupported(gamepad)
有些控制器擁有多個馬達,可用於各種振動強度。一旦您檢查了遊戲手柄是否支持振動,您還應該檢查它是否支持您通過 HapticService:IsMotorSupported() 使用的馬達。
檢查支持的馬達
local HapticService = game:GetService("HapticService")local gamepad = Enum.UserInputType.Gamepad1local isVibrationSupported = HapticService:IsVibrationSupported(gamepad)if isVibrationSupported thenlocal largeMotor = HapticService:IsMotorSupported(gamepad, Enum.VibrationMotor.Large)local smallMotor = HapticService:IsMotorSupported(gamepad, Enum.VibrationMotor.Small)local leftTriggerMotor = HapticService:IsMotorSupported(gamepad, Enum.VibrationMotor.LeftTrigger)local rightTriggerMotor = HapticService:IsMotorSupported(gamepad, Enum.VibrationMotor.RightTrigger)local leftHandMotor = HapticService:IsMotorSupported(gamepad, Enum.VibrationMotor.LeftHand)local rightHandMotor = HapticService:IsMotorSupported(gamepad, Enum.VibrationMotor.RightHand)end
大小或位置 | 說明 |
---|---|
Large | 較大的馬達,對於一般沖擊有用。 |
Small | 較小的馬達,對於像輪胎滑行、電擊等更微妙的聲音有用。 |
LeftTrigger | 在左觸發器下方。 |
RightTrigger | 在右觸發器下。 |
LeftHand | 在控制器的左側。 |
RightHand | 在控制器的右側。 |
啟用馬達
一旦您確認玩家的遊戲手柄 支持振動,您可以使用 HapticService:SetMotor() 啟動特定馬達。此方法接受遊戲手柄和振動幅度作為參數。幅度可為 0 與 1 之間的任何值。
啟動馬達
local HapticService = game:GetService("HapticService")local gamepad = Enum.UserInputType.Gamepad1local isVibrationSupported = HapticService:IsVibrationSupported(gamepad)if isVibrationSupported thenlocal largeMotor = HapticService:IsMotorSupported(gamepad, Enum.VibrationMotor.Large)if largeMotor thenHapticService:SetMotor(gamepad, Enum.VibrationMotor.Large, 0.5)endend
控制器模擬
控制器模擬器 讓你在工作室準確模擬遊戲手柄輸入。預設控制器是一個通用遊戲手柄,但您可以從左上角的選擇器菜單中選擇PlayStation、Xbox和任務裝置的替代方案。


在遊戲測試時,您可以使用滑鼠控制體驗,使用虛擬控制器。
您也可以點擊 編輯映射 在右上角查看並編輯虛擬控制器的關鍵映射,例如E到ButtonL2或9到ButtonA。這些映射像其他 Studio 設定一樣保存(每個控制器、每個使用者、每台電腦),並且在模擬器窗口和 3D 視角中翻譯為遊戲手柄事件。