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.

87 lines
1.9 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using VRTK;
  5. public class ResetAfterTime : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private float m_resetTime = 20;
  9. private VRTK_InteractableObject m_interactable;
  10. private Vector3 m_startPos;
  11. private Quaternion m_startRot;
  12. private Rigidbody m_rb;
  13. private void Awake()
  14. {
  15. m_interactable = GetComponent<VRTK_InteractableObject>();
  16. m_rb = GetComponent<Rigidbody>();
  17. m_startPos = transform.position;
  18. m_startRot = transform.rotation;
  19. }
  20. private IEnumerator resetAfterTime(float time)
  21. {
  22. yield return new WaitForSeconds(time);
  23. transform.position = m_startPos;
  24. transform.rotation = m_startRot;
  25. m_rb.velocity = Vector3.zero;
  26. m_rb.angularVelocity = Vector3.zero;
  27. }
  28. private void OnEnable()
  29. {
  30. RegisterEvents(true);
  31. }
  32. private void OnDisable()
  33. {
  34. RegisterEvents(false);
  35. }
  36. private void OnGrab(object sender, InteractableObjectEventArgs args)
  37. {
  38. StopAllCoroutines();
  39. }
  40. private void OnDrop(object sender, InteractableObjectEventArgs args)
  41. {
  42. StartCoroutine(resetAfterTime(m_resetTime));
  43. }
  44. private void OnSnap(object sender, InteractableObjectEventArgs args)
  45. {
  46. StopAllCoroutines();
  47. }
  48. private void RegisterEvents(bool value)
  49. {
  50. if (value)
  51. {
  52. m_interactable.InteractableObjectSnappedToDropZone += OnSnap;
  53. m_interactable.InteractableObjectGrabbed += OnGrab;
  54. m_interactable.InteractableObjectUngrabbed += OnDrop;
  55. }
  56. else
  57. {
  58. m_interactable.InteractableObjectSnappedToDropZone -= OnSnap;
  59. m_interactable.InteractableObjectGrabbed -= OnGrab;
  60. m_interactable.InteractableObjectUngrabbed -= OnDrop;
  61. }
  62. }
  63. }