Moving the player

The first feature to implement is the most vital one: moving the player.

Let's start by copying and pasting the code from Chapter 3, Let's Make Some Prefabs. In fact, we need to enforce both collider and rigidbody components, since we will use them to move the character (and also to detect collisions with bullets or enemies). Then, we actually need to move the character. You can refer to Chapter 3, Let's Make Some Prefabs, for a detailed explanation, but we are going to use, once again, the following equation:

The only difference is that we don't need to move the player along the y-axis, so the code within the FixedUpdate() function becomes shorter than one line. Also, we need to substitute the y variable in the last line of the function with transform.position.y.

The final code should look like the following:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
[RequireComponent(typeof(Rigidbody2D))] 
[RequireComponent(typeof(Collider2D))] 
public class PlayerController : MonoBehaviour { 
 
    public float speed = 10.0f; 
    private Rigidbody2D rigidBody; 
 
    // Use this for initialization 
    void Start() { 
        rigidBody = GetComponent<Rigidbody2D>(); 
    } 
 
    void FixedUpdate() { 
        //Get the new position of our character 
        var x = transform.position.x + Input.GetAxis("Horizontal") * Time.deltaTime * speed; 
 
        //Set the position of our character through the RigidBody2D component (since we are using physics) 
        rigidBody.MovePosition(new Vector2(x, transform.position.y)); 
    } 
} 

So far so good, the player is able to move left and right. However, the spaceships movement is unlimited along the x-axis. Therefore, we will need to use a wall to contain the player's movement. To begin we will need to add a variable. We assume that the main camera is centered (as in any 2D default scene). Thus, if the player is on x=0, then the spaceship is in the middle of the left and right sides of the screen. As a result, we are able to use just one variable to limit the motion both left and right symmetrically. We can add to our code the boundX variable, make it public (so designers can tweak it), and assign an initial value:

    public float boundX = 10.0f; 

Now, within the FixedUpdate() function, we need to use a math function to clamp the x value in case it becomes bigger than boundX or smaller than negative boundX.

A clamp function is a function that takes as input three parameters: a value, a min, and a max. If the value is in between the min and the max, the function returns (gives back) the value itself. Otherwise, the function returns max if the value is bigger than max; similarly, the function returns min if the value is less than min.

In the Unity Math Library, such a function is already implemented, so let's use it between getting the new x value and assign the new position to the rigidbody, like the following:

    void FixedUpdate() { 
        //Get the new position of our character 
        var x = transform.position.x + Input.GetAxis("Horizontal") * Time.deltaTime * speed; 
        //Clamp along x-value according to boundX variable 
        x = Mathf.Clamp(x, -boundX, boundX); 
        //Set the position of our character throught the RigidBody2D component (since we are using physics) 
        rigidBody.MovePosition(new Vector2(x, transform.position.y)); 
    } 

Save the script and test it to see if it works before moving on.

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

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