DistanceHaptics.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Triggers haptic pulses based on distance between 2 positions
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. public class DistanceHaptics : MonoBehaviour
  12. {
  13. public Transform firstTransform;
  14. public Transform secondTransform;
  15. public AnimationCurve distanceIntensityCurve = AnimationCurve.Linear( 0.0f, 800.0f, 1.0f, 800.0f );
  16. public AnimationCurve pulseIntervalCurve = AnimationCurve.Linear( 0.0f, 0.01f, 1.0f, 0.0f );
  17. //-------------------------------------------------
  18. IEnumerator Start()
  19. {
  20. while ( true )
  21. {
  22. float distance = Vector3.Distance( firstTransform.position, secondTransform.position );
  23. Hand hand = GetComponentInParent<Hand>();
  24. if (hand != null)
  25. {
  26. float pulse = distanceIntensityCurve.Evaluate( distance );
  27. hand.TriggerHapticPulse((ushort)pulse);
  28. //SteamVR_Controller.Input( (int)trackedObject.index ).TriggerHapticPulse( (ushort)pulse );
  29. }
  30. float nextPulse = pulseIntervalCurve.Evaluate( distance );
  31. yield return new WaitForSeconds( nextPulse );
  32. }
  33. }
  34. }
  35. }