Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.5 KiB

  1. // Hip Tracking|Presence|70050
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// Attempts to provide the relative position of a hip without the need for additional hardware sensors.
  7. /// </summary>
  8. /// <remarks>
  9. /// **Script Usage:**
  10. /// * Place the `VRTK_HipTracking` script on any active scene GameObject and this GameObject will then track to the estimated hip position.
  11. /// </remarks>
  12. [AddComponentMenu("VRTK/Scripts/Presence/VRTK_HipTracking")]
  13. public class VRTK_HipTracking : MonoBehaviour
  14. {
  15. [Tooltip("Distance underneath Player Head for hips to reside.")]
  16. public float HeadOffset = -0.35f;
  17. [Header("Optional")]
  18. [Tooltip("Optional Transform to use as the Head Object for calculating hip position. If none is given one will try to be found in the scene.")]
  19. public Transform headOverride;
  20. [Tooltip("Optional Transform to use for calculating which way is 'Up' relative to the player for hip positioning.")]
  21. public Transform ReferenceUp;
  22. protected Transform playerHead;
  23. protected virtual void Awake()
  24. {
  25. VRTK_SDKManager.AttemptAddBehaviourToToggleOnLoadedSetupChange(this);
  26. }
  27. protected virtual void OnEnable()
  28. {
  29. playerHead = (headOverride != null ? headOverride : VRTK_DeviceFinder.HeadsetTransform());
  30. }
  31. protected virtual void OnDestroy()
  32. {
  33. VRTK_SDKManager.AttemptRemoveBehaviourToToggleOnLoadedSetupChange(this);
  34. }
  35. protected virtual void LateUpdate()
  36. {
  37. if (playerHead == null)
  38. {
  39. return;
  40. }
  41. Vector3 up = Vector3.up;
  42. if (ReferenceUp != null)
  43. {
  44. up = ReferenceUp.up;
  45. }
  46. transform.position = playerHead.position + (HeadOffset * up);
  47. Vector3 forward = playerHead.forward;
  48. Vector3 forwardLeveld1 = forward;
  49. forwardLeveld1.y = 0;
  50. forwardLeveld1.Normalize();
  51. Vector3 mixedInLocalForward = playerHead.up;
  52. if (forward.y > 0)
  53. {
  54. mixedInLocalForward = -playerHead.up;
  55. }
  56. mixedInLocalForward.y = 0;
  57. mixedInLocalForward.Normalize();
  58. float dot = Mathf.Clamp(Vector3.Dot(forwardLeveld1, forward), 0f, 1f);
  59. Vector3 finalForward = Vector3.Lerp(mixedInLocalForward, forwardLeveld1, dot * dot);
  60. transform.rotation = Quaternion.LookRotation(finalForward, up);
  61. }
  62. }
  63. }