官术网_书友最值得收藏!

Creating lives for the player

In this section, we are going to make it so that the player has a set number of lives. If and when the player collides with an enemy, the player will die, the scene will reset back to the start, and a life will be deducted from the player. When all the lives are gone, we will introduce the game over scene.

We will be working with the following scripts in this section:

  • GameManager
  • SceneManager
  • Player

Let's start by revisiting the GameManager script and setting up the capability of giving and taking the player's lives away:

  1. Open the GameManager script and enter the following code:
          public static int playerLives = 3;
        

At the top of the script, just after entering the class and inheritance, enter a static (meaning only one) integer type labeled playerLives, along with the value 3.

Next, we need to create a new method for our GameManager script that will ensure the player loses a life. After we make this new method, the Player script will call it when it makes contact with an enemy.

Let's continue with our GameManager script.

  1. To create the LifeLost method, enter the following code in our GameManager class:
           public void LifeLost()
          
{

We need this to be a public method so that it can be accessed from outside of the script. It's set to void, meaning nothing is returned from the method, and it's followed by the name of the method with empty brackets as it isn't taking any arguments.

  1. So, within the LifeLost() method, we will check the player's lives with an if statement with the following code:
    //lose life
if (playerLives >= 1)
{
playerLives--;
Debug.Log("Lives left: "+playerLives);
GetComponent<ScenesManager>().ResetScene();
}

After reviewing the if statement code we have entered, we will make a start by adding a comment to let ourselves or other developers know what this condition is doing (//lose life). We will then add the if statement condition checking whether the player has more than or equal to one life left. If the player does have one or more lives left, we will deduct the player's lives by 1 with the -- operator, which is just a quicker way of saying playerLives = playerLives - 1;

The line of code following on from the deduction of the player's lives isn't required, but it will notify us, in the Unity editor Console window, with an information box telling us how many lives the player has left (for debugging purposes), as in the following screenshot:

Following on from displaying how many lives the player has left in the Console window, we will refer to the ScenesManager script, which is attached to the GameManager game object. We can use GetComponent to access the ScenesManager script's ResetScene method, which will reset our scene back to the start.

  1. We will now enter the else condition, which indicates that the player has died:
    else
{
playerLives = 3;
GetComponent<ScenesManager>().GameOver();
}
}

If our player doesn't have any more lives left, that means theifstatement condition isn't met, so we can then offer anelsecondition. Within the scope of ourelsestatement, we reset our player's lives back to 3.

We then access theGameOver()method from theScenesManagerclass, which will take us from the scene we are on over to thegameOverscene.

Lastly, all that we need to do now is to make our Player script call the LifeLost method when the player has collided with the enemy or the enemy's bullets:

  1. Save theGameManager script.
  2. From the Project window, navigate to the Playerscript (Assets/Resources/Script).
  3. Scroll down to itsDiemethod.
  4. Starting from above the destroy line (Destroy(this.gameObject);), enter the following code:
          GameManager.Instance.LifeLost();
        

Note that we can call the GameManager script directly without finding the game object in the scene by using code such as GetComponent to acquire a script. This is the power of using the Singleton design pattern, calling directly to the LifeLost method.

  1. Save the Player script.
  2. Press Play in the Unity editor and collide with an enemy.

The level should reset with a message in the Console window showing that we have a particular number of lives left. Repeat this three more times. On the third life lost, our scene should have changed from testLevel to gameOver.

The following screenshot shows the Console window tab selected and logging the lives that are lost; also above the Console section is the Hierarchy window, showing that our game has gone from testLevel to the gameOver scene:

With minimal code, we have now made it so that our player has a number of lives. We have introduced a ScenesManager script into our game framework that talks directly to GameManager, regardless of restarting and changing scenes.

As a side note, you might have noticed that when we changed to the gameOver scene, our GameManager game object was carried over into the gameOver scene. If you recall back to the Adding a Singleton Design Patternsection, we set up theCheckGameManagerIsInTheScenemethod, which is called in theAwakefunction. This means that just because we are in a different scene, doesn't mean theAwakefunction is called again.

Remember, the Awake function will only run when the script is active and will only run once—even if the script is attached to a game object and is carried through scenes.

This is because our gameOver scene only carried the GameManager game object over to the gameOver scene. It wasn't activated, which means the Awake function wasn't called.

We have our basic lives and scene structure and have also used the Console window to help us acknowledge the changes.

Before we move on, you may notice that when the player dies, the lights get darker in the scene. The following screenshot shows what I mean:

As you can see in the previous screenshot, on the left is the scene we start with and on the right is the scene that is reset when the player has died. To fix this, we just need to make it so that we generate our lighting manually instead of it being auto-generated by Unity.

To prevent our lighting from going dark between scenes, we need to do the following:

  1. At the top of the Unity editor, click on Window | Lighting | Settings.
  2. The Lighting Settings window will appear. At the bottom of the window, un-check Auto Generate and click on the button next to it, Generate Lighting. Use the following screenshot for reference:
  1. This will take a minute as Unity will be setting up the new light settings. Once this is done, save the Unity project and that should fix it.

Note that we will likely need to set the lighting manually for other scenes, such as the other levels and the shop scene, later on in this book.

Let's now turn our focus to the enemy and add some functionality so that when it is destroyed by the player, we can add a score to ScoreManager, which is a new script that we will be making next.

主站蜘蛛池模板: 淮北市| 淮北市| 黄平县| 皋兰县| 彭泽县| 织金县| 安阳市| 南岸区| 丰城市| 三穗县| 苗栗县| 博客| 龙岩市| 普定县| 镇巴县| 蒙山县| 宜川县| 太原市| 东城区| 和静县| 深水埗区| 彭阳县| 贺兰县| 彭阳县| 石景山区| 阿克苏市| 青龙| 砚山县| 平江县| 松江区| 阳原县| 山丹县| 祁阳县| 融水| 大荔县| 玉门市| 安溪县| 阿鲁科尔沁旗| 长治市| 耒阳市| 河间市|