Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'General Discussion' started by OnurYIL, Jan 17, 2015.

  1. OnurYIL

    OnurYIL

    Joined:
    Nov 4, 2013
    Posts:
    140
    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!
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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.
     
  3. OnurYIL

    OnurYIL

    Joined:
    Nov 4, 2013
    Posts:
    140
    Do you mean you record position and rotation on every frame? How do you then display this data? Thank you for the answer btw.
     
  4. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,605
    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.
     
  5. OnurYIL

    OnurYIL

    Joined:
    Nov 4, 2013
    Posts:
    140
    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.
     
  6. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    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.
     
  7. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,571
    FixedUpdate interval can be changed in Project Settings > Time. Reduce that to avoid jitteryness.
     
  8. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    But doesn't that cause more stress on the system as Physics has to be calculated more?
     
  9. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,605
    You need to save a float inside update when it's recording and replay for each time.

    Code (CSharp):
    1. float CurrentTime = 0;
    2. bool isRecording = false;
    3.  
    4. void Update(){
    5.       if(isRecording){
    6.             CurrentTime += Time.deltaTime;
    7.             //SAVE STUFF
    8.       }
    9. }
    10.  
    11. void PlayBackUpdate(){
    12.     mTime += Time.deltaTime;
    13.  
    14.        //allocated time, play frames
    15. }
     
  10. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884

    He wants the invisible car thing as well lol.
     
  11. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,605
    He just would use a shader for that..
     
  12. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,797
    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.
     
  13. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,571
    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.
     
    N1warhead likes this.