I began learning Unity by watching Mark Brown’s (GMTK) video on learning Unity as a complete beginner. I immediately found it useful as it helped me to quickly learn how the Unity systems interact with each other, allowing me to apply my own knowledge of programming on top of that, to produce a solid foundation on which I could build any game.
Games are broken into scenes, which are essentially levels. The Unity interface is very intuitive, with objects in the scene listed on the left, assets at the bottom, a view of the game space in the centre and an inspector on the right; the inspector allows the developer to select an object and view information specific to them.
I followed Mark’s advice and decided to create a simple game that already exists; this put the focus on the coding and building, rather than worrying about any of the design. I soon found that writing C#, which is Unity’s programming language for writing scripts, came easy to me as I have a background in Java and Object-Oriented Programming. However, I often found that I had difficulty with knowing exactly what each component did, and how I could best utilise them without the need for writing a script.
Components in Unity are, according to the Unity manual (https://docs.unity3d.com/Manual/Components.html), “the functional pieces of every GameObject.” A GameObject is simply a container for an object in the game, for example: a player character or the floor they are standing on; a GameObject can be empty and simply include scripts that need to run in the background. GameObjects need components to store and keep track of information about them, such as sprites, as well as code that is applied to them, such as a collider. Unity already comes with a lot of components that would be needed when developing a game. So far, from those that Unity provides, I have used the Sprite Renderer, Rigidbody 2D and Collider 2D components.
Renders a sprite image onto the object. This means that the player would see the sprite whenever the object appears on screen.
The RigidBody 2D component allows the object to have physics applied to it. For example, the object can be pushed around by other objects in the environment. This component also allows for gravity to affect the object.
The Collider 2D component adds a collider to the object that detects when it has collided with another object. There is not a component called “Collider 2D” as there is a collider component for each shape; the component needed would be “Box Collider 2D” or “Polygon Collider 2D” depending on the shape. A collider will automatically impact a rigidbody, stopping it from moving past it, but if the collider is set to “isTrigger” then the rigidbody will pass through it and a collision event will still be registered. The collision event can then be used in a script to do something, such as increase a score.
When choosing a game to develop I wanted to create one where the player stayed in the centre of the screen and obstacles came at them. This is because my game project involves the player controlling an entity in the centre and avoiding obstacles by going up or down. Therefore, I would learn how to spawn obstacles offscreen and how to allow the player to control up/down movement. I also worked in 2D, using only 2D components, because my game would be 2D.
I chose to develop a simple endless runner, as it included all of these elements. An endless runner was also similar to Flappy Bird, which Mark covered in his tutorial, so I could refer back to his video if I got stuck.
I began by generating the player object, creating an empty GameObject and applying a sprite renderer component to it that rendered a blue hexagon. Next I added a Rigidbody 2D component to the object that made gravity affect it. Then I added a new script component so that I could add a transform vector going up, every time the player presses space. This was put in the update function, which is called every frame, and I used an if statement to check if the player had pressed the space bar. The code is:
// Update is called once per frame
void Update()
{
if ( Input.GetKeyDown(KeyCode.Space) ) { // If space is being pressed
rigidBody.velocity = Vector2.up * jumpHeight; // Change the rigidbody velocity to vector going up multiplied by the jumpHeight variable
// jumpHeight is a public variable that can be changed in the Unity editor, thus making the code more flexible
}
}
I also added a Polygon Collider 2D to the player object, for use in the future. The collider, helpfully, adapted its shape to the edges of the hexagon so that I did not need to do it.
Next I added the floor as a new object that was bigger than the camera’s view. I added a Box Collider 2D and a Rigidbody 2D to it, however, then it was affected by gravity. To stop this, I froze the Rigidbody 2D component in x and y. However, I forgot to freeze the rotation, which resulted in this: