How I Built A 2D Platformer Using Unity3D game engine

Written by anshuman-pattnaik | Published 2020/05/10
Tech Story Tags: unity3d | game-development | indiedev | android-app-development | mobile-game-development | webgl | javascript | html5

TLDR Forest Assassin is a 2D adventure platformer game with easy controls and fun gameplay. To complete the game collect all the coins and the trophy try to collect all 100 gold coins to become the highest scorer. The game is available at https://games.hackbotone.com/forest-assassin/ and it supports all the modern desktop browsers. To move a character in the game you need to have a script which will control the character to Walk, Jump, Crawl and so on and also it requires physics. To control the physics Unity provides a component called Rigidbody which handles lots of fundamental physics like force and acceleration.via the TL;DR App

Forest Assassin is a 2D adventure platformer game with easy controls and fun gameplay, which helps every one to get started, this is a classic platformer game with wonderfully designed characters and world.

To complete the game collect all the coins and the trophy try to collect all 100 gold coins to become the highest scorer in this adventurers platformer game, on your way you will face many obstacles and enemies and use your sword to fight with the enemy.
How to play the game on desktop browser?
To make it easier for the user I have also published as a desktop browser game because people always love to play browser games and the reason is its quick easy no need for any types of installation.
Currently, the game is available at https://games.hackbotone.com/forest-assassin/ and it supports all the modern desktop browsers.
1. To move use [ A ] [ D ] or arrow keys
2. Use [ SPACE ] key to jump
3. Use [ W ] key to attack
4. To finish the game collect all coins and the trophy
How to play the game on Android device?
Forest Assassin is also available for Android devices and on the smartphone user will get the feelings of a classic platformer game. So please download the game and share your experience.
https://play.google.com/store/apps/details?id=com.forestassassin
1. To move use [ LEFT ] & [ RIGHT ] arrow button

2. Use [ UP ] arrow button to jump

3. Use [ SWORD ] button to attack

4. To finish the game collect all coins and the trophy
How to Build a 2D platformer game using Unity3D?
Unity3D game engine is a great platform to build a 2D or 3D game because it provides many major components such as physics, animation, cross-platform support and many more. And before starting to build any types of game the very first thing you need to do is design a Game Theory which means you need to build the idea behind the game which will help you to develop a successful game and always try to remember while designing a Game theory make it simpler and don’t make it complicated otherwise it will be quite difficult for you to finish the game.
If we see an example of a legendary T-Rex Game which you often play on Google chrome browser when you don’t have an internet connection and this game is the best example of a 2D platformer game and if you look at the game then you noticed the game is very simple and easy to play all you need to do is press the SPACE bar on the keyboard to avoid the obstacles and earn points.
The similar approach I also follow while designing this game at the very beginning my goal was to build a simple 2D platformer game so that any types of user can play and have fun and also it should help me while building this game.
Forest Assassin Game Theory
There is a king who hunts for the treasure and during his mission, he will face many enemies and there will be a 3 minutes time limit to complete the game.
And that’s all the theory behind my game and you should also follow a similar approach while building your game.
So now let’s discuss what are the tools and techniques behind Forest Assassin game?

Tools & Techniques
In every 2D platformer game, there are many components which we need to build to finish the game and these components are the major elements of every platformer game.
1. Character Movement
2. Character Death
3. Character attack
4. Score Counter
5. Timer
So now let’s discuss how I have implemented all of these components into my game one by one.
Character Movement
To move a character in the game you need to have a script which will control the character to Walk, Jump, Crawl and so on and also it requires physics.
To control the physics Unity provides a component called Rigidbody which handles lots of fundamental physics like force and acceleration and this component you can use with anything, which you want to interact with the physics.
At last, it requires a floor where the character can able to walk and in Unity it’s called Collider component and there are many different types of collider are available such as Box ColliderCircle Collider, etc.
When you have all of these elements in place then you can able to move your character.
Character Death — (Game Over)
In this game, the character can only die when he touches with the enemy or with the water and to handle the trigger between the collider Unity has an API called OnTriggerEnter2D(Collider2D trigger) which will handle the trigger between the collider and after the collider, it will send a message to the Collider2D parameter which we can use to decide the death of our character.
And to make it organised I have assign tag name called ‘Enemy’ so that in future if I will add one more enemy then all I need to do is assign this tag name to this component and it will be handled through the Script.

Character attack
Character attack is the most crucial element in every game because this is how you win the game and while building this component the two things you need to understand first is the animation and second is the attack script.
To make the animation in 2D first you need to gather all the sprites and add it to the animation component and create your animation and later you can use it through the script.
While writing the attack script a few things we need to know such as Attack range and attack point because it’s quite important that from which position the character will set his attack vector and in this game Sword is our attack vector, so when the character uses his sword to attack the enemy then we need to know the range and the point of the attack so that we can successfully launch our attack.
To calculate the attack range and point Unity has an API called Physics2D.OverlapCircleAll it will return a list of colliders which falls with the circular area and when we get all the colliders then all we need to do is start a loop and within the loop destroy the enemy game object.
And to give more clarity to our attack I have created a blood particle system which will appear on every enemy death.
Score Counter
If you understood the above two techniques (Character Death & Character Attack) then to implement the scoring functionality it’s quite easy as we need to follow a similar approach. When the character and the gold coins collide with each other then I am calculating the score to +10 through the script and again here also I am using OnTriggerEnter2D(Collider2D trigger) API.
To display the score on the game I have used Text UI component which comes under UnityEngine.UI.
And the same scoring variable is being used in all three components (You Win, Game Over & Time’s Up).
Timer
To implement the timer functionality Unity has an API called StartCoroutine which is used to create a parallel action and can pause the current execution and then continue where it left off and all the coroutine function is declared with a return type of IEnumerator with the yield return statement.
In this game, I have set the timer to 3 minutes and when the game starts immediately it will start the coroutine function and calculates the timer and when the timer reaches zero it will call StopCoroutine function.
And again I have used Text UI component to display the time on the game.
How to publish for WebGL?
Unity has WebGL build option which allows publishing content as Javascript programs and internally it uses HTML5/JavaScript, WebAssembly, WebGL rendering.
In today’s modern web browsers by default enables WebGL and it’s quite helpful to run Unity WebGL application on the browsers.
To run a WebGL application we need to run through a local server and in this game, I have Node.js as my server but you can also use any other scripting languages.
Open Source
I have made the complete development open-source on my Github so that you will have a clear understanding of every component of this game and also I hope it will give you an idea to build your own game.
So this is the overall explanation of Forest Assassin game, I hope you understood the tools and techniques behind this game if you find any issues then please feel free to raise an issue on Github.
Enjoy the game and also share this game with your friends.
Thank you & Happy coding :)

Written by anshuman-pattnaik | twiiter/@anspattnaik
Published by HackerNoon on 2020/05/10