-
Notifications
You must be signed in to change notification settings - Fork 0
/
delayTracker.cs
84 lines (71 loc) · 2.4 KB
/
delayTracker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class delayTracker : MonoBehaviour
{
public Transform tracker;
public float delayTime = 0.5f;
private List<trackerPos> trackerPoses;
private int bufferSize = 100;
private float fps;
private int counter = 0;
Referances referances;
void Awake()
{
resetPosesBuffer(delayTime);
referances = GameObject.FindGameObjectWithTag("GameManager").GetComponent<Referances>();
}
private void Start()
{
}
void FixedUpdate()
{
if (tracker == null)
{
if (GetComponent<LittleOnes>().whichChild == 1 && !referances.manager.stop)
{
tracker = referances.player.transform;
}
else if (GetComponent<LittleOnes>().whichChild != 1 && !referances.manager.stop)
{
tracker = referances.littleOnes[GetComponent<LittleOnes>().whichChild - 2].transform;
}
}
if (bufferSize > trackerPoses.Count) //grow the array until it reached the buffer size & do nothing until it is full
{
trackerPoses.Add(new trackerPos(tracker.position, tracker.rotation));
return;
}
trackerPoses[counter % bufferSize] = new trackerPos(tracker.position, tracker.rotation);
int followIndex = (counter % bufferSize) + 1; //point to the index furthest from just set
if (followIndex == bufferSize) //handle out of bounds, set back to 0
followIndex = 0;
transform.position = trackerPoses[followIndex].position;
transform.rotation = trackerPoses[followIndex].rotation;
counter++;
}
public void resetPositionBufferSize(InputField trackerDelayInputField)
{
delayTime = float.Parse(trackerDelayInputField.text);
resetPosesBuffer(delayTime);
}
void resetPosesBuffer(float _delayTime)
{
counter = 0;
fps = 1.0f / Time.deltaTime;
bufferSize = Mathf.CeilToInt(fps * _delayTime);
trackerPoses = new List<trackerPos>();
trackerPoses.Add(new trackerPos(tracker.position, tracker.rotation)); //fill first index
}
}
class trackerPos
{
public Vector3 position;
public Quaternion rotation;
public trackerPos(Vector3 _pos, Quaternion _rot)
{
position = _pos;
rotation = _rot;
}
}