How can I make an object follow another's path with some time delay?

I need to make an object move exactly as another one, but with a delay of a few seconds. The “master” object’s movements are not predictable.
I have tried a lot of different approaches, such as using the Invoke and Sleep method, but nothing seems to have the desired effect. Help would be so much appreciated!

Which error are you getting? Are you coding in JS, because yield WaitForSeconds() is only valid in JS, in C# it would be yield return new WaitForSeconds(5);.

There are at least two parts to the problems. One is storing the data and revisiting it for the “ghost” object. Another is dealing with prediction since just copying the position it had one frame may not give the smoothest experience if the game is not running at a solid frame rate.

Basically, you would keep an array large enough to store time and transformation position (time, position, rotation, scale, whatever you need?). If your game runs at 30 fps and you need 2 second delay, you need 302=60 elements in the array. If your game runs at 60 fps and you need 2 second delay, you need 602=120 elements in the array. You can also approximate by only recording position every x frames.

Each frame, record the timestamp and the positional data. Use an indexer variable that you keep incrementing that loops around to the start of the array once it hits the end of it. Something like this:

data *= someData;*

i = (i + 1) % data.Length;
When it comes to reading the data, keep another indexer or time variable and find the two entries in your array between “last update” and “now”, and get a new position by interpolating the “then” and “now” for the ghost. You can think of this as a circular array with two indexers chasing each other.
If you’ve thought about the problem for a bit, you’ll also realise that your game might potentially run at variable frame rates. Figuring out a stable system may take some thinking to get it right.