Game Development Reference
In-Depth Information
Managing the Gnomatic Garden Defender
The Gnomatic Garden Defender has two values that must be stored: its position (a Vector3
construct), and its y rotation (a simple float). The Level Manager will require the original transform at
the start, but it will also want the Gnomatic Garden Defender's updated position right before the level
changes from a garden level to a menu level. The code to go from GardenLevel1 to the MainMenu
level is in the GameMisc script, so that will be a good place to handle the data.
1.
Open the GardenLevel1 scene.
2.
Open the GameMisc script, and add the following variables:
LevelManager levelManager; // the script that holds the data between levels
public Transform gnomeTransform; // the gnome's transform
In the Start function, add the following:
3.
if(GameObject.Find("Level Manager")) {
levelManager = GameObject.Find("Level Manager").GetComponent<LevelManager>();
// if this is a new game, send the initial data
if(levelManager.gameState == 0) LevelPrep ();
}
As before, the Level Manager can be found only after the level has loaded, which is a drawback of
using DontDestroyOnLoad . By checking first to see if it has been found, you can avoid errors when
testing the level directly. The call to the LevelPrep function is where data will be gathered and sent to
the LevelManager script.
Create the LevelPrep function:
4.
void LevelPrep () {
if(levelManager) {
// send the gnome's transform off to be saved if the levelManager has
been found
Transform temptrans = gnomeTransform;
levelManager.gnomePos = temptrans.position;
levelManager.gnomeYRot = temptrans.localEulerAngles.y;
}
}
5.
Do not save the script yet.
If you are a chess player and are good at thinking things through, you may have spotted a bit of a
conflict between the code you added to the GameMisc script and the SensorDoors script. The first
time the player enters GardenLevel1, the gameState will be 0. If the GameMisc script is executed
first, LevelPrep() will be called. If the SensorDoors script is executed first, the state will have been
changed to 1 before the GameMisc script is executed and the LevelPrep() function will not be
called. To make sure the GameMisc is executed first, you will specify the execution order between
the two scripts.
 
Search WWH ::




Custom Search