Moongate Games UK

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

Tutorial: Ghosts #1 - Setup and Snapshots

Introduction:

One of the features boasted in many recent racing games is Ghosts, especially for game modes such as Time Trials. I remember playing Diddy Kong Racing on the N64 and playing also trying to beat my own or my brothers times.

So enough about my gaming habits lets get on with this tutorial.

This will be a three part from setting up what's needed with explanations etc. Going on to coding the recorder and finally coding a rough player to play the ghosts.

Setup:

First things first we'll need to setup something to hold each piece of data for the ghost, depending on the complexity of what you're recording it can be a lot. But for this we'll basic be recording the Location and Rotation of an object along with the Time.

So firstly we'd need something like this:

namespace Ghosts{
    public class Snapshot{
    
    }
}

This will be what holds each snapshot so to say. We need to now add variables to store the data. In this case we'll be storing Location, Rotation and Time, so for that we could use the Vector3 and Quaternion, but i don't really want this to depend on just Unity.

So we're going to split them out for this like the following:

//Location Data
public float XLocation {get; private set;}
public float YLocation {get; private set;}
public float ZLocation {get; private set;}

//Rotation Data
public float XRotation {get; private set;}
public float YRotation {get; private set;}
public float ZRotation {get; private set;}
public float WRotation {get; private set;}
        
//Time Data
public float Time {get; private set;}

So by looking at the above code so far, it's pretty easy to see where we're going with this. But as you may have noticed I'm not using a straight up variable I'm also using a getter and setter. These help when you have things that you want to set within the class (or when an object is created.) but don't want direct access. So all of the variables in this can only be set within the Snapshot class but they are visible to get from anywhere.

At the moment it looks like we can't set the data in a snapshot so for this we'll need to make a constructor to pass in all the information we need.

To do that we just need to add the following:

public Snapshot(float xLoc, float yLoc, float zLoc, float xRot, float yRot, float zRot, float wRot, float time){
    //Store Location Data
    XLocation = xLoc;
    YLocation = yLoc;
    ZLocation = zLoc;
    
    //Store the Rotation Data
    XRotation = xRot;
    YRotation = yRot;
    ZRotation = zRot;
    WRotation = wRot;
    
    //Store the Time
    Time = time;
}

So once you have all of that you should have a class that looks like the following:

namespace Ghosts{
    public class Snapshot{
        //Location Data
        public float XLocation {get; private set;}
        public float YLocation {get; private set;}
        public float ZLocation {get; private set;}

        //Rotation Data
        public float XRotation {get; private set;}
        public float YRotation {get; private set;}
        public float ZRotation {get; private set;}
        public float WRotation {get; private set;}

        //Time Data
        public float Time {get; private set;}

        //Constructor.
        public Snapshot(float xLoc, float yLoc, float zLoc, float xRot, float yRot, float zRot, float wRot, float time){
            //Store the Location Data.
            XLocation = xLoc;
            YLocation = yLoc;
            ZLocation = zLoc;

            //Store the Rotation Data
            XRotation = xRot;
            YRotation = yRot;
            ZRotation = zRot;
            WRotation = wRot;

            //Store the Time
            Time = time;
        }
    }
}

So as you can see this is the basic information you would need to store to make a basic Ghost system. The key part to this is the time since you will need to get the time that the snapshot was recorded to place the ghost in the correct position at the correct time.

In the next part of this tutorial we'll code a recorder in unity for the Ghost System for more updates and tutorials follow any of the social media pages at the bottom of this page.

Part 2 -->