Game Development Reference
In-Depth Information
3.
In the GardenLevel1 scene, assign the Gnomatic Garden Defender to the
new parameter in the Game Manager's Game Misc component.
4.
Save the scene.
Next, you will create the script that uses the stored data to put the Gnomatic Garden Defender into
its previous position when the player returns to the game.
1.
Create a new C# script in the Game Scripts folder, and name it SetTransform .
2.
Add the following function:
public void UpdateTransform (Vector3 newPos, float newYRot) {
// set the new position
Vector3 tempPos = new Vector3(transform.position.x,
transform.position.y,transform.position.z);
tempPos = newPos;
transform.position = tempPos;
// set the new y rotation
Vector3 tempRot = new Vector3(transform.localEulerAngles.x,
transform.localEulerAngles.y,transform.localEulerAngles.z);
tempRot.y = newYRot;
transform.localEulerAngles = tempRot;
}
3.
Save the script.
4.
Add it to the Gnomatic Garden Defender's prefab in the Project view.
5.
While you are there, rename the prefab Gnomatic Garden Defender to
match the updated name in the Hierarchy view.
Finally, in the LevelManager script, you will tell the gnome's data to be updated.
In the LevelManager's Update function, below the print statement, add
6.
ManageLevels (); // do stuff according to the level just entered
Create the ManageLevels function:
7.
void ManageLevels () {
if (currentLevel > 3) { // you are in a garden level
// reposition & re-orient gnome
GameObject.Find("Gnomatic Garden Defender").GetComponent<SetTransform>()
.UpdateTransform(gnomePos,gnomeYRot);
if (gameState == 1) return; // still in staging area, just repopulate a new game
}
}
8.
Save the script, and save the scene.
 
Search WWH ::




Custom Search