Time for action – adding GameObjects and a button to the Scene

Add a Plane GameObject and a Cube GameObject to the Scene panel. Add a Rigidbody Component to the Cube GameObject so it will fall to the Plane GameObject. Also add a Directional Light to the Scene panel if there isn't one already. Then we'll add a GUI button to switch to the PlayState object. To do so, perform the following steps:

  1. In the menu, navigate to GameObject | Create Other | Plane.
  2. In the Inspector panel, make sure the Position is 0,0,0 for X,Y and Z.
  3. In the menu, navigate to GameObject | Create Other | Cube.
  4. Set the values in the Inspector panel as shown in the next screenshot.
  5. Select Cube in the Hierarchy panel.
  6. In the menu, navigate to Component | Physics | Rigidbody.
  7. In the menu, navigate to GameObject | Create Other | Directional Light if needed.
    Time for action – adding GameObjects and a button to the Scene
  8. Add the following code to the code block of the ShowIt() method of BeginState:
      if (GUI.Button(new Rect(10, 10, 150, 100), "Press to Play"))
        {
          manager.SwitchState (new PlayState (manager));
        }

What just happened?

We now have a little bit of action in the Scene. When you click on Play, the following screenshot will be shown:

What just happened?

Then the Cube GameObject falls to the ground, Plane, as shown in the following screenshot:

What just happened?

Suppose your game had many things moving around, and instead of a button, you had a fullscreen graphic. You will not see any of that movement, nor will you really want all that action taking place before you switch to the PlayState class.

Pausing the game Scene

There is a little trick you can use to stop all the game activity while in BeginState. Unity has a Time class and a timeScale variable, which you can set to slow down your game or stop it completely. It's similar to a pause feature.

..................Content has been hidden....................

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