Requirements of the player controller

Let's start by defining the requirements for this player controller.

First of all, we would like to lock the movement to only left and right; we don't want the player going up and down. Of course, this is the choice we made when we designed the game earlier in the chapter. Another consideration is that the left and right movements are limited; the player cannot go off screen. In Angel Cakes we have solved this problem by placing walls, so we delegated to the physics system the fact that the player cannot go beyond certain boundaries. In space, there are no walls, and even if we could place invisible ones, for the sake of learning something new, we will see how to limit the movement through the script.

Differently from Angel Cakes, the player here can shoot. In particular, the player can shoot straight from the player's location. In our implementation, this means that the player controller has to instantiate a bullet. However, we also need to limit this process, since we don't want the player to shoot billions of projectiles per second, but rather have a reload time before shooting the next bullet. As such, the player controller is also responsible for making this check.

Lastly, the spaceship can take damage, so we need to show the animation of the explosion where the enemy's bullet has hit the player. Again, the player controller has to deal with it. Actually, not only should it show the explosion, but it should also play a sound and decrease the number of lives by one. However, for these two things, we need to wait until Chapter 7, Getting Serious About Gameplay, in which we will implement the UI, but it was worth mentioning it here since we are designing our player controller.

Now that we understand the requirements for our player controller, let's implement it. If you remember in Chapter 3, Let's Make Some Prefabs, we saw the script piece by piece. Here, to change the approach, we will see all its functionalities one by one.

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

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