Moongate Games UK

"Changing worlds, one line of code at a time"

Tutorial: Ghosts #2 - Recording the Data

Introduction:

In this part of the tutorial we'll be making a script to record the data from an objects transform to make a basic ghost system.

Recording:

First things first we'll need to create a new script called GhostRecording.cs this will be where all the magic to record the ghost will happen. So to start with we'll need to add the following two namespaces to the default script.

using System.Collections.Generic;
using Ghosts;

The reason we need the Generic Collections is because we're going to be using a List to store the snapshots and the reason we need Ghosts is because we need that for access the Snapshot class.

So far your code should look like this:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Ghosts;

public class GhostRecording : MonoBehaviour {
    
    //Use this for initialization
    void Start(){
    
    }
    
    //Update is called once per frame
    void Update(){
    
    }
}

This as it stands will do nothing, so we need to add some variables above the Start() function but still within the GhostRecording class.

These variables are as follows:

//Public variables.
public Transform ObjectToRecord;
public float TimePerSnapshot = 0.1f;

//Private variables.
private List<Snapshot> _snapshots;
private float _time;
private float _currentTime = 0.0f;

Once you have the those variables done, we next need to populate the Start() and Update() functions like the following:

void Start() {
    //Initialize a new Snapshot list.
    _snapshots = new List<Snapshot>();
}

void Update(){
    //increment the time.
    _time += Time.deltaTime;
    
    SnapshotUpdate(); //This Doesn't exist yet so don't worry about an error.
}

For the moment not much will happen as the SnapshotUpdate() function doesn't exist yet, so that's what we're going to get to next.

Basically what the SnapshotUpdate function does is increment the _currentTime byTime.deltaTime,  check that the _currentTime is greater than or equal to TimePerSnapshot and then record the data if it is. After the data has been recorded the _currentTime variable is set back to 0.0f. Rinse and Repeat.

This is the code that is needed for it to work:

void SnapshotUpdate(){
    _currentTime += Time.deltaTime;
    
    //Check if the current time is greater or equal to TimePerSnapshot.
    if(_currentTime >= TimePerSnapshot){
        var position = ObjectToRecord.position;
        var rotation = ObjecttoRecord.rotation;
        
        //Add a new snapshot to the snapshot list.
        _snapshots.Add(new Snapshot(position.x,position.y, position.z,rotation.x, rotation.y, rotation.z, rotation.w,_time));
        
        //Set the _currentTime to 0.
        _currentTime = 0.0f;
    }
}

After all this you should have the following code for recording ghosts.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;;
using Ghosts;

public class GhostRecording : MonoBehaviour {
    
    //Public variables.
    public Transform ObjectToRecord;
    public float TimePerSnapshot = 0.1f;
    
    //Private variables
    private List<Snapshot> _snapshots;
    private float _time;
    private float _currentTime = 0.0f;
    
    //Use this for initialization
    void Start(){
        //Initialize a new snapshit list.
        _snapshots = new List<Snapshot>();    
    }
    
    //Update is called once per frame
    void Update(){
        //increment the time.
        _time += Time.deltaTime;
        
        SnapshotUpdate();
    }
    
    void SnapshotUpdate(){
        _currentTime += Time.deltaTime;
        
        //Check if the current time is greater or equal to TimePerSnapshot.
        if(_currentTime >= TimePerSnapshot){
            var position = ObjectToRecord.position;
            var rotation = ObjectToRecord.rotation;
            
            //Add a new snapshot to the snapshot list.
            _snapshots.Add(new Snapshot(position.x, position.y, position.z, rotation.y, rotation.z, rotation.w, _time);
            
            //Set the _currentTime to 0.
            _currentTime = 0.0f;
        }
    }
}

That should be all you need to make something that records ghost run of something.

In the final part of these tutorials we'll be coding up something to show a ghost that's been recorded.