Game Development Reference
In-Depth Information
afresh back at home. This is simply because we are not tracking where the player left the
previous scene.
There are two ways to handle this: either we record where exactly everything is in every
scene and where the player enters and exits, or we can simply track the last known posi-
tion (or possibly a mixture of the two?).
For now, let us simply implement the last known position method. To do this, we are go-
ing to need a central place to remember everything about our game world (well, at least
the important bits we want to track), such as the player's stats, options and preferences
they have set, and where they have been in the world. Some of these will need saving for
the next time the player runs the game and some are just for the current instantiation, but
we will cover saving and loading later in Chapter 10 , The Battle Begins .
The settings we need don't have to be part of any scene, actively tracked in the scene, or
even interact with other game components. So we don't need a class that implements
MonoBehaviour or ScriptableObject; we do, however, need it to be around all the time
and not be reloaded in every scene. For this, we need a very simple static class (we
implemented one of these earlier in Chapter 6 , The Big Wild World , with Naviga-
tionManager ).
Create a new C# script in Assets\Scripts\Classes called GameState and popu-
late it with the following code:
using System.Collections.Generic;
using UnityEngine;
public static class GameState {
public static Player CurrentPlayer =
ScriptableObject.CreateInstance<Player>();
public static bool PlayerReturningHome;
public static Dictionary<string, Vector3>
LastScenePositions = new Dictionary<string, Vector3>();
}
Here, we have some simple static properties to:
• Track the player's stats
• A flag to note whether the player is running home away from a battle
Search WWH ::




Custom Search