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.

146 lines
4.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using VRTK;
  5. /// <summary>
  6. /// Class which deals with how objects behave when they are dropped
  7. /// </summary>
  8. [RequireComponent(typeof(VRTK_InteractableObject))]
  9. public class GravityData : MonoBehaviour
  10. {
  11. [Header("Data")]
  12. public string Name;
  13. public float Gravity;
  14. [Header("Visual Settings")]
  15. [SerializeField]
  16. private Transform m_planet;
  17. [SerializeField]
  18. private float m_rotationSpeed = 1;
  19. [SerializeField]
  20. private float m_bounceSpeed = 1;
  21. [SerializeField]
  22. private float m_bounceAmplitude = 0.1f;
  23. private Vector3 m_StartPos;
  24. public float m_randomOffset;
  25. /// <summary>
  26. /// The vrtk interactable object on this object
  27. /// </summary>
  28. private VRTK_InteractableObject m_interactable;
  29. /// <summary>
  30. /// Rigid body on this object
  31. /// </summary>
  32. private Rigidbody m_rigid;
  33. private void Awake()
  34. {
  35. //Set up some variables on start
  36. m_interactable = GetComponent<VRTK_InteractableObject>();
  37. m_rigid = GetComponentInChildren<Rigidbody>();
  38. m_StartPos = transform.position;
  39. m_randomOffset = Random.value * 20;
  40. }
  41. private void OnEnable()
  42. {
  43. RegisterEvents(true);
  44. }
  45. private void OnDisable()
  46. {
  47. RegisterEvents(false);
  48. }
  49. private void Update()
  50. {
  51. if (m_planet != null)
  52. {
  53. m_planet.Rotate(Vector3.up, 360 / m_rotationSpeed * Time.deltaTime, Space.Self);
  54. m_planet.localPosition = Vector3.up * Mathf.Sin(Time.time * m_bounceSpeed + m_randomOffset) * m_bounceAmplitude;
  55. }
  56. }
  57. private void OnGrab(object sender, InteractableObjectEventArgs args)
  58. {
  59. StopAllCoroutines();
  60. m_rigid.isKinematic = false;
  61. }
  62. private void OnDrop(object sender, InteractableObjectEventArgs args)
  63. {
  64. StopAllCoroutines();
  65. StartCoroutine(LerpToPosition(m_StartPos, Quaternion.identity, 2.0f));
  66. }
  67. private void OnSnap(object sender, InteractableObjectEventArgs args)
  68. {
  69. StopAllCoroutines();
  70. //m_rigid.isKinematic = true;
  71. }
  72. private void RegisterEvents(bool value)
  73. {
  74. if (value)
  75. {
  76. m_interactable.InteractableObjectSnappedToDropZone += OnSnap;
  77. m_interactable.InteractableObjectGrabbed += OnGrab;
  78. m_interactable.InteractableObjectUngrabbed += OnDrop;
  79. }else
  80. {
  81. m_interactable.InteractableObjectSnappedToDropZone -= OnSnap;
  82. m_interactable.InteractableObjectGrabbed -= OnGrab;
  83. m_interactable.InteractableObjectUngrabbed -= OnDrop;
  84. }
  85. }
  86. #region Coroutines
  87. /// <summary>
  88. /// Lerps object to a position over an amount of time
  89. /// </summary>
  90. /// <param name="SnapPos">End position</param>
  91. /// <param name="SnapRot">End rotation</param>
  92. /// <param name="snapTime">Time it takes to lerp</param>
  93. private IEnumerator LerpToPosition(Vector3 SnapPos, Quaternion SnapRot, float snapTime)
  94. {
  95. Debug.Log("Lerp to position");
  96. //This is based off of SteamVR Lerping code
  97. float dropTimer = -1;
  98. m_rigid.isKinematic = false;
  99. //once again this is all steamVR
  100. while (dropTimer < 1)
  101. {
  102. float t = Mathf.Pow(35, dropTimer);
  103. m_rigid.velocity = Vector3.Lerp(m_rigid.velocity, Vector3.zero, Time.fixedDeltaTime * 4);
  104. if (m_rigid.useGravity)
  105. m_rigid.AddForce(-Physics.gravity);
  106. transform.position = Vector3.Lerp(transform.position, SnapPos, Time.fixedDeltaTime * t * 3);
  107. transform.rotation = Quaternion.Slerp(transform.rotation, SnapRot, Time.fixedDeltaTime * t * 5);
  108. yield return new WaitForFixedUpdate();
  109. dropTimer += Time.fixedDeltaTime / (snapTime / 2);
  110. }
  111. //#Audio: object has just arrived at where it was lerping to
  112. //set correct transform in case object stuck
  113. m_rigid.isKinematic = true;
  114. transform.position = SnapPos;
  115. transform.rotation = SnapRot;
  116. }
  117. #endregion Coroutines
  118. }