- Home /
how to make an object track the other object's exact movements
hey guys, so this script helps me to to tell the follower object to copy exact movements of the leader object ,i found this script in unity forums and it works pretty good acutely. but my question is how can i add some kind speed variable to it .how can i tell the follower object to move at lower speed? using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class delayTracker : MonoBehaviour
{
float progress;
const int MAX_FPS = 60;
public Transform leader;
public float lagSeconds = 0.5f;
Vector3[] _positionBuffer;
float[] _timeBuffer;
int _oldestIndex;
int _newestIndex;
// Use this for initialization
void Start()
{
int bufferLength = Mathf.CeilToInt(lagSeconds * MAX_FPS);
_positionBuffer = new Vector3[bufferLength];
_timeBuffer = new float[bufferLength];
_positionBuffer[0] = _positionBuffer[1] = leader.position;
_timeBuffer[0] = _timeBuffer[1] = Time.time;
_oldestIndex = 0;
_newestIndex = 1;
}
void LateUpdate()
{
// Insert newest position into our cache.
// If the cache is full, overwrite the latest sample.
int newIndex = (_newestIndex + 1) % _positionBuffer.Length;
if (newIndex != _oldestIndex)
_newestIndex = newIndex;
_positionBuffer[_newestIndex] = leader.position;
_timeBuffer[_newestIndex] = Time.time;
// Skip ahead in the buffer to the segment containing our target time.
float targetTime = Time.time - lagSeconds;
int nextIndex;
while (_timeBuffer[nextIndex = (_oldestIndex + 1) % _timeBuffer.Length] < targetTime)
_oldestIndex = nextIndex;
// Interpolate between the two samples on either side of our target time.
float span = _timeBuffer[nextIndex] - _timeBuffer[_oldestIndex];
float progress = 0f;
if (span > 0f)
{
progress = (targetTime - _timeBuffer[_oldestIndex]) / span;
}
transform.position = Vector3.Lerp(_positionBuffer[_oldestIndex], _positionBuffer[nextIndex], progress );
}
void OnDrawGizmos()
{
if (_positionBuffer == null || _positionBuffer.Length == 0)
return;
Gizmos.color = Color.grey;
Vector3 oldPosition = _positionBuffer[_oldestIndex];
int next;
for (int i = _oldestIndex; i != _newestIndex; i = next)
{
next = (i + 1) % _positionBuffer.Length;
Vector3 newPosition = _positionBuffer[next];
Gizmos.DrawLine(oldPosition, newPosition);
oldPosition = newPosition;
}
}
}
Do you mean with a delay or a slower speed, as with a slower speed it will get further and further behind till it doesnt even appear to be copying the 1st objects movements?
the script has already a delay variable , what i need is to tell the follower object to move alone the path at much lower speed. right now if i move the leader object quickly, the follower object will also moves quickly as well .but i need the follower to move at my desired speed ,not the leader's speed
Answer by tormentoarmagedoom · May 06, 2019 at 11:21 AM
Good day.
I did not read the code, but i did something similar in the past, (I was using NavMeshSystem to move the object)s and I created a Vector3 array, storing the position of first object every 0.5 seconds, and then make the 2nd object follow that Vector3 positions one by one.
hey , yeah i thought about that too and i was watching this video https://www.youtube.com/watch?v=N$$anonymous$$h-tkf3iaQ but i was wondering if there was any way not use nav mesh agent .just send a normal gameObject along that path