// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using System.Collections; using UltimateXR.Avatar; using UltimateXR.Core; using UltimateXR.Core.Components; using UnityEngine; namespace UltimateXR.Haptics.Helpers { /// /// Component that will send haptic feedback while enabled. /// public class UxrFixedHapticFeedback : UxrComponent { #region Inspector Properties/Serialized Fields [SerializeField] private UxrHandSide _handSide = UxrHandSide.Left; [SerializeField] private UxrHapticMode _hapticMixMode = UxrHapticMode.Mix; [SerializeField] [Range(0, 1)] private float _amplitude = 0.5f; [SerializeField] private float _frequency = 100.0f; #endregion #region Public Types & Data /// /// Gets or sets the target hand. /// public UxrHandSide HandSide { get => _handSide; set => _handSide = value; } /// /// Gets or sets the haptic playback mix mode. /// public UxrHapticMode HapticMixMode { get => _hapticMixMode; set => _hapticMixMode = value; } /// /// Gets or sets the haptic signal amplitude. /// public float Amplitude { get => _amplitude; set => _amplitude = value; } /// /// Gets or sets the haptic signal frequency. /// public float Frequency { get => _frequency; set => _frequency = value; } #endregion #region Unity /// /// Starts the haptic coroutine. /// protected override void OnEnable() { base.OnEnable(); _hapticsCoroutine = StartCoroutine(HapticsCoroutine()); } /// /// Stops the haptic coroutine. /// protected override void OnDisable() { base.OnDisable(); StopCoroutine(_hapticsCoroutine); } #endregion #region Coroutines /// /// Coroutine that sends continuous, fixed, haptic feedback to the target controller. /// /// Coroutine enumerator private IEnumerator HapticsCoroutine() { yield return null; while (true) { if (isActiveAndEnabled && UxrAvatar.LocalAvatar) { SendHapticClip(_handSide); } yield return new WaitForSeconds(UxrConstants.InputControllers.HapticSampleDurationSeconds); } } #endregion #region Private Methods /// /// Sends the haptic feedback. /// /// Target hand private void SendHapticClip(UxrHandSide handSide) { UxrAvatar.LocalAvatarInput.SendHapticFeedback(handSide, _frequency, _amplitude, UxrConstants.InputControllers.HapticSampleDurationSeconds); } #endregion #region Private Types & Data private Coroutine _hapticsCoroutine; #endregion } }