In this second part of our comprehensive tutorial series, we'll dive deeper into creating a hyper casual game for Android using Unity. If you haven't already, be sure to complete Part 1 before proceeding.
Source Code Files
To follow along with this tutorial, download the source code files here. All project files are located in the main folder, while additional intermediate files used during the process of creating game sprites can be found in the asset folder. This tutorial was created using Unity version 2018.2.13f1.
Let Us Know
If you encounter any errors or issues throughout this tutorial, please let us know by completing this form and we'll look into it!
Adding a Prefab Folder to the Project
To begin, create a new folder in your project and name it "Prefabs." This folder will contain game objects that we'll instantiate later on in the tutorial.
Obstacle Creation
Start by creating the first obstacle – a square. Create an empty GameObject and reset its transform component after creation. Rename this GameObject to "SquareObstacle." Next, create a square sprite to use as an obstacle for the player to avoid during gameplay. Right-click in the Sprites folder and choose "Create" > "Sprites" > "Square." Make the Square a child of the "SquareObstacle" and add a 2D box Collider component to it. Set the Is Trigger checkbox to true on the square.
Tagging the Obstacle
Create a new tag called "Obstacle" and tag the Square as "Obstacle."
Adding Collision Detection
Open up the Player script and add the OnTriggerEnter2D function to detect when the player collides with this square obstacle. Add this function under the PlayerInput() function already in the script. Set up a Debug statement inside this function so that we can test out the collision detection and ensure the player is indeed entering the trigger of the Square obstacle.
Game Manager Creation and Setup
Create another empty GameObject and add it to the scene, resetting its transform component and naming it "Game Manager." Create and add a new script called "GameManager" to the Game Manager object, moving this script into the Scripts folder in the project. Open the Player script and get a reference to our GameManager script by creating a GameManager variable named gameManagerObject.
Creating a Public "GameOver" Function
Open the GameManager script and create a public "GameOver" function. This function needs to be public so that we can access it from other scripts. Add a Debug statement to this function, stating "Game Over!" Save the script.
Implementing Player Death
Create a private PlayerDeath function in the Player script. Call the GameOver function inside this function. We'll then call the PlayerDeath function inside the OnTriggerEnter2D function; essentially, whenever our player GameObject collides with an obstacle, this will cause the GameOver function to fire.
Creating a Player Death Effect
Create an Empty GameObject and rename it to "PlayerDeathEffect." Reset its transform component in the inspector. Click on the PlayerDeathEffect object in the Hierarchy and add a particle system to it. The Particle System should now be a child of the PlayerDeathEffect game object. Adjust some aspects of the newly created particle system, including changing the Material on the Renderer component to Sprites Default.
Note: Throughout this tutorial series, we'll continue to explore the world of mobile game development and hyper casual games for Android using Unity. Stay tuned for Part 3!