7 minutes
I made a game!!!!!
Written By: A.Karthik
Hello my fellow blog readers, I got an interesting post for you today. Who does not like playing games, almost from 3 year olds to 40 year olds play games on mobile or their pc’s or laptops or consoles, lot of options. So today I am going to discuss what unity is and how I developed a 2d game using UNITY.
Unity is a software/game-engine!!
So let us say that someday you decided to develop a game all by yourselves by using java or objective-c , this would just be fine if you are in the 1900’s but your in 2000’s now (this does’nt make sense but you get the point).The biggest problem when you decide to develop a game from scratch is that you have to develop your own collision system, a physics engine, an object renderer, a light emission system and a lot of other stuff which is scary right, so instead of finishing the game in 1 year you might end up finishing it in 2 or maybe more years this is where unity comes into picture.Unity has everything that you need to get started, it has its own physics system(which is really complex to understand sometimes), its own animation rig and others that make developing a game lot more fun and interesting.
So why Unity and why not others??
There are other handfull of game engines that you could use like unreal, GODOT, GameMaker studio and source engine(not for commercial purposes). FUN FACT: SOURCE ENGINE IS A REALLY OLD GAME ENGINE AND APEX LEGENDS WAS DEVELOPED USING THIS ENGINE, so why unity.Unity Technology may be an unsung name among the general public, but game developers know that it is the most widely used and fastest growing game development platform available today. Big game titles including “Fallout Shelter,” “Temple Run,” “Monument Valley,” “Battlestar Galactica Online,” “Assassin’s Creed: Identity” and “Hearthstone: Heroes of Warcraft” are made with Unity.Unity decreases the necessary complexity of game code - and also increases comfort and capability.Another thing that sets unity aside from others is its crazy fast development time.Also, Unity3d is really good on multiple platforms. Of course, you can’t create, say, a windows .exe game and then magically have it “just work” on the iPhone; but Unity gets pretty close to that. What is required is “tweaking” more than “porting”.
The charts say it all, Worldwide Unity takes a plus 45% share of the full feature game engine market, approx. 3 times that of our closest competitor and you know what the unity game engine is far more popular amongst developers than any other game development software.The proportion relying on Unity as their primary development tool using Unity is growing all the time.
Unity Dominates 3D Mobile Games industry.In every major market in the world, an overwhelming majority of the top-grossing 3D mobile games made with third party tools are made using Unity.
Enough of these charts now lets break down the development of my game.
How did it start??
Before I knew about game engines I wanted to make a space-shooter using java and as I discussed earlier the problems one would face while doing this, but i did not know about UNITY or any other game engine so I developed my own game engine without even knowing it.So my first ever game was called THE SPACE WARRIORS and was entirely developed in java.I never finished developing this game because it was really hard to do it.The most difficult part for me while developing this game was to make it run the same on a high end system and a low end system another notable difficulty was the collision system and the sprite rendering system which helps render images or sprites on to the screen. here is a screen shot of how the game looked(all the spaceships or warplanes that you see in this pic were designed by me):
I tried to learn unity many number of times but I could not understand how it all worked, like how 2d animations should be done, how the levels should be designed and how the player movement should be implemented, but the urge to develop a 2d game made me comeback to unity and it made me keep learning unity. After spending hours and hours of watching tutorials on youtube(especially from BRACKEYS, because he is the best) I finally understood some basic functionalities of UNITY and started experimenting with them.
The following codeblock is the first movement script that I have ever written and it worked just fine.
//written in C#.
void Movement(){
float move = Input.GetAxisRaw("Horizontal");
if(Input.GetKeyDown(KeyCode.Space) && _grounded){
_rigidBody.velocity = new Vector2(_rigidBody.velocity.x ,_jumpForce);
_anim.jumping(true);
}
_rigidBody.velocity = new Vector2(move * _speed,_rigidBody.velocity.y);
//play the animation of movement.
_anim.Move(move);
}
let us break the codeblock down.There are some variables that you might notice like _anim,_rigidBody and _grounded which are not really easy to explain afterall ABSTRACTION is what we need so lets move on to the simpler one’s. The if condition is where the jumping happens and the _grounded variable is a boolean that is controlled by a function that I developed using unity’s ray tracing, ray tracing is just a ray drawn from the center of the Rigid Body to anywhere you want it to point, this ray detects the collisions of the RIGID BODY with the other objects in our case the GROUND.
I also learned that this is the basis of all the player movement, this is how it is done. The character was moving fine and all but the sprite was not flipping, it was facing the positive x-direction even when the player is moving in -ve x-direction.The following was a simple fix for that.
//_renderer is an instance of SpriteRenderer and using this instance the
//flipX property is accessed accrodingly.
if(move<0) _renderer.flipX = true;
else if(move > 0) _renderer.flipX = false;
Ok so the flipping was fixed but what about the animation??
Animation and Animator in UNITY.
Before we talk about animation and animator lets talk about sprites and sprite sheets. What you are seeing in the image is called as a spritesheet and each individual image after slicing from the spritesheet is called as a sprite, When you play this sprites in the same order you get an animation VOILA, so this is how 2d-animation works in unity.Unity also offers another way to animate your character and it is called skeletal animation which is way more complex cause you have to use a bone mapping system and a muscle system on your character in order to get a smooth animation but this is very effective because it lets you move your character in any desirable manner. Consider the same image and let us say that you sliced the individual “SPRITES” from the “SPRITE SHEET”, now how do you animate it?? thats where unity’s animation system enters.
Unity makes use of state machines to handle multiple animation states, and these state machines can get pretty complex depending on the abilities that your player has or how the player can interact with the surroundings etc and each state of the state machine displays a different animation but how does one transition from one animation state another?? using the parameters that you can change through code.Unity lets you add any number of parameters to a state and these parameters can help you transition from one state to another.So let us say that your character is moving so you set up a float parameter for the animation state and when the conditions for the parameter are satisfied the animation state is played.
//a simple script that handles the move and jump animations.
public class playerAnimation : MonoBehaviour
{
private Animator _anim;
// Start is called before the first frame update
void Start()
{
_anim = GetComponentInChildren<Animator>();
}
public void Move(float move){
_anim.SetFloat("Move", Mathf.Abs(move));
}
public void jumping(bool jump){
_anim.SetBool("jumping",jump);
}
}
//this movement function is from another class.
void Movement(){
float move = Input.GetAxisRaw("Horizontal");
_rigidBody.velocity = new Vector2(move * _speed,_rigidBody.velocity.y);
//calling the move function of the playerAnimation class.
_anim.Move(move);
}
In this codeblock you might be wondering what monobehaviour is, well for now let us just say that it is the base class from which every unity script derives(taken from official unity scripting API) but there is a lot more to it that this single line.So thats how you set up animations in unity.
There is a lot more to talk about developing a game but these are the basic steps that you might need to kick off. Since this is a diverse topic I am breaking this post into 2 parts , so in the second part I’ll be talking about how to design and develop levels using unity’s tile mapping system, the collision system of unity and the enemy AI.