9 Nov 2020
Released way back in 2005, Unity has risen to the pinnacle of game development for both 3D and 2D games over the years. Today, it holds the bragging rights for 27 target deployment platforms, including Android, iOS, and Windows.
This blog takes a look at what it is, what makes Unity so popular, and how aspiring and professional game developers can get started with the platform.
Unity is a popular game development engine with a visual interface for creating games and simulations for cross-platforms. While being proficient in both 3D and 2D games, Unity’s primary goal is to empower developers to create interactive content. It comes with an entire toolkit with various provisions for designing and building games. These include high-level graphics, audio files, level-building tools, and more.
This makes Unity a complete game development package, ensuring that developers require a minimal intervention of any third-party application to complete their projects.
As a platform and game engine, Unity has aligned itself over the years to be at the forefront of all trends. This is one of its USPs. It not only supports the development of mobile games but also supports AR and VR elements in games as well.
Notable features that add to its popularity include:
To get started with Unity, you need to download it first. Head over to this link to download the free version of Unity. Once downloaded, launch the installer package and continue with the on-screen instructions to complete the installation.
Once you launch Unity for the first time, it would ask you to create a Unity ID. Next, select a Microgame to begin with your first Unity project. You can also select an Empty project to have zero preset elements and maximum flexibility.
Now let’s take a look at some of the major components of Unity and how you can go about them:
Once Unity launches, this is the first visible screen and is known as the Editor Window. It is divided into the following sections:
Unity essentially has a set of predefined elements that come together to form a game through their various functionalities:
To understand how Assets can be added to the first Project that we created before, we would be using an image as a player. To do so, simply follow these steps:
As your project grows, you can keep adding various objects in a similar manner and organize them as needed.
The physics that is used in any game defines the way players and characters interact or the way their world behaves. In the most basic sense, physics is used to define the position of objects and displace them by adding forces.
To understand the basics, let’s look at 2D Physics.
We need to deal with 2 axes here - x and y.
In Unity, there are two scripting classes, Vector2 and Vector3, which function as containers for numeric values. Vector2 can hold two values, that is what we will be primarily using. This means that in the code, instead of defining two separate values (x and y-axis) for the ball’s position, we will use Vector2 to hold the value of both these variables. The code can look something like:
Vector2 location; location = new Vector2 (ballPositionX, ballPositionY);
Here, the first line declares an instance of the class Vector2. Line two assigns values to the location vector with values ballPositionX and ballPositionY defining the position of the player ball. Now let’s understand how such codes can be included in a game.
To customize the objects of the game and their interactions, C# scripts are used to define their movements and more.
To create a Script in Unity, follow these steps:
When you open the Script file (in a text editor of your choice), you will see two predefined methods under the MonoBehavior class.
MonoBehavior is the base class in Unity from which all other classes inherit properties. It consists of various values and methods that developers can use in their scripts.
In Unity, Rigidbody is a property that enables an object to interact with forces and acceleration by following the usual norms of physics. You can use Rigidbody2D for this that interacts with the XY axis.
To add a Rigidbody in our example project, follow these steps:
In this manner, you can define how different game objects behave with rest to gravity, essentially defining their mass and free-fall momentum.
Rigidbodies can be used to make the player move through the arrow keys. This can be easily done with the Movement script that we created earlier. We would be adding more lines of code under the Start() and Update() methods now and edit the script to the following code:
Here’s what happens in the code:
ballBody.velocity= is used to assign a velocity to the Rigidbody that is denoted by ballBody.
Vector2defines the velocity of the Rigidbody with input parameters in x and y axes.
Input.GetAxisRaw("Vertical")are used to handle inputs corresponding to the Horizontal and Vertical input axes based on the movement buttons that the player is pressing (up, right, down, left).
speeddefines the speed with which the object will move in the direction of movement.
Now we save this ‘Movement’ script and open Unity to attach it to an object. To do this, simply drag and drop it over the target object.
To have further control over the manner in which a gameObject behaves according to user input, you can make changes to the Update() function of any component such as follows (for example):
Input.GetKey(KeyCode.W)Returns True when the player holds down the W key.
Input.GetKeyDown(KeyCode.W)Returns True when the player first presses the W key.
Hope you liked this tutorial and are now versed with the basics of Unity!
While creating your first game, always remember that it may take a lot of time and practice before you can confidently leverage every feature of Unity to your advantage. In the end, Unity game development is a full-time job that takes substantial time and effort to learn and create. Breaking down your game into small components and achievable milestones can come to your rescue more often than code walls ever will.
In case, if you are looking for a dedicated source and a structured course to learn more about Unity, C#, and game development, stay tuned for our upcoming courses. Not sure how to kickstart? Come join us in this hands-on workshop to build your first minigame!