HapticService

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

無法建立
服務
未複製

現代控制器和設備有內置馬達提供觸摸式回饋。添加漂亮和振動可以提供難以通過視覺或音訊頻傳輸的觀點提出的微妙反饋。

Roblox 支援以下設備的震動:

  • 支援震動的 Android 和 iOS 手機,包括大多數 iPhone、Pixel 和 Samsung Galaxy 裝置
  • PlayStation 遊戲控制器
  • Xbox 遊戲控制器
  • 任務控制器

概要

方法

屬性

方法

GetMotor

將目前的振動值設為指定的 UserInputTypeEnum.VibrationMotor 。如果 SetMotor 尚未呼叫,將不會返回任何內容。

參數

指定的 Enum.UserInputType

vibrationMotor: Enum.VibrationMotor

指定的 Enum.VibrationMotor


返回

目前的振動值設為指定的 Enum.UserInputTypeEnum.VibrationMotor 或 null 如果 SetMotor 未被呼叫。

IsMotorSupported

如果指定的引擎可用,返回 true 。

參數

正在檢查 Enum.UserInputTypeEnum.VibrationMotor 協助。

vibrationMotor: Enum.VibrationMotor

指定的 Enum.VibrationMotor 檢查是否支援指定的 Enum.UserInputType


返回

對於指定的 Enum.UserInputType 可用,且指定的引擎可用,否則為 false。

IsVibrationSupported

如果指定的 Enum.UserInputType 支援觸摸回饋,則返回 true。

參數

指定的 Enum.UserInputType 檢查是否支援觸摸式反回饋。


返回

對於具有指定 Enum.UserInputType 的輸入類型,是否支援觸摸式反回饋。

SetMotor

void

設定指定 UserInputTypeEnum.VibrationMotor 的震動強度。注意,幾乎所有的使用案例都指定 Gamepad1 作為 1> Class.InputObject.UserInputType|UserInputType1> 。

參數

指定的 Enum.UserInputType

vibrationMotor: Enum.VibrationMotor

指定的 Enum.VibrationMotor

vibrationValues: Tuple

發動機應該震動得多麼強。 只使用數值在表中的第一個值,這應該是數字。


返回

void

範例程式碼

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.

HapticService:SetMotor()

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)

活動