ゲームパッド入力

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

Roblox は、Xbox や PlayStation のような USB ゲームパッドからの入力を受け入れます。ゲームパッドはさまざまな種類があるため、プレイヤーのゲームパッド入力がエクスペリエンスで使用できるかどうかを確認するために追加の設定を行う必要があります。

ゲームパッドの入力を設定するには、 または を使用して、 接続されたゲームパッドを検出する 、Roblox に互換可能なサポートされる入力を検証する 、入力を受け取る などがあります。

ゲームパッドの入力をバインドするときは、一般的な制御スキーム を参照して、プレイヤーに一貫したゲームパッドエクスペリエンスを作成します。入力が設設定するされた後、対応するコントローラーに ハプティックフィードバック を含めることで、プレイヤーの体験を向上させることができます。

ゲームパッドを検出

UserInputService.GamepadEnabled プロパティを使用して、プレイヤーのデバイスが現在ゲームパッドを有効に使用しているかどうかを検出できます。

ゲームパッドの検出

local UserInputService = game:GetService("UserInputService")
if UserInputService.GamepadEnabled then
print("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() メソッドを使用して接続されているかどうかを調べることもできます。これは引数として を取り、 を通じてのみ値を受け入れます。

特定のゲームパッド接続をクエリする

local UserInputService = game:GetService("UserInputService")
if UserInputService:GetGamepadConnected(Enum.UserInputType.Gamepad1) then
print("Gamepad1 is connected")
elseif UserInputService:GetGamepadConnected(Enum.UserInputType.Gamepad2) then
print("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 do
print(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リロード、ターゲティング、またはインベントリやミニマップへのアクセスなどのセカンダリアクション。

ハプティックフィードバック

多くのゲームパッドコントローラーには、ハプティックフィードバックを提供するモーターが内蔵されています。ランブルと振動を追加すると、プレイヤーのエクスペリエンスが大幅に向上し、ビジュアルまたはオーディオを超えた微妙なフィードバックを提供できます。You can use the HapticService to 振動サポートを確認する 前に モーターをオンにする

振動サポート

すべてのコントローラーにモーターがあるわけではないので、ハプティックモーターを使用しようとする前にサポートを確認することが重要です。指定のコントローラーが振動サポートを持っているかどうかをクエリーするには、HapticService:IsVibrationSupported() を呼び出します。

振動サポートをチェック

local HapticService = game:GetService("HapticService")
local gamepad = Enum.UserInputType.Gamepad1
local isVibrationSupported = HapticService:IsVibrationSupported(gamepad)

いくつかのコントローラーには、さまざまな振動レベルのための複数のモーターがあります。ゲームパッドが振動をサポートしているかどうかを確認したら、HapticService:IsMotorSupported() を通じて使用したいモーターをサポートしているかどうかも確認する必要があります。

モーターのサポートをチェック

local HapticService = game:GetService("HapticService")
local gamepad = Enum.UserInputType.Gamepad1
local isVibrationSupported = HapticService:IsVibrationSupported(gamepad)
if isVibrationSupported then
local 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.Gamepad1
local isVibrationSupported = HapticService:IsVibrationSupported(gamepad)
if isVibrationSupported then
local largeMotor = HapticService:IsMotorSupported(gamepad, Enum.VibrationMotor.Large)
if largeMotor then
HapticService:SetMotor(gamepad, Enum.VibrationMotor.Large, 0.5)
end
end

コントローラーエミュレーション

コントローラーエミュレーター では、スタジオでゲームパッドの入力を正確に直接エミュレートできます。デフォルトのコントローラーは一般的なゲームパッドですが、上左のピッカーメニューからプレイステーション、Xbox、クエストデバイスの代替を選択できます。

Emulate Device Controllers button indicated in Test tab View of the generic controller in the Controller Emulator.

プレイテスト中、マウスを使用してバーチャルコントローラーでエクスペリエンスを制御できます。

また、右上隅の マッピングを編集 をクリックして、バーチャルコントローラーのキーマッピングを表示し、編集できます。例えば、EButtonL2 または 9ButtonA にします。これらのマッピングは、他の Studio 設定と同様に保存され(コントローラーごと、ユーザーごと、コンピューターごと)、エミュレータウィンドウと 3D ビューポートの両方でゲームパッドイベントに翻訳されます。