Time for action – rotating Player in SetupState

There will be a setting in the Inspector panel to set the rotating speed. Why is Player rotating in SetupState? It allows you to view the changes made to Player. It's also a nice visual effect, much better than just looking at a static screen.

  1. Create a script containing the code shown in the following screenshot:
    Time for action – rotating Player in SetupState
  2. Create a C# Script and name it PlayerControl.
  3. Open it in MonoDevelop.
  4. Add the setupSpinSpeed variable as shown on line 6 of the preceding screenshot.
  5. Modify Update() as shown on line 13 of the preceding screenshot.
  6. Save the file.
  7. Attach this script to the Player GameObject.

To control the rotation of Player, we need to add the code (shown in the following screenshot) to SetupState.

Time for action – rotating Player in SetupState

The SetupState class needs to have two pieces of information to be able to control Player.

  • It needs to know about the Player GameObject
  • It needs to know about the PlayerControl Component script attached to Player

Therefore we need two variables to store this information in SetupState. Then it's just a matter of using Dot Syntax to control the Player.

  1. Add a variable named player as shown on line 10.
  2. Add a variable named controller as shown on line 11.
  3. In the SetupState() constructor method, add lines 19 and 20.
  4. In the StateUpdate() method, add lines 25 and 26.

What just happened?

Let us analyze the code shown in the preceding screenshot:

In PlayerControl:

Line 6: public float setupSpinSpeed = 50.0f;

  • This variable is public so it will appear in the Inspector
  • It is set for a rotation speed of 50 degrees per second. Why?Just seems to be a nice speed visually

Line 13: public void PlayerUpdate ()

  • This method needs to be public because it will be called from a State's StateUpdate() method
  • Since the State Machine is controlling what code is invoked and when, PlayerControl won't be using the usual Update() method

In SetupState:

Line 10: private GameObject player;

  • The variable player is going to store a reference to the Player GameObject
  • It's private because only SetupState needs to see this variable

Line 11: private PlayerControl controller;

  • The variable controller is going to store a reference to the PlayerControl Component object that's attached to Player

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

  • GameObject.Find() is the method used to find the Player GameObject
  • When it finds the Player GameObject in the memory, a reference to it is stored in the player variable

Line 20: controller = player.GetComponent<PlayerControl>();

  • The GetComponent() method gets the PlayerControl script Component that's attached to Player and stores a reference of PlayerControl in the controller variable
  • We now have access to any public variables and methods in the PlayerControl script

Line 26: controller.transform.Rotate(0, controller.setupSpinSpeed * Time.deltaTime, 0);

  • The Rotate() method causes the Player GameObject to spin
  • Rotate() is a method available in the Transform class and it takes a Vector3 parameter
  • This means it needs to know the number of degrees of rotation for each axis: x, y, and z
  • transform is a variable that stores a reference to the PlayerControl Transform Component object in memory
  • The x and z axes are 0 because we only want Player to spin around the vertical y axis
  • The controller.setupSpinSpeed statement retrieves the value stored, 50.0, in the setupSpinSpeed variable of the PlayerControl script
  • This is multiplied by Time.deltaTime to give a rotation speed of 50 degrees per second

Line 25: if(!Input.GetButton("Jump"))

  • This if statement checks to see if the Space bar is not being pressed
  • Notice the NOT operator being used (exclamation point)
  • When the Space bar is not being pressed, line 26 is executed
  • Open the InputManager from the menu by selecting Edit | Project Settings | Input
  • Expand Jump and you will see that Jump is mapped to the Space bar

Adding the Player Color option

While Player is spinning, we are going to create some buttons to change its color. For my Player, this will change the body color. If you're using a Cube as a Player GameObject, the whole Cube will change color.

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

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