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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Ghost replay function

Discussion in 'Scripting' started by Verdemis, Mar 14, 2018.

  1. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Hello everyone,

    I'm at the moment thinking about a ghost function for a new project. It's about a side scrolling 2D game and I want to replay the player movement and actions in the next run.

    What would be the best way to approach this problem?

    I know I could save the player position at every frame but this sound like an absolut overkill. With the movement I could use interpolation to prevent to save every frame. But I need to know when the player was shooting too. How could I save both of these informations the best?
     
  2. diasrodrigo

    diasrodrigo

    Joined:
    Jul 4, 2017
    Posts:
    420
    I think save the position each frame is the way to do this. you can restrict how many seconds you want to save position to avoid an overkill.

    Maybe this video can help you. About knowing when to shoot you can add a new variable in the class to save and add a bool to know when it has shot.
     
  3. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Thank you for your reply. I already know this video from brackeys. A restriction in time is not possible in my case. It has to cover the full level playtime.
     
    diasrodrigo likes this.
  4. diasrodrigo

    diasrodrigo

    Joined:
    Jul 4, 2017
    Posts:
    420
    I think it will not be simple. It's the way I can imagine by now. If I find something I post here.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    There are only two practical possibilities: either record the position and state of the game objects, or record the inputs and recalculate all the motion & state from that (which requires that your controller code is absolutely repeatable).

    If it were me, I'd probably record the position and state for each frame. You say this is overkill, but is it really? Let's say you use a struct containing XY position (8 bytes) and some sort of state number (4 bytes; I assume you get this from your controller state machine, and it's enough to imply which sprite to show, when to spawn a bullet, etc.). That's 12 bytes per frame. If a run is 3 minutes long at 60 fps, that's 10,800 frames, or about 126k of data. No big deal.

    Or you go with the other approach, recording the inputs. In this case you only need to add an entry to your record when the inputs change, which is probably not every frame. But in this case you must be very, very careful that your controller plays out exactly the same every time, or small errors will accumulate and could end up resulting in a completely different outcome (like your ghost getting stuck behind an obstacle that should have been cleared). That means no physics, no multiplying by Time.deltaTime; everything has to be done on a frame-by-frame basis, old school.

    It is possible to do a sort of hybrid approach that replays from inputs, but includes a "sync" frame every so often (say, once per second), with a full record of the state at that point. This prevents errors from accumulating, and even if the ghost gets stuck, it will be only temporary. The drawback of course is that if such events happen, the player will notice the ghost jumping from point to point in an unnatural manner.
     
    Verdemis likes this.
  6. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Thank you JoeStrout, that is really helpful.
     
    JoeStrout likes this.
  7. SamohtVII

    SamohtVII

    Joined:
    Jun 30, 2014
    Posts:
    364