[Unity project combat] Hands-on teaching: Flying bird (4) text addition

        According to the previous article: [Unity project combat] Hands-on teaching: Flying bird (3) animation production , we have connected the flying and death animation of the bird with the normal screen through the trigger, the next step is to pass the bird Scripting and writing, so that the bird can fly according to our mouse click.

1. Modify the bird script

        Let's go back to the bird script written in Chapter 2:

        Add the following code to the script:

        (Note that the "die" and "fly" in the code on the picture are the names of my triggers. The name must be written correctly, otherwise the trigger cannot be triggered. The name of the trigger is mentioned in Chapter 3 Animation Production, so I won’t repeat it here .)

        So far, we click the play button, and click our mouse on the debugging page, we will find that the bird plays according to the animation we set in advance.

2. Text settings

     We right-click to add a text in the property window:

         

        After the addition is complete, you will find that there will be an additional "Text" UI in the Canvas directory

        Two texts are added in the example, namely "ScoreText" for recording scores and "GameOverText" for prompting the end of the game. The name of the text can be modified by double-clicking the text UI:

         We may not find the newly added text in the debug window, at this time, we can press the "F" key in the debug window to display all the text

        Then we can also adjust the properties of the text in the property bar of the text

        According to the adjustment effect, we can adjust the text to the appropriate position of the game window

        Because the text "GAMEOVER" is not displayed at the beginning, but after the bird touches the ground to trigger the "death" condition, we need to gray out the text at the beginning and control it in the script

3. Add control script

         After adding the text, we need to add a control script, which is responsible for a series of game control operations such as managing the bird score, displaying the game over message, restarting the level, etc. First, create an empty game object and rename it to "GameController":

        Add a new script to the newly created empty object, name it "GameControl", double-click to open the script, and start writing code

        We first consider the game logic of the game failure. When the bird touches the grass, it should be judged as the game failure, and the "GameOver" text will be played, the bird "DIE" animation will be played, and the mouse will not be able to fly again. The above logic processing Judging game failure and playing "GameOver" text, others have been implemented in the Brids script, so we now complete the above control in the new script, the code is as follows:

         After declaring the game failure logic, we still need to modify the Start function to the Awake function

        And singleton the class in the Awake function:

        In the next step, we switch back to the script of the bird. In the script of the bird, the function of monitoring whether the bird touches the grass (for details, please refer to the second section: [ Unity Project Combat] Hands-on Teaching: Flying Bird (2 ) scripting ) to add the game failure function:

        At this point, when our bird falls on the grass, it will be judged as a game failure immediately, and the "GameOver" text will be played, and the bird "DIE" animation will be played. Clicking the mouse can no longer fly. We can save the script and click to run the game to test whether met expectations.

        The next chapter ( [Unity Project Actual Combat] Hands-on Teaching: Flying Birds (5) Background Scrolling ) will continue to explain complex logic such as scoring mechanism and background cycle switching.

Guess you like

Origin blog.csdn.net/qq_41884002/article/details/127329957