Exploring the Unity Game Engine
Introduction:
Unity is one of the world's leading real-time development platforms. It is predominantly used for Game Development; however, its incredible capabilities also allow Unity to also be used for Architectural Visualization, Animation, Simulations, etc. Some notable games that are developed on Unity include: Rust, Cuphead, Among Us, Fall Guys and Genshin Impact.
Installation:
To install Unity, we first have to install the Unity Hub, this can be installed directly from the Unity website. The Unity Hub, like the name suggests, serves as a hub of everything you work on in Unity. Upon Installation the Unity Hub will appear as such.
There are multiple versions of Unity that can be accessed through the Unity Hub. Throughout the duration of this semester, I will be using 'version 2021.3.16f1'. Unity is installed with a Code Editor Package for Visual Studio which integrates with Microsoft Visual Studio by default, but it can be changed if you want to use a different environment.
To set the External Script Editor Manually:
- Open a New Project.
- Go to Edit > Preferences.
- Select External Tools.
- Click on the External Script Editor dropdown and select your preferred Editor.
The Interface:
When opening a new project, the interface will be divided into four main tabs. There's the Hierarchy, the Scene View, the Inspector, and the Project Manager. Each tab will have its own unique function.
- The Hierarchy
The hierarchy contains everything in your 'scene'. The hierarchy will contain every component that is in your game (Camera, Lighting, Environment, Player, Enemies, etc.)
- The Scene View
The scene view is pretty self explanatory. This is where you'll be able to view your entire scene. You can drag and drop components from the hierarchy into the scene view. You'll be able to move around the scene view with the right-mouse button, and the middle-mouse button.
You can switch between the Scene View and the Game View by clicking the tabs at the top of the panel. The game view will lock you to the perspective of your camera in the scene. If you do not have a camera component, the screen will appear black.
- The Project Manager
The project view is where all the files in the project is managed. This is where your scripts, textures, audio sources and prefabs will be located. From here, you can also import assets that you can use for your project.
- The Inspector
The inspector contains all of the properties of any asset. Properties like appearance, material, texture, physics, and scripts will be added and edited from here.
Creating a new Script:
There are multiple ways to create a new script. You can create a new script through the project window- Right-Click > Create.
- C# Script.
When creating a script through the project window, you'll have to manually drag the script into the inspector of any asset you want the script to apply to. You can also create a new script through the Inspector. Once you have an asset selected, you will have the option to add a component through the inspector.
Selecting the 'New Script' option will prompt you for a name. Once the script is created it will appear in the project window. You'll be able to see a preview of the script through the inspector if you select it from the project window.
Working on Scripts:
Every script in Unity derives from the base class MonoBehaviour. This class holds many important functions that will be necessary when making any game such as, Start(), Update(), Invoke(), Destroy(), Instantiate().
Creating a script will automatically generate a public class with the given name of your script and two methods, Start() and Update(). These two methods are pretty self explanatory. The Start() method will initiate at the very moment you run the game. The Update() method will continue on initiating and update each asset at every frame.
For example, I start my game with a cube. At the very beginning of the game, I want to change the color of the cube to blue. This can be done by creating a Renderer variable and a Color variable.
Creating public variables in your script will allow for it to be visible in the inspector window. From the inspector window, you can edit and assign values to your created variables. Naturally, any private variables will not be visible from the inspector window. Inside the Start method, we can create a new Material and assign that value to the material of the renderer. We can then assign the color of the material to the color that we chose in our created variable.
This should effectively change the color of our cube once the game is started. Going back to the Unity project, the game can be started by clicking the play button located at the top of the Scene View. However, once we start the game the color of the cube won't actually change and you will be met with the following error message.
This is because we have yet to assign a value to the Renderer variable that we created. While having the cube selected, we can see the properties of the script that we created in the inspector. Because we created two public variables in the script, these settings will appear.
We have to assign a variable to the Renderer property. This can be done by manually dragging the cube from the hierarchy into the box, or by clicking the circle icon on the right of the panel and then selecting the cube from there. Once we start the game again, the color of the cube should now successfully change to blue.
Currently, once we run the game the color of the cube changes, but it doesn't have any other features. What if I want the cube to grow bigger over time, or I want the cube to move to another position? This can be done through the Update() method.
We have to initialize a GameObject variable so that we are able to access the physical properties of the cube. This can be done by creating a variable of type "GameObject". Furthermore we also want to initialize a value in which we want to change the scale of our cube.
Additionally, I want the cube to move forward once the game starts. To do this, I use the 'transform.Translate(Vector3.forward)' command to move the cube. 'Vector3.forward' is essentially a simplified way of saying 'Vector3(0, 0, 1)'. Since we are working inside the Update method, this command will move the cube at every frame. To produce a more consistent effect, we have to use 'Time.deltaTime'. This essentially updates the cube every second instead of every frame.
Implementing Player Movement:
One of the most important aspect to any game is controls. Whether its a platformer or a first-person shooter, the player has to have something to control in order for it to be called a game. Luckily unity has a built in Input manager that can be easily used to implement player controls. We start by creating a new project and creating a 3d plane object through the hierarchy that will serve as the ground. We can also add a few cubes and arrange them into a structure. Lastly we need an object for the player to control and position the camera so that we can view the entire scene from the top.
The Unity Input Manager allows you to define inputs and their associated actions according to your needs. It can be accessed from Unity's main window by going to Edit > Project Settings, then selecting Input Manager. The input manager has 18 default inputs mapped but you can add and remove inputs according to what your project requires.
I created a script called 'CharacterMovement' which will hold all the necessary code in order for the player to be able to move around in the game. I start by creating variables for horizontal input, vertical input, and speed. In order for these variables to directly reference the inputs from the Input Manager, we have to use the Input.GetAxis() command. Now in the Update() method we can call transform.Translate along with the input variables that we created.
How this works is each Axis in the input manager has a negative and positive button. In the case of the vertical input the positive button is "W" and the negative button is "S". The default value for the inputs is 0, but pressing these assigned buttons will change the value of Vertical Input to 1 and -1 respectively. This can be best observed through the inspector.
Conclusion:
There are many things to be achieved through unity and game development as a whole and its a wonderful place to express creativity and develop in demand skills. Whether its a career or just a hobby, Unity's ecosystem caters to everyone. Over the past few weeks, I've established a decent understanding of the basics of Unity, but that's just the tip of the iceberg. Over the remaining duration of the semester, I will be attempting to create a basic topdown shooter which is what I will be highlighting in my next blog post.

Comments
Post a Comment