Timeline.cs
1    using System.Collections;
2    using System.Collections.Generic;
3    using UnityEngine;
4    using UnityEngine.UI;
5    
6    
7    public class Timeline : MonoBehaviour
8    {
9        [Header("Setup")]
10       public Slider timelineSlider;
11       public Slider velocitySlider;
12       public GameObject pivot;
13   
14       public VariableData data;
15   
16       [Header("While running")]
17       
18       public bool active; //for recording
19       public bool play; //for replaying;
20   
21       float startTime;
22   
23       public List<Point> pointsList = new List<Point>();
24       public int index = 0;
25   
26   
27       // Start is called before the first frame update
28       void Start()
29       {
30           velocitySlider.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
31       }
32   
33   
34   
35       private void Update()
36       {
37           if(active)
38           {
39               timelineSlider.value += Time.deltaTime;
40           }
41           else if(play && pointsList.Count != 0)
42           {
43               timelineSlider.value += Time.deltaTime;
44   
45               if(Time.time - startTime >= pointsList[index].GetTime() && index < pointsList.Count) //at each time stamp updates the pivot
46               {
47                   pivot.GetComponent<Rigidbody2D>().angularVelocity = pointsList[index].GetVel();
48                   //pivot.GetComponent<RectTransform>().eulerAngles = new Vector3(0, 0, pointsList[index].GetRot()); //setting rotation creates glitchy sim
49                   index++;
50               }
51   
52               if(index >= pointsList.Count)
53               {
54                   play = false;
55                   index = 0;
56               }
57   
58           }
59       }
60   
61   
62   
63       public void ClickPlay()
64       {
65           //restart timeline and play by play
66           //or stop and restart timeline
67   
68           if(!active)
69           {
70               timelineSlider.value = 0;
71               play = !play;
72   
73               if (play == false)
74               {
75   
76                   data.GetComponent<VariableData>().active = true;
77               }
78               else
79               {
80                   pivot.GetComponent<RectTransform>().eulerAngles = new Vector3(0, 0, pointsList[index].GetRot());
81   
82                   data.GetComponent<VariableData>().active = false;
83               }
84   
85               startTime = Time.time;
86           }
87       }
88   
89       public void ClickRecord()
90       {
91           
92   
93           if(!play)
94           {
95               active = !active;
96               timelineSlider.value = 0;
97   
98               if (active)
99               {
100  
101                  pointsList = new List<Point>(); //resets list when recording again
102  
103                  startTime = Time.time;
104                  ValueChangeCheck(); //stores initial value
105              }
106              else
107              {
108                  Point temp = new Point(Time.time - startTime, 0, pivot.GetComponent<RectTransform>().eulerAngles.z);
109                  pointsList.Add(temp); //sets final position 
110  
111                  startTime = Time.time;
112              }
113          }
114  
115          
116  
117      }
118  
119      public void ValueChangeCheck()
120      {
121          if(active)//because can be accessed even when not recording
122          {
123              pointsList.Add(new Point(Time.time - startTime, pivot.GetComponent<Rigidbody2D>().angularVelocity, pivot.GetComponent<RectTransform>().eulerAngles.z));
124          }
125  
126  
127      }
128  }
129