Search Unity

Rewinding time and projectiles

Discussion in 'Scripting' started by Corva-Nocta, Mar 22, 2021.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hey all, I started working on a side project for fun and wanted to figure out something if I can. I am making an fps where you can rewind time, pretty fun so far! I followed the Brackeys video to set up the rewinding, pretty easy to do. Now I am trying to make it work for projectile attacks like rockets. The base part of the rewinding is pretty easy, its the same as everything else. Where I am having trouble though is figuring out how to turn the object off and on. Essentially the rocket will move forward until it hits something, then it will turn off the rocket object (and later turn on an explosion FX) but I need to make it so when I am rewinding time I know to turn the object back on. Any ideas of how I would set this up?

    I'm not exactly sure which part of code I would need to post, but I'll add what I think is useful.

    The basics of the rewinding and recording:
    Code (csharp):
    1. void Rewind()
    2.     {
    3.         if (pointsInTime.Count > 0)
    4.         {
    5.             PointInTime pointInTime = pointsInTime[0];
    6.             transform.position = pointInTime.position;
    7.             transform.rotation = pointInTime.rotation;
    8.             if (hp)
    9.             {
    10.                 hp.currentHealth = pointInTime.currentHealth;
    11.             }
    12.             if (isSpawned) // destroy if returned to its origin
    13.             {
    14.                 if (transform.position == spawnLocation)
    15.                 {
    16.                     Destroy(gameObject);
    17.                 }
    18.             }
    19.             pointsInTime.RemoveAt(0);
    20.         }
    21.         else
    22.         {
    23.             StopRewind();
    24.         }
    25.     }
    26.     void Record()
    27.     {
    28.         if (pointsInTime.Count > Mathf.Round(rewindTime / Time.fixedDeltaTime))
    29.         {
    30.             pointsInTime.RemoveAt(pointsInTime.Count - 1);
    31.         }
    32.         if (hp)
    33.         {
    34.             pointsInTime.Insert(0, new PointInTime(transform.position, transform.rotation, hp.currentHealth));
    35.         }
    36.         else
    37.         {
    38.             pointsInTime.Insert(0, new PointInTime(transform.position, transform.rotation, -1));
    39.         }
    40.     }
    I have a separate script that controls the projectile aspect of the object. Right now its just a simple script to move the object forward, do some damage, and turn the object off on impact.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I think when an object is destroyed (such as the rocket), you would need to keep a disabled copy of it around so that you could turn it back on when in reverse... as far as playing the particle explosion itself in reverse... well, that sounds a LOT hairier! I'm not even sure how that would work.
     
  3. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Ended up doing this little change and it seems to be working so far!

    Code (csharp):
    1. void Rewind()
    2.     {
    3.         if (pointsInTime.Count > 0)
    4.         {
    5.             PointInTime pointInTime = pointsInTime[0];
    6.             transform.position = pointInTime.position;
    7.             transform.rotation = pointInTime.rotation;
    8.             if (hp)
    9.             {
    10.                 hp.currentHealth = pointInTime.currentHealth;
    11.             }
    12.             if (pointInTime.isActive)
    13.             {
    14.                 isActive = true;
    15.             }
    16.             if (isSpawned) // destroy if returned to its origin
    17.             {
    18.                 if (transform.position == spawnLocation)
    19.                 {
    20.                     Destroy(gameObject);
    21.                 }
    22.             }
    23.             pointsInTime.RemoveAt(0);
    24.         }
    25.         else
    26.         {
    27.             StopRewind();
    28.         }
    29.     }
    30.     void Record()
    31.     {
    32.         int newHP;
    33.         if (pointsInTime.Count > Mathf.Round(rewindTime / Time.fixedDeltaTime))
    34.         {
    35.             pointsInTime.RemoveAt(pointsInTime.Count - 1);
    36.         }
    37.         if (hp)
    38.         {
    39.             newHP = hp.currentHealth;
    40.         }
    41.         else
    42.         {
    43.             newHP = -1;
    44.         }
    45.         pointsInTime.Insert(0, new PointInTime(transform.position, transform.rotation, newHP, isActive));
    46.     }
     
  4. Seehundy1995

    Seehundy1995

    Joined:
    Mar 22, 2021
    Posts:
    6
    Sorry i am new here...