WfGrabbableObject.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(Rigidbody))]
  4. [RequireComponent(typeof(VelocityTracker))]
  5. public class WfGrabbableObject : VRGrabbable
  6. {
  7. public Rigidbody rigidBody { get; private set; }
  8. public VelocityTracker velocityTrack { get; private set; }
  9. protected override void Awake()
  10. {
  11. base.Awake();
  12. this.rigidBody = base.GetComponent<Rigidbody>();
  13. this.velocityTrack = base.GetComponent<VelocityTracker>();
  14. }
  15. public override void GrabBegin(VRGrabber hand, Collider grabPoint)
  16. {
  17. base.GrabBegin(hand, grabPoint);
  18. }
  19. public override void GrabEnd(Vector3 linearVelocity, Vector3 angularVelocity)
  20. {
  21. float magnitude = linearVelocity.magnitude;
  22. if (this.throwForceThreshold <= magnitude)
  23. {
  24. if (this.onThrowObjectCorrectionVelocityEvent != null)
  25. {
  26. this.onThrowObjectCorrectionVelocityEvent(ref linearVelocity, ref angularVelocity);
  27. }
  28. base.GrabEnd(linearVelocity * this.throwForceRate, angularVelocity);
  29. if (this.isThrowCorrection)
  30. {
  31. this.PlayLookRotationAnime(Quaternion.LookRotation(linearVelocity.normalized * -1f));
  32. }
  33. }
  34. else
  35. {
  36. base.GrabEnd(linearVelocity * Mathf.Max(1f, magnitude / this.throwForceThreshold * this.throwForceRate), angularVelocity);
  37. }
  38. if (this.onThrowObjectEvent != null)
  39. {
  40. this.onThrowObjectEvent(this, this.throwForceThreshold > magnitude);
  41. }
  42. }
  43. public void PlayLookRotationAnime(Quaternion targetRotation)
  44. {
  45. this.backupRotation = base.transform.rotation;
  46. this.targetRotation = targetRotation;
  47. this.isRotationAnime = true;
  48. this.animeValue = (this.animeValue2 = 0f);
  49. this.lastY = 0f;
  50. this.rigidBody.freezeRotation = true;
  51. }
  52. public void StopLookRotationAnime()
  53. {
  54. this.isRotationAnime = false;
  55. this.rigidBody.freezeRotation = false;
  56. }
  57. public void Update()
  58. {
  59. if (this.isRotationAnime)
  60. {
  61. this.animeValue += ((this.trackedLinearCorrectionTime != 0f) ? (1f / this.trackedLinearCorrectionTime * Time.deltaTime) : 1f);
  62. Mathf.Clamp(this.animeValue, 0f, 1f);
  63. if (1f <= this.animeValue)
  64. {
  65. Vector3 trackedLinearVelocity = this.velocityTrack.TrackedLinearVelocity;
  66. float num = Vector3.Dot(Vector3.down, trackedLinearVelocity);
  67. if (0f < num)
  68. {
  69. if (this.animeValue2 == 0f)
  70. {
  71. this.backupRotation = base.transform.rotation;
  72. }
  73. this.animeValue2 += ((this.fallDownCorrectionTime != 0f) ? (1f / this.fallDownCorrectionTime * Time.deltaTime) : 1f);
  74. Mathf.Clamp(this.animeValue2, 0f, 1f);
  75. Vector3 forward = (trackedLinearVelocity.normalized * -1f + new Vector3(0f, -0.65f, 0f)).normalized;
  76. float y = (forward.y >= this.lastY) ? forward.y : this.lastY;
  77. forward = new Vector3(forward.x, y, forward.z);
  78. base.transform.rotation = Quaternion.Lerp(this.backupRotation, Quaternion.LookRotation(forward), this.animeValue2);
  79. }
  80. else
  81. {
  82. Vector3 forward = trackedLinearVelocity.normalized * -1f;
  83. this.lastY = forward.y;
  84. }
  85. }
  86. else
  87. {
  88. base.transform.rotation = Quaternion.Lerp(this.backupRotation, this.targetRotation, this.animeValue);
  89. }
  90. }
  91. }
  92. private void OnCollisionEnter(Collision collision)
  93. {
  94. this.StopLookRotationAnime();
  95. this.rigidBody.velocity = Vector3.zero;
  96. if (this.onCollisionEnter != null)
  97. {
  98. this.onCollisionEnter(this, collision);
  99. }
  100. }
  101. [SerializeField]
  102. protected bool isThrowCorrection;
  103. [SerializeField]
  104. protected float trackedLinearCorrectionTime;
  105. [SerializeField]
  106. protected float fallDownCorrectionTime;
  107. [SerializeField]
  108. protected float throwForceThreshold = 3f;
  109. [SerializeField]
  110. protected float throwForceRate = 1f;
  111. public WfGrabbableObject.OnThrowObjectEvent onThrowObjectEvent;
  112. public Action<WfGrabbableObject, Collision> onCollisionEnter;
  113. public WfGrabbableObject.OnThrowObjectCorrectionVelocityEvent onThrowObjectCorrectionVelocityEvent;
  114. private Quaternion backupRotation;
  115. private Quaternion targetRotation;
  116. private bool isRotationAnime;
  117. private float animeValue;
  118. private float animeValue2;
  119. private float lastY;
  120. public delegate void OnThrowObjectEvent(WfGrabbableObject throwObject, bool mistakeThrow);
  121. public delegate void OnThrowObjectCorrectionVelocityEvent(ref Vector3 linearVelocity, ref Vector3 angularVelocity);
  122. }