Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 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. private List<timeInstance> times;
  8. private bool isRewinding;
  9. private Rigidbody rb;
  10.  
  11. private Vector3 VelAtStopRewind;
  12.  
  13. // Use this for initialization
  14. void Start()
  15. {
  16. times = new List<timeInstance>();
  17. if (transform.GetComponent<Rigidbody>() != null)
  18. {
  19. rb = transform.GetComponent<Rigidbody>();
  20. }
  21. }
  22. void Update()
  23. {
  24. if (Input.GetKeyDown(KeyCode.Return))
  25. {
  26. setIsRewinding();
  27. }
  28. if (Input.GetKeyUp(KeyCode.Return))
  29. {
  30. setNotRewinding();
  31. }
  32. }
  33.  
  34. void FixedUpdate()
  35. {
  36. if (isRewinding)
  37. {
  38. rewind1step();
  39. } else
  40. {
  41. addToTimes();
  42. }
  43. }
  44. void setIsRewinding()
  45. {
  46. isRewinding = true;
  47. if (rb != null)
  48. {
  49. rb.isKinematic = true;
  50. }
  51. }
  52. void setNotRewinding()
  53. {
  54. isRewinding = false;
  55. if (rb != null)
  56. {
  57. rb.isKinematic = false;
  58. rb.velocity = VelAtStopRewind;
  59. }
  60. }
  61.  
  62.  
  63. void addToTimes()
  64. {
  65. if (rb != null)
  66. {
  67. times.Insert(0, new timeInstance(transform.localPosition, transform.localRotation, rb.velocity));
  68. }
  69. else
  70. {
  71. times.Insert(0, new timeInstance(transform.localPosition, transform.localRotation));
  72. }
  73. }
  74.  
  75. void rewind1step()
  76. {
  77. if (times.Count < 1)
  78. {
  79. setNotRewinding();
  80. return;
  81. }
  82. timeInstance temp = times[0];
  83. times.RemoveAt(0);
  84. transform.localPosition = temp.position;
  85. transform.localRotation = temp.rotation;
  86.  
  87. if (rb != null)
  88. {
  89. VelAtStopRewind = temp.velocity;
  90. }
  91. }
  92. }
  93. [Serializable]
  94. public struct timeInstance
  95. {
  96. public Vector3 position;
  97. public Quaternion rotation;
  98. public Vector3 velocity;
  99. public timeInstance(Vector3 _pos, Quaternion _rot, Vector3 _velocity)
  100. {
  101. position = _pos;
  102. rotation = _rot;
  103. velocity = _velocity;
  104. }
  105. public timeInstance(Vector3 _pos, Quaternion _rot)
  106. {
  107. position = _pos;
  108. rotation = _rot;
  109. velocity = new Vector3(0,0,0);
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement