HapticService

显示已弃用

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

无法创建
服务
未复制

概要

方法

属性

方法

GetMotor

将当前振动值设置为指定的 UserInputTypeEnum.VibrationMotor 。如果 SetMotor() 先没有调用,将不会返回任何东西。

参数

指定的 Enum.UserInputType

默认值:""
vibrationMotor: Enum.VibrationMotor

指定的 Enum.VibrationMotor

默认值:""

返回

当前振动值设置为指定的 Enum.UserInputTypeEnum.VibrationMotornil 如果 SetMotor() 尚未调用前。

IsMotorSupported

返回 true 如果指定的电机可用于与指定的 Enum.UserInputType 一起使用。

参数

正在检查特定的 Enum.UserInputType 以获得 Enum.VibrationMotor 协助持。

默认值:""
vibrationMotor: Enum.VibrationMotor

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

默认值:""

返回

如果指定的发动机可以与指定的 一起使用,则 否则。

IsVibrationSupported

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

参数

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

默认值:""

返回

如果指定的 true 支持触觉反馈,Boolean 的值为 Enum.UserInputType

SetMotor

()

设置指定的 inputTypevibrationMotor 的振动强度。请注意,几乎所有使用案例都指定 Enum.UserInputType.Gamepad1inputType ,这在内部映射到设备的相应硬件。

参数

指定的 Enum.UserInputType

默认值:""
vibrationMotor: Enum.VibrationMotor

指定的 Enum.VibrationMotor

默认值:""
vibrationValues: Tuple

电机应振动的程度。仅使用 tuple 中的第一个值,该值应为数字。

默认值:""

返回

()

代码示例

这个例子使小型马达根据左侧触发器受到的压力量来振动,大型马达根据右侧触发器受到的压力量来振动。

震动服务:设置电机()

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
-- 手动观察此输入以准确更新振动电机
cachedInputs[input] = input.Changed:Connect(onInputChanged)
end
end
end
end
UserInputService.InputBegan:Connect(onInputBegan)

活动