[Unity project combat] Hands-on teaching: Flying bird (5) background scrolling

        To undertake the previous article: [Unity Project Combat] Hand-in-hand teaching: Flying bird (4) text added , we have made the protagonist bird jump to the Game Over state after touching the ground, and then we will continue to explain the score mechanism.

1. Re-enter the game

        According to the last description of the previous article, if our bird falls on the grass, it will be judged as a game failure immediately, and the "GameOver" text will be played, the bird "DIE" animation will be played, and the mouse will not be able to fly again after clicking the mouse. And we can regard clicking the mouse as re-entering the game, and the implementation method is also very simple. We only need to detect a touch of the left mouse button when the game is over, and the game will start automatically. We return to the GameControl script and add the following code snippet:

         After completing the filling, we save it, jump to the main page of unity, and you can see that after the failure, click the mouse again, and the game will start automatically.

2. Background scrolling

        Next, we want the character bird to fly to the right to avoid obstacles. Although it is flying, in fact our protagonist bird is still, and what is really moving is the background. We control the bird to fly continuously , the background is constantly scrolling, making us look like a bird is moving.

        To this end, we need to add a rigid body and a control script to the background image to control the background image to move slowly to the left at a certain speed. First, we add a 2D rigid body under the control of the background image, so that we can control the background image:

         After adding the rigid body, we also need to change the material to "Kinematic", so that our background image will not fall down due to the influence of physical gravity. Next, we come to the "GameControl" script, because we need a speed to move the background image to the left, and we define this variable in the singleton mode under the "GameControl" script, so that this variable can be singled out And globalization (if you don't understand what a singleton mode is, you can check my last article: [Unity Project Combat] Hands-on Teaching: Flying Birds (4) Text Add ):

        After adding the speed variable, we create a new script named "ScrollingObject" under the background map control:

        Double-click the mark on the figure to enter the script, and add the following code:

         Next, we need to judge that when the game fails, the background image will also stop moving, so add the following content under the Update function:

         Let's save it, go back to the main interface of unity and run the game, you can observe that the background image is moving to the left, and the background image will stop moving after the bird touches the grass, but a new problem comes again: the background image will Gradually away from our field of vision, leaving only an empty scene and our little bird flying in the end! So the effect we want to achieve should be that when the background image is about to fly out of the field of view, a new background image will be regenerated on the right ! In this way, our bird will look like it is always flying in the background image. To do this, what we need is an identical background control, copy and paste the background image:

        Then stitch the two background images together:

        The next thing we need to do is to use the script to make these two background images appear alternately . When the bird is in the red frame background image, the blue frame background image is on standby on the right. When the bird enters the blue frame background image, the red frame The background image of the frame will immediately run to the right to stand by, and so on. So we need to add a script responsible for running the background image back and forth:

        And create a script called "RepeatingBackGround":

        After double-clicking to open, initialize the following parameters first:

         Then we write the background image offset function so that the two background images can keep jumping to the right and wait for the bird to fly over:

        Finally, we judge in real time in the Update function. When the bird flies to the edge of the background image, we immediately call the background image offset function to move the background image to the right, so that there is no empty background in the bird's field of vision:

        If you don't know what I'm talking about, it doesn't matter. After we save it, go back to the main page of unity and run the game, and you will understand. In the next article ( [Unity Project Combat] Hands-on Teaching: Flying Birds (6) Add Obstacles ), we will add obstacles to add gameplay to the game. 

Guess you like

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