How to Get Started With Game Development Using Unity

on November 09, 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.


What Is Unity?


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.


Why Is Unity So Popular?


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:

  • Affordable Cost: Anyone can get started with Unity via its free version. And for veteran developers who are looking for extra features, affordable monthly plans do the trick.
  • Less Code Dependency: Unity is beginner-friendly and does not require much prior coding knowledge. In fact, it is possible to create a game from scratch without even writing a single line of code.
  • Strong Community Support: Unity sports a friendly community environment where developers can always reach out for and receive prompt help.
  • Graphic Suite: With a rich library of high-quality visual effects and high customization, Unity supports the development of intuitive games with smooth and natural movements and rendering.

Unity For Beginners - Your First Project


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:


1. Editor Window


Editor Window Unity

Once Unity launches, this is the first visible screen and is known as the Editor Window. It is divided into the following sections:

  • Hierarchy: Nests and structures all the GameObjects that are being used in a scene.
  • Scene View: Caters to the placement and movement of various GameObjects.
  • Inspector: Shows all available details of GameObjects that are present in the scene.
  • Game View: Gives a preview of the player’s point of view while playing the game.
  • Assets / Project: Stores details and components such as textures, models, scripts, etc.

2. Elements of Unity 3D


Unity essentially has a set of predefined elements that come together to form a game through their various functionalities:

  • Assets: They are the representation of an external file (audio, image, texture) that can be imported in Unity. Assets can include animation files, renders of textures, audio mixers, and more.

Elements of Unity 3D


  • Project: A location that holds all the files in your current project along with their assets.
  • Packages: A cluster of game assets that have been precompiled and included in Unity.
  • GameObject: All possible objects that are used in a game are called GameObjects. They function as placeholders for important components such as Transform, Light, or Script.
  • Components: Basic building blocks of games that include the details of all objects in a game and their activities.
  • Scenes: Scenes act as the base on which GameObjects are placed to make Levels. Multiple Levels are then put together and linked to create game sections.
  • Prefab: Reusable GameObject components that can be used on multiple scenes by creating ‘Instances’.
  • Build: An export of the game that contains all scenes for playbacks.

3. Built-in Components


Unity implements an Actor Component model to create games where the GameObjects are actors and Components are scripts. What does this mean? GameObjects on their own do not have any use or functionality. To add this, Components in the form of C# or Javascript are required. Key build-in components in Unity include:

  • MeshRender: Assigns materials to a 3D Mesh.
  • Light: Illuminates a scene or parts of it.
  • [Box | Mesh] Collider: Used during collisions to enable detection of GameObjects.
  • Camera: Attaches the Player viewport to a GameObject.
  • MeshFilter: Assign different materials to a 3D mesh to a GameObject.
  • Rigidbody: Acts on GameObjects with 3d Meshes by using realistic physics simulation.
  • Multiple UI Canvas Components to display GUIs.

4. Adding and Managing Assets


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:

  • Add the image to Unity by dragging and dropping it to ‘Assets’.

Unity Assets


  • Now drag this file onto the Scene section to add it to the current scene.

Unity Advance Assets


  • You may now notice the ‘Transform’ properties of this object on the right-hand panel. Here you can edit the Position, Rotation, and Scale factors to see and understand how it behaves.
  • Once done, click on the Play button to see that the image is now a part of the game and visible.

![Unity Transform](unity_transform.png)

As your project grows, you can keep adding various objects in a similar manner and organize them as needed.


5. Engine Physics Components


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.


Unity 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.


6. How to Add C# Script


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:

  • Right-click in the Assets panel. Navigate to CreateC# Script

Script in Unity


  • You will now see a new file under Assets called NewBehaviourScript.

New Behaviour Script

  • Rename the script to your choice. For example, while defining the position of the object, you can name the script as Position (for easy retrieval). Since we will be defining the movement of our player ball here, let’s name it as Movement.

When you open the Script file (in a text editor of your choice), you will see two predefined methods under the MonoBehavior class.


File Info Unity


  • Start() method: Run by the script when the GameObject is initialized and becomes active. It is used to set the initial state of GameObjects, such as the bullets in a gun as depicted in the example below:
    public int bullets;

Start() Method


  • Update() method: Called once per frame, this is where all the action of games takes place including the addition of forces, detecting user input, spawning, etc.

Void Update

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.


7. Rigidbodies


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:

  • Navigate to the inspector for the GameObject and click on AddComponent.

Game Object Add Component


Navigate to Physics 2D -> RigidBody2D to add a RigidBody2D component.
![Rigid Body 2d](rigid_body_2d.png)
  • As soon as you click on this, you will notice the ball fall out of the scene. This is because of the Gravity Scale property of Rigidbody. Setting it to 0 will solve this.

In this manner, you can define how different game objects behave with rest to gravity, essentially defining their mass and free-fall momentum.


8. Movement of Game Objects


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:

Rigidbodies

Here’s what happens in the code:

  • ballBody.velocity = is used to assign a velocity to the Rigidbody that is denoted by ballBody.
  • Vector2 defines the velocity of the Rigidbody with input parameters in x and y axes.
  • Input.GetAxisRaw("Horizontal") and 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).
  • speed defines 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.


9. Accepting User Inputs


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.

Conclusion


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!