Ghost/replay system

Hi,

I am trying to create a system that allow to recreate player’s drive path. Vehicle is a rigidbody object with physic, “ghost” is simple transform that interpolates between positions and rotations from drivee path.
How it works now? I check every time step player’s vehicle position and rotation and save it in an array. It looks like this:

	public void mark() {
		time += Time.deltaTime;
		if(time >= TIME_STEP) {
			time = 0;
			interpolations.Add(player.transform.position);
		}
	}

When I read array I try to interpolate between current position and destination point. Code looks like this:

	void readMark() {
		time += Time.deltaTime;
		if(time >= TIME_STEP) {
			time = 0;
			if(index >= (interpolations.Length -1)) {
				index = 0;
			} else {
				index++;
			}
			moveDirection = (interpolations[index] - transform.position).normalized;
			velocity = ((interpolations[index] - transform.position).magnitude / TIME_STEP);
		}
		transform.position += moveDirection * (velocity * Time.deltaTime);
	}

This code works… more or less. More, cus path is right, but less, cus you can notice juttering while moving. Any ideas how it should be done without annoying jittering?

Maybe store your last position at TIME_STEP and use Mathf.Lerp to interploate smoothly?

I actually take position at TIME_STEP. For example, if TIME_STEP equals 1, that means I take position every second. I tried to make TIME_STEP smaller to have more data, but jittering still exists.

Ok, this was easy if you think for a while :smile:.

Update isn’t called with same interval, so I have statement time >= TIME_STEP. And this was a problem, cus first script could save points after undefined time step (bigger or equal to TIME_STEP) and second script could read data after different time step.

I did two coroutines, reader and writer, both waits my TIME_STEP, so now writing and loading points are synchronized with same time intervals.

Interpolation is key with stuff like this. You probably want to use linear (“Lerp”), and you want to apply it to both position and rotation. You’ll also need a fairly short timestep, otherwise it’ll still look really floaty.