Skip to content
EN

Play Haptics from a Script

This page covers how to play haptics from inside your game logic, such as taking damage, a bullet hitting, or picking up an item.

There are two ways to play from a script.

MethodEventMapWhen to use
A. Call a Trigger component (the default)Used. Which haptic to play, at what strength, and on which device are set in the EventMapMost cases
B. Call HapbeatManager directlyNot used. The Kit Event ID or AudioClip, strength, and destination are specified in codeWhen the EventMap cannot manage it, e.g. the Event ID is built at runtime

For playing directly from collisions, the Animator, UI, and so on without writing code (via components), see Trigger Components.

Your script holds only a reference to a Trigger component (HapbeatUnityEventTrigger). Which entry of which EventMap to play is selected in the Trigger component’s Inspector, so the EventMap does not appear in the code.

Your script (e.g. PlayerHealth)
└ _damageHaptic ──reference──▶ HapbeatUnityEventTrigger (a component in the scene)
├ Event Map : MyScene-EventMap.asset ← chosen in the Inspector
└ Event : player_damage ← chosen in the Inspector
│
▼
EventMap entry (Mode / Clip / Gain / Target)
│
▼
HapbeatManager → Hapbeat device
  • HapbeatUnityEventTrigger is a MonoBehaviour provided by the SDK, with public methods such as Fire() (play) / Stop() (stop). For the list, see Script API Reference.
  • “UnityEvent” in its name means you can also call Fire() from a UnityEvent such as a Button’s OnClick without code. The same component is used when calling from a script.
  • To change the strength or destination, edit the EventMap. You do not need to rewrite the script.

1. Create an entry in the EventMap

Open Hapbeat → Open Event Map, add an entry with the + button in the toolbar, and set the following.

ItemExample setting
Nameplayer_damage (the name shown in the Trigger’s Event dropdown)
ModeSet CLIP (Stream Clip) and assign a WAV to Clip to try it without deploying a Kit
GainAround 0.5
TargetingEmpty (all devices)

For the meaning of each item, see EventMap Window. The ▶ button on a row lets you try playback without entering Play mode.

2. Place a Trigger component and select the EventMap and entry

  1. Select any GameObject in the Hierarchy (the player itself or [Hapbeat Event Router] both work)
  2. Add Component → Hapbeat → Hapbeat UnityEvent Trigger
  3. In the Inspector, select the EventMap from step 1 in Event Map, and player_damage in the Event dropdown

When you have several haptics to play, place one Trigger per entry (several can be on the same GameObject).

3. Call Fire() from the script

using UnityEngine;
using Hapbeat; // the SDK namespace
public class PlayerHealth : MonoBehaviour
{
// Drag the Trigger from step 2 onto this field in the Inspector.
// The EventMap and entry to play are selected in that Trigger's Inspector.
[SerializeField] private HapbeatUnityEventTrigger _damageHaptic;
public void TakeDamage(int amount)
{
// ...reduce HP...
_damageHaptic.Fire(); // play the EventMap entry player_damage
}
}

Press Play, and if the device vibrates when TakeDamage() is called, the setup is complete.

Play with a different strength — Put a multiplier (0–2) in GainMultiplier and then Fire(). The sent value is “the Kit clip gain (clip_gain) × entry Gain × GainMultiplier”, capped at 1.

// The more damage taken, the stronger
_damageHaptic.GainMultiplier = Mathf.Clamp01(amount / 50f);
_damageHaptic.Fire();

Loop playback, then stop — Set the entry to CLIP (Stream Clip) + Loop, and call Fire() to start and Stop() to end.

[SerializeField] private HapbeatUnityEventTrigger _engineHaptic; // a Trigger with a Loop entry selected
void StartEngine() => _engineHaptic.Fire();
void StopEngine() => _engineHaptic.Stop();

Change strength and left / right while it plays — Rewriting GainMultiplier or Pan (−1 = left to +1 = right) during the loop is reflected in the playing vibration immediately. The same can be done without code using Parameter Binding.

void Update()
{
// The faster the speed, the stronger
_engineHaptic.GainMultiplier = Mathf.InverseLerp(0f, 30f, _speed);
}

Grab → while held → release — To handle the three stages of a one-shot at the start, a continuous loop, and a one-shot at the end together, use the Hapbeat Sequence Trigger. From a script, call Fire() (grabbed) and Stop() (released).

[SerializeField] private HapbeatSequenceTrigger _grabHaptic;
void OnGrab() => _grabHaptic.Fire(); // On Start one-shot → Loop starts
void OnRelease() => _grabHaptic.Stop(); // Loop stops → On Stop one-shot

Temporarily disable playback — While TriggerEnabled is false, calling Fire() does not play.

_damageHaptic.TriggerEnabled = false;

This method uses neither a Trigger component nor the EventMap, and passes everything about the playback to HapbeatManager.

// FIRE (Command): play by Event ID from a Kit already deployed to the device
HapbeatManager.Instance?.Play("my-kit.enemy_hit", gain: 0.8f);
// CLIP (Stream): send a Unity AudioClip and play it. Stop or change strength with the returned handle
HapbeatStreamPlayback pb = HapbeatManager.Instance?.StreamAudioClip(myClip, gain: 0.5f);
pb?.Stop();

Because it does not go through the EventMap, the EventMap’s Gain and Target, the Kit clip gain (clip_gain), and latency compensation do not apply. Use it in cases such as:

  • Building the Event ID at runtime (e.g. $"heartbeat.player_{id}" per player)
  • There are too many entries to manage in the EventMap

You can also call HapbeatManager directly while reading the EventMap settings. The Showcase’s ChargeShooter.cs is an example: it passes GetEffectiveGain() / target / streamClip of an entry taken out with _eventMap.FindById(...) to the Manager.

The Showcase sample plays haptics from a script in these three places.

ZoneScriptMethodWhat it does
Z3 FishingFishingController.csA_sequence.Fire() when the mouse is pressed, _sequence.Stop() when released. These two lines are the only haptics-related code
Z4 Stream ConsoleStreamDemoController.csAToggles Fire() / Stop() of HapbeatUnityEventTrigger with the Space key, and uses ActivePlayback to tell whether it is playing
Z5 Charge & ShootChargeShooter.csBReads EventMap entries, calls HapbeatManager directly, and changes the strength during playback according to the charge amount

For what each zone contains, see Showcase Sample.

SymptomWhat to check
Nothing happens on Fire()Whether [Hapbeat] Fire rejected: no EventMap assigned appears in the Console (no Event Map selected in the Trigger)
entry id '...' not found warningThe entry was deleted from the EventMap. Pick the entry again in the Trigger’s Event field
Plays the first time but not when called repeatedlyThe Trigger’s Cooldown is greater than 0
Exception because _damageHaptic is nullThe Trigger was not assigned to the script’s field
CLIP plays but FIRE does notThe Kit is not deployed to the device (Fire vs. Clip — When to Use Each)