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.

117 lines
3.3 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using VRTK;
  5. public class ReturnToStart : MonoBehaviour
  6. {
  7. private Vector3 m_StartPos;
  8. private Quaternion m_StartRot;
  9. private VRTK_InteractableObject m_interactable;
  10. private Rigidbody m_rigid;
  11. // Start is called before the first frame update
  12. void Awake()
  13. {
  14. m_StartPos = transform.position;
  15. m_StartRot = transform.rotation;
  16. //Set up some variables on start
  17. m_interactable = GetComponent<VRTK_InteractableObject>();
  18. m_rigid = GetComponentInChildren<Rigidbody>();
  19. }
  20. private void OnEnable()
  21. {
  22. RegisterEvents(true);
  23. }
  24. private void OnDisable()
  25. {
  26. RegisterEvents(false);
  27. }
  28. private void OnGrab(object sender, InteractableObjectEventArgs args)
  29. {
  30. StopAllCoroutines();
  31. m_rigid.isKinematic = false;
  32. }
  33. private void OnDrop(object sender, InteractableObjectEventArgs args)
  34. {
  35. //other hand might still be grabbed
  36. if (!m_interactable.IsGrabbed())
  37. {
  38. StopAllCoroutines();
  39. StartCoroutine(LerpToPosition(m_StartPos, m_StartRot, 2.0f));
  40. }
  41. }
  42. private void OnSnap(object sender, InteractableObjectEventArgs args)
  43. {
  44. StopAllCoroutines();
  45. //m_rigid.isKinematic = true;
  46. }
  47. private void RegisterEvents(bool value)
  48. {
  49. if (value)
  50. {
  51. m_interactable.InteractableObjectSnappedToDropZone += OnSnap;
  52. m_interactable.InteractableObjectGrabbed += OnGrab;
  53. m_interactable.InteractableObjectUngrabbed += OnDrop;
  54. }
  55. else
  56. {
  57. m_interactable.InteractableObjectSnappedToDropZone -= OnSnap;
  58. m_interactable.InteractableObjectGrabbed -= OnGrab;
  59. m_interactable.InteractableObjectUngrabbed -= OnDrop;
  60. }
  61. }
  62. #region Coroutines
  63. /// <summary>
  64. /// Lerps object to a position over an amount of time
  65. /// </summary>
  66. /// <param name="SnapPos">End position</param>
  67. /// <param name="SnapRot">End rotation</param>
  68. /// <param name="snapTime">Time it takes to lerp</param>
  69. private IEnumerator LerpToPosition(Vector3 SnapPos, Quaternion SnapRot, float snapTime)
  70. {
  71. Debug.Log("Lerp to position");
  72. //This is based off of SteamVR Lerping code
  73. float dropTimer = -1;
  74. m_rigid.isKinematic = false;
  75. //once again this is all steamVR
  76. while (dropTimer < 1)
  77. {
  78. float t = Mathf.Pow(35, dropTimer);
  79. m_rigid.velocity = Vector3.Lerp(m_rigid.velocity, Vector3.zero, Time.fixedDeltaTime * 4);
  80. if (m_rigid.useGravity)
  81. m_rigid.AddForce(-Physics.gravity);
  82. transform.position = Vector3.Lerp(transform.position, SnapPos, Time.fixedDeltaTime * t * 3);
  83. transform.rotation = Quaternion.Slerp(transform.rotation, SnapRot, Time.fixedDeltaTime * t * 5);
  84. yield return new WaitForFixedUpdate();
  85. dropTimer += Time.fixedDeltaTime / (snapTime / 2);
  86. }
  87. //#Audio: object has just arrived at where it was lerping to
  88. //set correct transform in case object stuck
  89. m_rigid.isKinematic = true;
  90. transform.position = SnapPos;
  91. transform.rotation = SnapRot;
  92. }
  93. #endregion Coroutines
  94. }