[Unity project combat] Hands-on teaching: Flying birds (6) Add obstacles

        To undertake the previous article: [Unity project combat] Hands-on teaching: Flying bird (5) background scrolling , we have made the protagonist move the background image while standing still, so that the protagonist bird seems to move forward by itself Same, then we will continue to explain how to add obstacles.

1. Add obstacle columns

        Drag the obstacle material (pull two) to the main interface, and then adjust the positions of these two obstacles:

        The positions of my two obstacles are as follows:

        The post position parameters below are as follows:

        The post position parameters above are as follows:

        Of course, you can also adjust the gap between the pillars yourself, so as not to be too narrow to make it difficult for birds to pass, or too wide to be challenging. After adjusting the position, add a collision detector to the two pillars (the green edge is the detection range), to detect if the bird hits the pillar:

         The range of the collider is also set according to your own preferences. Note that the range should be moderate. Collision detectors must be added to both columns. Here is an example:

         After the addition is complete, create a new empty object (named Columns), and pull all the two column obstacles into this object as a whole. In this way, a simple obstacle is completed.

        It should be noted that the empty object also has its own coordinates, so before pulling the pillar obstacle in, you need to change all the uppermost tables of the empty object to 0, and then pull the pillar obstacle in, otherwise the position will shift! ! (You can try not to change the coordinate position of the empty object, pull in the pillar obstacle and then look at the coordinates of the two pillars)

2. Add scoring detector

         Because the score needs to be calculated after the bird safely passes between the two pillars, it is necessary to add a collider between the two pillars to detect whether the bird has passed the pillar. We directly select the Columns object and enter it in the configuration Add a box collider (set as trigger):

        The position and size are according to your own preferences, and my settings are as above. After the setting is complete, in order to test the rationality of the column, we can drag the column to the right, and then click Run to see the reaction of the column:

         When the game is running, you will find that the column and the bird are "stationary" in the current interface, so we also need to add a physical property to the Columns object to make the column "moving" for the bird:

        Then add a fixed script:

 3. Obstacle script writing

        First of all, let's write the game scoring function. Whenever the bird crosses the pillar, the game control should automatically add one point, and update the score in the "SCORE" text below, so we first add in the previous script "GameControl" A score function, create a new global variable to calculate the score:

         Then we add a judgment, once the game is over, do not calculate the score, otherwise the score will continue to be calculated:

        In the next step, because we want to update the score of the interface in time, we need to use Unity's UI library and define a global variable to update the interface text:

         After the scoring function is completed, we have to let the pillar object judge. If the bird passes the pillar, it will call the scoring function here, so we need to create a new script under the Columns object (named columns):

        In the script, when the collider detects that a bird rigid body touches the trigger, it immediately calls the score function to update the interface score:

         Next, we save the script, and switch back to the main interface of unity, drag and drop the score text on the main interface into the GameControl script to form an internal connection, and bind the SoreText global variable in the GameControl script to the score text on the main interface:

         After everything is done, let's save it and click to run directly, and you will find that after the bird passes the pillar, the score is really updated by one point! congratulations! You have completed 80% of the entire project. In the last chapter [Unity Project Combat] Hands-on Teaching: Flying Birds (7) Obstacle Object Pool , I will explain the last point, which is also the most complicated part. , is how to make the pillar obstacles randomly refresh infinitely and destroy the objects.

Supongo que te gusta

Origin blog.csdn.net/qq_41884002/article/details/130564793
Recomendado
Clasificación