- Home /
Accessing postion of an object x seconds ago in c #
Hi all,
I'm trying to find the cleanest way to access the position that an object was in X seconds ago. Are there functions to do this easily or do I need to constantly write the position data into an array, etc... If it's the array solution I'm curious how the code for this would look like.
Thanks so much, Saschka
Answer by kurtdog · Aug 17, 2018 at 03:20 PM
I've gone with the array solution until I can find something better. I tore a few methods out of one of my classes so there may be some small errors here. But you can get a gist of the algorithm I'm using.
private List<Vector3> _positionBuffer;
private float _timer = 0;
private bool _positionBufferFull = false;
private int _positionBufferStepSize = 0;
[SerializeFields]
private GameObject[] _objects;
private void Start()
{
_positionBuffer = new List<Vector3>();
}
private void Update()
{
//fill the position buffer
if(_timer < _time)
{
_positionBuffer.Add(transform.position);
_timer += Time.deltaTime;
}
else
{
if(!_positionBufferFull)
{
_positionBufferFull = true;
_positionBufferStepSize = _positionBuffer.Count / (_numCopies+1);
}
_positionBuffer.Add(transform.position);
_positionBuffer.RemoveAt(0);
}
private void PositionObjectsBasedOnTime()
{
for (int i = 0; i < _trail.Count; i++)
{
int index = (i + 1) * _positionBufferStepSize;
//Debug.Log(i + " % " + percent + " d " + distance);
_objects[_trail.Count - i -1].transform.position =_positionBuffer[index];
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612112332im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to access a non static timer to store in highscore 1 Answer
Minute Timer Issue 1 Answer
Unity and music tracks - load track at certain point and fading tracks in and out 1 Answer
weapon bobbing 1 Answer
Time-based progression 1 Answer