HapticService

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务
未复制

现代控制器和设备有内置电机提供触摸反馈。添加噪声和振动可以提供隐微的反馈,这很难通过视觉或音频传达。

Roblox 支持以下设备的震动:

  • 支持 Android 和 iOS 的手机,包括大多数 iPhone、Pixel 和 Samsung Galaxy 设备
  • 播放站游戏手柄
  • Xbox 游戏手柄
  • 任务触控器

概要

方法

属性

方法

GetMotor

将当前的振动值设置为指定的 UserInputTypeEnum.VibrationMotor 。如果 SetMotor 未调用,将返回无。

参数

指定的 Enum.UserInputType

vibrationMotor: Enum.VibrationMotor

指定的 Enum.VibrationMotor


返回

当前振动值设置为指定的 Enum.UserInputTypeEnum.VibrationMotor 或为 nil 如果 SetMotor 未调用先前。

IsMotorSupported

如果指定的机器可用,返回 true 使用指定的 Enum.UserInputType

参数

正在检查 Enum.UserInputTypeEnum.VibrationMotor 协助。

vibrationMotor: Enum.VibrationMotor

指定的 Enum.VibrationMotor 检查它是否支持指定的 Enum.UserInputType


返回

如果指定的发动机可用于指定的 Enum.UserInputType ,则为 true 。 如果不是,则为 false 。

IsVibrationSupported

返回 true 如果指定的 Enum.UserInputType 支持触摸反馈。

参数

指定的 Enum.UserInputType 检查是否支持触摸反馈。


返回

枚列表.UserInputType 支持触摸反馈。

SetMotor

void

设置指定的 UserInputTypeEnum.VibrationMotor 的振动强度。注意,几乎所有的使用例子都指定 Gamepad1 作为 2>Class.InputObject.UserInputType2> 。

参数

指定的 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)

活动