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
Résumé
Méthodes
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.
Returns true if the specified motor is available to be used with the specified Enum.UserInputType.
Returns true if the specified Enum.UserInputType supports haptic feedback.
- SetMotor(inputType : Enum.UserInputType,vibrationMotor : Enum.VibrationMotor,vibrationValues : Tuple):void
Sets the vibration intensity of the specified UserInputType and Enum.VibrationMotor.
Propriétés
Méthodes
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.
Paramètres
The specified Enum.UserInputType.
The specified Enum.VibrationMotor.
Retours
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.
Paramètres
The specific Enum.UserInputType being checked for Enum.VibrationMotor support.
The specified Enum.VibrationMotor checked to see if it supports the specified Enum.UserInputType.
Retours
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.
Paramètres
The specified Enum.UserInputType checked to see if it supports haptic feedback.
Retours
True if the specified Enum.UserInputType supports haptic feedback.
SetMotor
Sets the vibration intensity of the specified UserInputType and Enum.VibrationMotor. Note that almost all use cases specify Gamepad1 as the UserInputType.
Paramètres
The specified Enum.UserInputType.
The specified Enum.VibrationMotor.
How intensely the motor should vibrate. Only uses the first value in the tuple, which should be a number.
Retours
Échantillons de code
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)