現代控制器和設備有內置馬達提供觸摸式回饋。添加漂亮和振動可以提供難以通過視覺或音訊頻傳輸的觀點提出的微妙反饋。
Roblox 支援以下設備的震動:
- 支援震動的 Android 和 iOS 手機,包括大多數 iPhone、Pixel 和 Samsung Galaxy 裝置
- PlayStation 遊戲控制器
- Xbox 遊戲控制器
- 任務控制器
概要
方法
將目前的振動值設為指定的 UserInputType 和 Enum.VibrationMotor 。如果 SetMotor 尚未呼叫,將不會返回任何內容。
如果指定的引擎可用,返回 true 。
如果指定的 Enum.UserInputType 支援觸摸回饋,則返回 true。
- SetMotor(inputType : Enum.UserInputType,vibrationMotor : Enum.VibrationMotor,vibrationValues : Tuple):void
設定指定 UserInputType 和 Enum.VibrationMotor 的震動強度。
屬性
方法
GetMotor
將目前的振動值設為指定的 UserInputType 和 Enum.VibrationMotor 。如果 SetMotor 尚未呼叫,將不會返回任何內容。
參數
指定的 Enum.UserInputType。
指定的 Enum.VibrationMotor 。
返回
目前的振動值設為指定的 Enum.UserInputType 和 Enum.VibrationMotor 或 null 如果 SetMotor 未被呼叫。
IsMotorSupported
如果指定的引擎可用,返回 true 。
參數
正在檢查 Enum.UserInputType 為 Enum.VibrationMotor 協助。
指定的 Enum.VibrationMotor 檢查是否支援指定的 Enum.UserInputType。
返回
對於指定的 Enum.UserInputType 可用,且指定的引擎可用,否則為 false。
IsVibrationSupported
如果指定的 Enum.UserInputType 支援觸摸回饋,則返回 true。
參數
指定的 Enum.UserInputType 檢查是否支援觸摸式反回饋。
返回
對於具有指定 Enum.UserInputType 的輸入類型,是否支援觸摸式反回饋。
SetMotor
設定指定 UserInputType 和 Enum.VibrationMotor 的震動強度。注意,幾乎所有的使用案例都指定 Gamepad1 作為 1> Class.InputObject.UserInputType|UserInputType1> 。
參數
返回
範例程式碼
This example makes the small motor vibrate depending on how much pressure is applied to the left trigger, and the large motor vibrate depending on how much pressure is applied to the right trigger.
local UserInputService = game:GetService("UserInputService")
local HapticService = game:GetService("HapticService")
local cachedInputs = {}
local keyToVibration = {
[Enum.KeyCode.ButtonL2] = Enum.VibrationMotor.Small,
[Enum.KeyCode.ButtonR2] = Enum.VibrationMotor.Large,
}
local function onInputChanged(property)
if property == "Position" then
HapticService:SetMotor(inputType, vibrationMotor, input.Position.Z)
end
end
local function onInputBegan(input)
if not cachedInputs[input] then
local inputType = input.UserInputType
if inputType.Name:find("Gamepad") then
local vibrationMotor = keyToVibration[input.KeyCode]
if vibrationMotor then
-- Watch this input manually to accurately update the vibration motor
cachedInputs[input] = input.Changed:Connect(onInputChanged)
end
end
end
end
UserInputService.InputBegan:Connect(onInputBegan)