LinearDrive.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Drives a linear mapping based on position between 2 positions
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. [RequireComponent( typeof( Interactable ) )]
  12. public class LinearDrive : MonoBehaviour
  13. {
  14. public Transform startPosition;
  15. public Transform endPosition;
  16. public LinearMapping linearMapping;
  17. public bool repositionGameObject = true;
  18. public bool maintainMomemntum = true;
  19. public float momemtumDampenRate = 5.0f;
  20. protected Hand.AttachmentFlags attachmentFlags = Hand.AttachmentFlags.DetachFromOtherHand;
  21. protected float initialMappingOffset;
  22. protected int numMappingChangeSamples = 5;
  23. protected float[] mappingChangeSamples;
  24. protected float prevMapping = 0.0f;
  25. protected float mappingChangeRate;
  26. protected int sampleCount = 0;
  27. protected Interactable interactable;
  28. protected virtual void Awake()
  29. {
  30. mappingChangeSamples = new float[numMappingChangeSamples];
  31. interactable = GetComponent<Interactable>();
  32. }
  33. protected virtual void Start()
  34. {
  35. if ( linearMapping == null )
  36. {
  37. linearMapping = GetComponent<LinearMapping>();
  38. }
  39. if ( linearMapping == null )
  40. {
  41. linearMapping = gameObject.AddComponent<LinearMapping>();
  42. }
  43. initialMappingOffset = linearMapping.value;
  44. if ( repositionGameObject )
  45. {
  46. UpdateLinearMapping( transform );
  47. }
  48. }
  49. protected virtual void HandHoverUpdate( Hand hand )
  50. {
  51. GrabTypes startingGrabType = hand.GetGrabStarting();
  52. if (interactable.attachedToHand == null && startingGrabType != GrabTypes.None)
  53. {
  54. initialMappingOffset = linearMapping.value - CalculateLinearMapping( hand.transform );
  55. sampleCount = 0;
  56. mappingChangeRate = 0.0f;
  57. hand.AttachObject(gameObject, startingGrabType, attachmentFlags);
  58. }
  59. }
  60. protected virtual void HandAttachedUpdate(Hand hand)
  61. {
  62. UpdateLinearMapping(hand.transform);
  63. if (hand.IsGrabEnding(this.gameObject))
  64. {
  65. hand.DetachObject(gameObject);
  66. }
  67. }
  68. protected virtual void OnDetachedFromHand(Hand hand)
  69. {
  70. CalculateMappingChangeRate();
  71. }
  72. protected void CalculateMappingChangeRate()
  73. {
  74. //Compute the mapping change rate
  75. mappingChangeRate = 0.0f;
  76. int mappingSamplesCount = Mathf.Min( sampleCount, mappingChangeSamples.Length );
  77. if ( mappingSamplesCount != 0 )
  78. {
  79. for ( int i = 0; i < mappingSamplesCount; ++i )
  80. {
  81. mappingChangeRate += mappingChangeSamples[i];
  82. }
  83. mappingChangeRate /= mappingSamplesCount;
  84. }
  85. }
  86. protected void UpdateLinearMapping( Transform updateTransform )
  87. {
  88. prevMapping = linearMapping.value;
  89. linearMapping.value = Mathf.Clamp01( initialMappingOffset + CalculateLinearMapping( updateTransform ) );
  90. mappingChangeSamples[sampleCount % mappingChangeSamples.Length] = ( 1.0f / Time.deltaTime ) * ( linearMapping.value - prevMapping );
  91. sampleCount++;
  92. if ( repositionGameObject )
  93. {
  94. transform.position = Vector3.Lerp( startPosition.position, endPosition.position, linearMapping.value );
  95. }
  96. }
  97. protected float CalculateLinearMapping( Transform updateTransform )
  98. {
  99. Vector3 direction = endPosition.position - startPosition.position;
  100. float length = direction.magnitude;
  101. direction.Normalize();
  102. Vector3 displacement = updateTransform.position - startPosition.position;
  103. return Vector3.Dot( displacement, direction ) / length;
  104. }
  105. protected virtual void Update()
  106. {
  107. if ( maintainMomemntum && mappingChangeRate != 0.0f )
  108. {
  109. //Dampen the mapping change rate and apply it to the mapping
  110. mappingChangeRate = Mathf.Lerp( mappingChangeRate, 0.0f, momemtumDampenRate * Time.deltaTime );
  111. linearMapping.value = Mathf.Clamp01( linearMapping.value + ( mappingChangeRate * Time.deltaTime ) );
  112. if ( repositionGameObject )
  113. {
  114. transform.position = Vector3.Lerp( startPosition.position, endPosition.position, linearMapping.value );
  115. }
  116. }
  117. }
  118. }
  119. }