Link Search Menu Expand Document

Triggering Haptic Clips

🔥NOTE: At the time of this writing, the Studio plug-in for Unity is in beta and has very limited functionality. Specifically, it is currently possible to only load one haptic clip into a Haptic Source per GameObject and play only one Haptic Source at any given time. Future releases will address this limitation.

Triggering the Haptic Source of a GameObject can be achieved via simple Play and Stop commands.

Play

This function plays back the haptic clip specified with the GameObject’s Haptic Source. In this current beta version, playback of a haptic source is not triggered automatically (e.g. by proximity between the Haptic Receiver and the game object the Haptic Source is attached to) and must always be triggered with this function.

hapticSource.Play()

NOTE: In this version, the haptic clip will always be played unaltered—no attempts will be made to change the haptic intensity, position, or speed based on the location of the Haptic Source relative to the Haptic Receiver.

❗ on Android: playback of haptic clips with more than ~50 breakpoints might feel “stretched” during playback making clips play longer than designed. However, if your app is CPU load intensive, this might not happen. This is a current limitation of the Android Vibration API and it has been fixed by the Android team for future major platform releases (after API Level 30).

Priority

There is the possibility to set the Haptic Source playback priority.

If a Haptic Source is currently playing , another Haptic Source can steal the playback if it has higher priority. With the same logic, a Haptic Source will not trigger playback if a Haptic Source with higher priority is already playing.

The priority can be assigned in the Inspector View of Haptic Source.

Focus change

If the Unity app focus is changed and the app goes into background, the Haptic Source playback will be stopped. Once the app comes comes back from the background, the Haptic Source playback won’t be resumed from where it was before the app went into background.

NOTE: Future releases will address this limitation and resume haptics playback, similar to what Audio Source does.

Stop

This function can stop the playback of a Haptic Source if it’s currently playing; if not playing, this function will do nothing.

hapticSource.Stop()

Changing the HapticClip for a Haptic Source

You can change the haptic clip assigned to a Haptic Source from code using the clip property:

public class PlayerCharacter : MonoBehaviour
{
    public HapticClip jumpHaptic; // Creates an GameObject GUI element to select a jump haptic.
    public HapticClip landHaptic; // Creates an GameObject GUI element to select a land haptic.
    private HapticSource hapticSource;

    void Start()
    {
        hapticSource = GetComponent<HapticSource>();
    }

    public void Jump()
    {
        hapticSource.clip = jumpHaptic; // Assigns the jumpHaptic into the Haptic Source
        hapticSource.Play(); // Plays the jumpHaptic
    }

    public void Land()
    {
        hapticSource.clip = landHaptic; // Assigns the landHaptic into the Haptic Source
        hapticSource.Play(); // Plays the landHaptic
    }
}

Changing the Haptic Source Priority

You can change the priority of a Haptic Source from code using the priority property:

hapticSource.priority = 0; // Sets the Haptic Source priority to 0 (highest).

Checking haptics playback support

To check if the device supports Lofelt Haptics playback, you can call a Haptic Receiver class function:

HapticReceiver.DeviceMeetsMinimumRequirements();

This will return True if the device supports rich haptics playback and False otherwise.

Unity Demo Example

Included in the Lofelt Unity Asset Package is an demo example that makes use of the Studio plug-in for generating haptics. It shows how to load separate haptic clips onto different buttons and then play them individually when clicked, as well as assigning haptic source playback priority. You can find the example in the Asset Package at Lofelt/Examples/Demo.

< Prev Next >


Copyright © 2020-2021 Lofelt GmbH. All rights reserved.