[UNITY TUTORIAL] [BEGINNER] [COLLECTIBLES AND DOOR UNLOCK]
Welcome back! We are heading towards the end of this Tutorial series. In this tutorial, we will get to know how to collect the collectibles like Gems and Keys to perform some specific operations.
We will learn how to develop a method to make the level pass only if the Player has the key.
Steps:-
1. Open Unity and you can see the project in the state where we left before. Now let’s import the Collectibles Asset to the Box Bot Graphics Asset folder by drag and Drop.
2. Now change the Pixel Per Unit to 1000 and click on Apply for both the Collectibles that are gem and Key.
3. Drag and drop the Gem to the Scene view and place the Gem wherever you want. Do the same thing for the Key.
4. Let’s reduce the scale of both the Game objects to 0.8 in x and 0.8 in y for better visuals by selecting the Game Objects and changing the scale in transform. Also, add the Polygon Collider to the game objects to detect all the collisions and triggers.
5. Add a tag to the Gem Game Object and the Key Game Object to detect the collisions and triggers.
6. It’s time to add functionalities to the Game. Open up the Visual Studio by double Clicking the Movement Script. Add an OnTriggerEnter2D method to check the Player Trigger with the Game Object having a specified tag. Check for the Player Trigger with the Game Object Tag “gem” and “key”. And also assign an operation that will print a Debug Message in the console if the condition is true.
7. Now Save the script and head back to Unity. Check the Trigger Box present in the Polygon Collider 2D for both the Collectible Game Objects. Play and you can see the debug message is working perfectly. And Yes you have completed the Core Game Play! It’s time to polish a game little bit!
8. This time we will make lock the Finish line and make our Player win the Level only when he collects the key. Open the Visual Studio and declare a Reference to the Sprite renderer of the Finish Line. Also, assign the Colour Red to the Sprite Renderer at the Start Method.
9. Also delete the gameobject after the Player has taken the Collectibles by writing Destroy(collision.gameobject), where this line helps in determining the gameobject which has been triggered with the Player and henceforth destroys it. Next, change the Colour of the Sprite Renderer back to white if the Player got the Key.
Now check for the Condition in the OnCollisionEnter2D if the Player Collides with Finish Line. If the Player has the key then the Level will complete successfully or else it will ask to get the key. We will check for the color of the Finish Line, If it’s red then the player needs to collect the Key first and the Finish Line is locked but if the Player collects the key, the Finish Line changes it Colour to White, allowing the Player to complete the Level.
10. Check if the Player Collides with the tag “Exit” and the Finish Line is White, which means Player has already collected the key. Also, write if the Player hasn’t collected the key but has collided with the Finish Line then the message pops out asking the Player to Collect the key in order to complete the Level. Lastly, save the code and get back to Unity. Assign the Finish Line Sprite Renderer to the Movement Script.
You can see that if we don’t collect the key and go over the Finish Line, the Debug Message pops out asking for the Player to collect the key. But if the Player already collected the key then the finish Line changes its color to white so that the Player could complete the Level.
Congratulations! You have completed the basic steps to collect the Collectibles and learned how to interchange the Game Proceedings.
Project Files - Box Bot Tutorial Project Files
Graphics Assets - Box Bot Graphics Assets
Komentáre