I am working on temple run like game using this Kit. Unity Asset Store - The Best Assets for Game Making. I want to insert a room with two doors , when my player enters in the room it stops running and user can control with arrow keys , and when it leaves the room by back door it starts running again . what should i do when it collides with the door collides ? I am doing this by replacing the scenes. I am instantiating an empty Game-object prefab randomly in GamePlayScene when player is colliding with that i am loading HouseScene and when it is colliding with back door (in HouseScene) i am loading GamePlayScene. but the game is starting from the beginning. how can I resume the game From where I left And keep the track of Distance covered and coins collected? And also for HouseScene . Remember the points i achieved in it. Thanks.
There are two ways to go about with this one.
First is to either create a singleton or simply just have a game controller i.e. a game object where you call DontDestroyOnLoad, to keep track of progress. Instead of calling the Application.LoadLevel(“…”) you should add a method to the game controller called LoadLevel(string level), that first save your current position in the current level in a dictionary: positions.Add(Application.loadedLevelName, player.transform.position) assuming positions is a Dictionary<string, Vector3>. Then load the new level.
When you enter any level you should then always check if the dictionary contains any previous positions for the newly loaded level.
The second method is to never unload the main level, but instead use Application.LoadLevelAdditive(“…”). To do this you should:
- pause the running part of the game
- load the room additive some where away from the runner
- make sure the camera in the new room is set to be the main camera
- wait for player to finish the room
- destroy the room - including its camera
- resume the running
To do this you should create the room with one single parent, containing everything i.e. the new camera, the temporary player character, all walls, all collectibles and etc. This is to make it easier to destroy the room again, as you simply have to call Destroy(gameObject) on the one parent node.
You can create a singleton object to keep track of data that must precast across scenes, goes into save files and through the network.
public class GameSingleton {
private static GameSingleton instance = null;
public static GameSingleton Instance {
get {
if(null == instance) {
instance = new GameSingleton();
}
return instance;
}
}
/// ... Your data and functions
}
This way you are never dependent on scenes or other in game objects to init and hold critical data.
Access the singleton like so:
GameSingleton.Instance.getPoints();
You will use this data when changing scenes (init objects) and when saving/sending data across the network.