Advertisement
Guest User

Untitled

a guest
May 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5.  
  6. public class TimeManager : MonoBehaviour {
  7. [SerializeField]
  8. private List<timeInstance> times;
  9. private bool isRewinding;
  10. private Rigidbody rb;
  11. public bool destroyOnStartAgain;
  12. public bool backToObjectOnStartAgain;
  13. public megacube objectToGoBackTo;
  14.  
  15. public bool clearOnEnable;
  16.  
  17. private Vector3 VelAtStopRewind;
  18.  
  19. private bool justSpawned = true;
  20.  
  21.  
  22. public bool getIsRewinding()
  23. {
  24. return isRewinding;
  25. }
  26. // Use this for initialization
  27. void OnEnable()
  28. {
  29. if (clearOnEnable)
  30. {
  31. setNotRewinding();
  32. justSpawned = true;
  33. times = new List<timeInstance>();
  34. if (transform.GetComponent<Rigidbody>() != null)
  35. {
  36. rb = transform.GetComponent<Rigidbody>();
  37. }
  38. }
  39. }
  40. void Start()
  41. {
  42. if (!clearOnEnable)
  43. {
  44. setNotRewinding();
  45. justSpawned = true;
  46. times = new List<timeInstance>();
  47. if (transform.GetComponent<Rigidbody>() != null)
  48. {
  49. rb = transform.GetComponent<Rigidbody>();
  50. }
  51. }
  52. }
  53. void Update()
  54. {
  55.  
  56. if (Input.GetKeyDown(KeyCode.Return))
  57. {
  58. setIsRewinding();
  59. }
  60. if (Input.GetKeyUp(KeyCode.Return))
  61. {
  62. setNotRewinding();
  63. }
  64. justSpawned = false;
  65. }
  66.  
  67. void FixedUpdate()
  68. {
  69. if (isRewinding)
  70. {
  71. rewind1step();
  72. } else
  73. {
  74. addToTimes();
  75. }
  76. }
  77. void setIsRewinding()
  78. {
  79. isRewinding = true;
  80. if (rb != null)
  81. {
  82. rb.isKinematic = true;
  83. }
  84. }
  85. void setNotRewinding()
  86. {
  87. isRewinding = false;
  88. if (rb != null)
  89. {
  90. rb.isKinematic = false;
  91. rb.velocity = VelAtStopRewind;
  92. }
  93. }
  94.  
  95.  
  96. void addToTimes()
  97. {
  98. if (rb != null)
  99. {
  100. times.Insert(0, new timeInstance(transform.localPosition, transform.localRotation, rb.velocity));
  101. }
  102. else
  103. {
  104. times.Insert(0, new timeInstance(transform.localPosition, transform.localRotation));
  105. }
  106. }
  107.  
  108. void rewind1step()
  109. {
  110. if (times.Count < 1 && !justSpawned)
  111. {
  112. //setNotRewinding();
  113.  
  114. if (destroyOnStartAgain)
  115. {
  116. GameObject.Destroy(gameObject);
  117. }
  118. if (backToObjectOnStartAgain)
  119. {
  120. gameObject.GetComponent<BoxCollider>().enabled = false;
  121. gameObject.GetComponent<Rigidbody>().isKinematic = true;
  122. transform.parent = objectToGoBackTo.transform;
  123. if (!objectToGoBackTo.transform.GetComponent<TimeManager>().enabled)
  124. {
  125. objectToGoBackTo.enabled = true;
  126. objectToGoBackTo.transform.GetComponent<TimeManager>().enabled = true;
  127. objectToGoBackTo.transform.GetComponent<Rigidbody>().isKinematic = false;
  128. objectToGoBackTo.transform.GetComponent<BoxCollider>().enabled = true;
  129. }
  130.  
  131. this.enabled = false;
  132. }
  133.  
  134. return;
  135. }
  136. timeInstance temp = times[0];
  137. times.RemoveAt(0);
  138. transform.localPosition = temp.position;
  139. transform.localRotation = temp.rotation;
  140.  
  141. if (rb != null)
  142. {
  143. VelAtStopRewind = temp.velocity;
  144. }
  145. }
  146. }
  147. [Serializable]
  148. public struct timeInstance
  149. {
  150. public Vector3 position;
  151. public Quaternion rotation;
  152. public Vector3 velocity;
  153. public timeInstance(Vector3 _pos, Quaternion _rot, Vector3 _velocity)
  154. {
  155. position = _pos;
  156. rotation = _rot;
  157. velocity = _velocity;
  158. }
  159. public timeInstance(Vector3 _pos, Quaternion _rot)
  160. {
  161. position = _pos;
  162. rotation = _rot;
  163. velocity = new Vector3(0,0,0);
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement