What's the logic behind "racing with your previous score" in racing games?

Hello everyone,

I wasn’t able to fit my question into the header and I don’t think my english is good enough to explain it but I will try. I was often seeing racing games where you try to beat your highest score, and your previous gameplay was shown on the screen like a transparent AI car moving next to you. I really wonder how to achive something like that. I’m not thinking doing something like that but I can’t stop thinking about how to achive that.

Thank you for the help, cheers!

You need to record the position of the car and it’s rotation. It’s sufficient to record a delta over threshold to save data.

Do you mean you record position and rotation on every frame? How do you then display this data? Thank you for the answer btw.

Save data in a struct with pos and rot of the car you want to save. When saving it, save the time also so that when it’s replayed, it’ll be replayed exactly the same times as the normal version. I’d use a fixed time scale.

Something like this should work then,

-Save the pos, rot and the time spend since the start on a struct (in FixedUpdate?) ,
-When displaying the data again, starting from the first data on the struct, check if the time spend since the start has reached the current records time, if so, set the pos and rot of the car to the current datas values and get the next data(in FixedUpdate again?).

Putting all together so maybe someone can use this.

I will definitely try this. Thank you.

no a fixed Time Scale not Fixed Update, fixed update, updates every 3 frames, which isn’t bad, but it may cause jitteryness. (Unless you’re using forces) then it may be applicable, but I’d ask someone more experienced with that one lol.

FixedUpdate interval can be changed in Project Settings > Time. Reduce that to avoid jitteryness.

But doesn’t that cause more stress on the system as Physics has to be calculated more?

You need to save a float inside update when it’s recording and replay for each time.

float CurrentTime = 0;
bool isRecording = false;

void Update(){
      if(isRecording){
            CurrentTime += Time.deltaTime;
            //SAVE STUFF
      }
}

void PlayBackUpdate(){
    mTime += Time.deltaTime;

       //allocated time, play frames
}

He wants the invisible car thing as well lol.

He just would use a shader for that…

It’s called a ghost car. (To ghost your previous drive).

As others have said, store the position and rotation of your vehicle as you race in a struct of some sort in Fixedupdate.
Then when you start the next race, create an additional vehicle that reads, at every FixedUpdate, what it’s position and rotation were at that FixedUpdate increment.

You can use any Transparent shader for your ghost vehicle.

It does, but matching the FixedUpdate interval to the target frame time should also see benefits when driving the car. I was offering one possible solution, but it will have a cost like most options will. Projects vary, so it may or may not be practical in any individual case.

1 Like