Unity Tutorial For Beginner (end)

Continuing my previous post, I created a function called addScore(), which adds points to the text on the screen. Today I create a new gameObject which is the space between the pipes and when the bird collides with that space, it call the addScore() function. Also create game over screen when the bird is collide with the pipes.

First go back to the Pipe gameObject and create a new gameObject called Middle (which is the space between the pipes).

And in the Middle gameObject, add Box Collider 2D and new script called “Pipe Middle Script”.

In the Box Collider 2D, make it is trigger, and change its size like below.

Because the addScore() function is in the LogicScript. So to can call that function, we need to connect 2 script together. In the PipeMiddleScript, create a LogicScript called logic and create a OnTriggerEnter2D() function. This function is check if something is collide with it, it will do something.

Go back to the Unity, move the logicScript into the Logic of the Middle gameObject. It will connect 2 scripts together.

Go to the Logic Manager and create a new tag called Logic, and change its tag to Logic.

In the Pipe Middle Script, logic is equal the gameObject with the tag in Logic. The 2 script is connected now, call the addScore() function in the OnTriggerEnter2D() function.

Now the score is added 1 when the bird goes through the pipe. To make sure the bird went through the pipes, putting the bird on a layer. And check if the colliding object was on that layer and add the score. In the Bird gameObject, make a new layer called Bird and change its layer to Bird layer.

Go back to the Pipe Middle Script, write an if statement to check if the colliding object was on the layer 3, call the addScore() function.

We can also create the function of having an argument like python. Go to the addScore() function in LogicScript, add one argument called scoreToAdd with integer data type. And instead playerScore add 1, add it with scoreToAdd.

Then in the Pipe Middle Script, we can write a 1 in the parentheses after the addScore() function. It will add 1 point to the player’s score, or if the number in the parentheses is 5, it will add 5 points to the player’s score.

Next is to create a Game Over screen. In the Canvas gameObject, create a new gameObject called Game Over Screen.

Then create the Text and Button inside the Game Over Screen gameObject. And change its size and the text to “Game Over “.

Next is create a text in the button and change its to “Play Again”.

In the LogicScript, import the SceneManagement of UnityEngine. Create a function called restartGame(), it will restart the game when we call it.

Go back the Button and scroll up to see the on click. In the on click, move the Logic Manager to it and call the restartGame() function of LogicScript.

The game will restart after we click the play again button.

Next is to make the game over screen just pops up when the bird is colliding with the pipes. First uncheck the Game Over Screen, like we hide it and it will work until we call it.

Then in the LogicScript, create gameOverScreen with GameObject data type. Create a new function called gameOver(). And in this function, set the active of the gameOverScreen is true.

And go back to the Unity, move the GameOverScreen gameObject to the Game Over Screen of Logic Manager.

Like we just did with the addScore() function. In the BirdScript, create a logic with LogicScript data type. And logic is equal to the gameObject with Logic tag. And create a new function that checks if the bird is colliding with another object, call the gameOver() function.

Like you can see above, there was an error that we can play when the game over screen pops up. To fix this error, just create a BirdIsAlive with boolean data type is true. And in the function make the bird jump, write one more command that to check if the BirdIsAlive in true, then make it can jump. And when the bird is colliding with another object, the BirdIsAlive is false.

Now our Flappy Bird is done. The tutorial that I was followed to learn the basics of the Unity was from Game Maker’s Toolkit.

Tags:

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *