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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Ghost Replay For Racing Game

Discussion in 'Scripting' started by unity_qx9jFbktFBXe9g, Jan 25, 2020.

  1. unity_qx9jFbktFBXe9g

    unity_qx9jFbktFBXe9g

    Joined:
    Mar 26, 2019
    Posts:
    3
    Hello, I have been struggling for awhile now to get my racing replay system to work. I have finally got it storing the positions and rotations to playback, however when I play back the ghost while testing it is too fast. I want to make it go the same speed as the original car but I can not figure out how to do it.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     public float movementSpeed, rotationSpeed;
    8.     public float fbInput, lrInput;
    9.  
    10.     public List<PointInTime> ghostPointsInTime;
    11.  
    12.     public bool isReplaying;
    13.     public GameObject ghostCar;
    14.  
    15.     private float nextActionTime = 0.0f;
    16.     public float period = 0.5f;
    17.  
    18.     void Start()
    19.     {
    20.         movementSpeed = 8.0f;
    21.         rotationSpeed = 100.0f;
    22.         ghostPointsInTime = new List<PointInTime>();
    23.         ghostCar = GameObject.FindGameObjectWithTag("Ghost");
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         fbInput = Input.GetAxis("Vertical");
    29.         lrInput = Input.GetAxis("Horizontal");
    30.         Move();
    31.  
    32.         if (Input.GetKeyDown(KeyCode.Return))
    33.             StartReplay();
    34.         if (Input.GetKeyUp(KeyCode.Return))
    35.             StopReplay();
    36.     }
    37.  
    38.     public void FixedUpdate()
    39.     {
    40.         if (isReplaying)
    41.             Replay();          
    42.         else
    43.             RecordMovement();
    44.     }
    45.  
    46.     public void Move()
    47.     {
    48.         transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed * fbInput);
    49.         transform.Rotate(Vector3.up * Time.deltaTime * rotationSpeed * lrInput);
    50.        
    51.     }
    52.  
    53.     public void RecordMovement()
    54.     {
    55.         if (Time.time > nextActionTime)
    56.         {
    57.             nextActionTime = Time.time + period;
    58.             ghostPointsInTime.Add(new PointInTime(transform.position, transform.rotation));
    59.             Debug.Log("Recording");
    60.         }
    61.     }
    62.  
    63.     public void Replay()
    64.     {
    65.         if (ghostPointsInTime.Count > 0)
    66.         {
    67.             PointInTime pointInTime = ghostPointsInTime[0];
    68.             ghostCar.transform.position = pointInTime.position;
    69.             Debug.Log(pointInTime.position);
    70.             ghostCar.transform.rotation = pointInTime.rotation;
    71.             Debug.Log(pointInTime.rotation);
    72.             ghostPointsInTime.RemoveAt(0);
    73.         }
    74.  
    75.         else
    76.         {
    77.             StopReplay();
    78.         }
    79.  
    80.     }
    81.  
    82.     public void StartReplay()
    83.     {
    84.         isReplaying = true;
    85.     }
    86.  
    87.     public void StopReplay()
    88.     {
    89.         isReplaying = false;
    90.     }
    91. }
    92.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Time.time
    is actually the time since the game booted. I think that's your problem.

    You can use
    Time.timeSinceLevelLoad
    instead but I highly recommend you track your own notion of playback time in a floating point variable, then advance it each frame yourself with :

    Code (csharp):
    1. myTime += Time.deltaTime;
    You probably want to do the same thing time-wise (starting from zero) when you record and when you playback.

    Full reference on Unity Time here:

    https://docs.unity3d.com/ScriptReference/Time.html
     
  3. unity_qx9jFbktFBXe9g

    unity_qx9jFbktFBXe9g

    Joined:
    Mar 26, 2019
    Posts:
    3
    Thank you for the response, I will try that out and take a look through the documentation