Switching to the first play State and playable scene

Chapter 9, Start Building a Game and Get the Basic Structure Running, finished with SetupState. Now we need to switch to the next State, PlayStateScene1_1. At the bottom of the SetupState screen is a button Click Here or Press 'P' to Play.

In SetupState, here's the code for that button:

Switching to the first play State and playable scene

An analysis of the code shown in the preceding screenshot is as follows:

Line 69: player.transform.position = new Vector3(50, .5f, 40);

  • Places Player near the center of the Terrain

Loading Scene1 using code

In Scene1, Player will use physics to hover and move. Here is the relevant code in PlayStateScene1_1:

Loading Scene1 using code

An analysis of the code shown in the preceding screenshot is as follows:

Line 15: if(Application.loadedLevelName != "Scene1")

  • We were in Scene0, so this condition is true, Scene1 is not loaded, therefore:

Line 16: Application.LoadLevel("Scene1");

  • Scene1 is loaded

Line 10: private GameObject player;

  • The variable player will store a GameObject which we'll find in line 18

Line 18: player = GameObject.Find("Player");

  • The Find() method is used to find the Player GameObject in the Hierarchy
  • A reference to the Player GameObject is assigned to the variable player
  • If Player isn't found, player will be storing null which will create an error in line 19

Line 19: player.rigidbody.isKinematic = false;

  • With a reference to Player, we can access its Rigidbody Component stored in the variable rigidbody
  • The variable isKinematic is assigned the value false
  • The Player GameObject is now affected by physics in PlayStateScene1_1 and PlayStateScene1_2
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.77.208