Player Controller

In the previous section, we implemented a script to move the character. However, if we hit Play, the character won't move, aside from idling. The reason lies in the fact that we have two functions to move the character, but no script calls them. If you go back to the Control Scheme section, you see that we need a Player Controller (or a UI interface in case of mobile games, but we will explore that later in Chapter 12, The Nature of Export), which sends the input to the Player. In this section, we are going to implement that script.

Create a new C# script and name it PlayerController. The script per se is very simple; however, we want to compile it only if the game is running either in the Editor or on a standalone version of the game (such as on Windows or macOS). As a result, we need to use compiler directives. So, after the name of the class, let's open the Compile Directive, which will compile all the code we are going to write if, and only if, we are running either in the Editor or in a standalone version of the game:

public class PlayerController : MonoBehaviour {
#if UNITY_STANDALONE || UNITY_EDITOR
//[...]
}

Now, it's time to think what the script should do. First of all, we need to retrieve the MovementComponent attached to the character. To do so, let's create a new variable of the type MovementComponent:

   MovementComponent movementComponent;

Then, in order to initialize the variable, we can set its value in the Awake() function. We can achieve that by using GetComponent() to retrieve the MovementComponent. Furthermore, we need to launch a warning in case the MovementComponent is missing, and remove this very script from the Panda Hero:

   void Awake () {
//Retrieve reference to the Movement Component
movementComponent = GetComponent<MovementComponent>();

//Disable this script in case the Movement Component is not found,
and leave an error message.
if(movementComponent == null) {
Debug.LogError("Missing MovementComponent on "
+ gameObject.name + " to run PlayerController.
Please add one.");
Destroy(this);
}
}

Next, we need to send the input to the MovementComponent. If you recall, the character needs to move left and right as well as jump. For the first two, we can just gather the value of the Horizontal axis value and use it as a normalized speed for the MoveCharacter() function. As a result, we will be able to use the arrows on our keyboard to move the character. As for the jump, we can use the up arrow to make the character jump. This is great, but we need to keep in mind that the MovementComponent uses Rigidbody2D. Hence, as we have seen for the previous projects, we need to use the LateUpdate() function. Here is how the code should be:

   void FixedUpdate() {
//Send input to the Movement Component in order to move the character
movementComponent.MoveCharacter(Input.GetAxis("Horizontal"));

//Check if the Up Arrow has been pressed, and if so,
send the Jump input to the Movement Component
if (Input.GetKeyDown(KeyCode.UpArrow)) {
movementComponent.Jump();
}
}

Finally, we need to close the compiler directive by closing the if-statement, shown as follows:

public class PlayerController : MonoBehaviour {

#if UNITY_STANDALONE || UNITY_EDITOR
//[...]
#endif
}

Save the script and attach it to the Panda Hero. There are no settings to set here, so we are good to go.

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

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