HapticService

非推奨を表示
作成できません
サービス
複製されていません

Modern controllers and devices have motors built in to provide haptic feedback. Adding rumbles and vibrations can provide subtle feedback that is hard to convey through visuals or audio.

Roblox supports haptics for the following devices:

  • Android and iOS phones supporting haptics including most iPhone, Pixel, and Samsung Galaxy devices
  • PlayStation gamepads
  • Xbox gamepads
  • Quest Touch controller

概要

方法

プロパティ

方法

GetMotor

Returns the current vibration value set to the specified UserInputType and Enum.VibrationMotor. This will not return anything if SetMotor has not been called prior.

パラメータ

The specified Enum.UserInputType.

vibrationMotor: Enum.VibrationMotor

The specified Enum.VibrationMotor.


戻り値

The current vibration value set to the specified Enum.UserInputType and Enum.VibrationMotor or nil if SetMotor has not been called prior.

IsMotorSupported

Returns true if the specified motor is available to be used with the specified Enum.UserInputType.

パラメータ

The specific Enum.UserInputType being checked for Enum.VibrationMotor support.

vibrationMotor: Enum.VibrationMotor

The specified Enum.VibrationMotor checked to see if it supports the specified Enum.UserInputType.


戻り値

True if the specified motor is available to be used with the specified Enum.UserInputType, false if not.

IsVibrationSupported

Returns true if the specified Enum.UserInputType supports haptic feedback.

パラメータ

The specified Enum.UserInputType checked to see if it supports haptic feedback.


戻り値

True if the specified Enum.UserInputType supports haptic feedback.

SetMotor

void

Sets the vibration intensity of the specified UserInputType and Enum.VibrationMotor. Note that almost all use cases specify Gamepad1 as the UserInputType.

パラメータ

The specified Enum.UserInputType.

vibrationMotor: Enum.VibrationMotor

The specified Enum.VibrationMotor.

vibrationValues: Tuple

How intensely the motor should vibrate. Only uses the first value in the tuple, which should be a number.


戻り値

void

コードサンプル

HapticService:SetMotor

local UserInputService = game:GetService("UserInputService")
local HapticService = game:GetService("HapticService")
local cachedInputs = {} -- Note that we use a cache so we don't attach a Changed event more than once.
local keyToVibration = {
[Enum.KeyCode.ButtonL2] = Enum.VibrationMotor.Small,
[Enum.KeyCode.ButtonR2] = Enum.VibrationMotor.Large,
}
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 InputObject manually so we can accurately update the vibrationMotor.
local function onChanged(property)
if property == "Position" then
HapticService:SetMotor(inputType, vibrationMotor, input.Position.Z)
end
end
cachedInputs[input] = input.Changed:Connect(onChanged)
end
end
end
end
UserInputService.InputBegan:Connect(onInputBegan)

イベント