The Unity Tutorial For Complete Beginners
Introduction
In this section, Mark introduces himself and explains his experience with Unity game engine. He also shares his three-step technique for learning Unity.
Mark's Experience with Unity
- Mark has always wanted to make video games using software like Unity.
- He found lengthy tutorials to be ineffective and prefers hands-on learning.
- He developed a three-step technique that worked for him:
- Learn the absolute basics of Unity
- Cement those lessons with simple exercises
- Figure out the rest as you go along
Success Story
- Using his technique, Mark went from ripping off iPhone games to working on his own puzzle platformer about magnets.
- He released an interactive video essay that has had over 100,000 plays.
Purpose of the Video
- The purpose of this video is to save viewers time by providing a tutorial that covers everything needed to make a game in Unity.
- The tutorial will cover every step from downloading Unity to building a game that can be shared with friends.
Getting Started with Unity
In this section, Mark walks through the process of downloading and installing Unity and Visual Studio.
Downloading and Installing Unity Hub
- Go to the website and download the Unity Hub.
- Create a free account to use it.
- Install the Unity Editor (version 2021.3).
Installing Visual Studio
- Under installs, hit the cog icon on the Unity Editor and pick modules.
- Microsoft Visual Studio should be ticked; hit continue and install it.
- Scroll down on this screen and tick game development with unity, then untick unity hub because we already have it installed.
Creating a New Project
- Open up unity hub again after installation is complete
- Pick new project and choose all templates.
- Use 2D, Core. This is an empty project with a few configurations to make it suitable for 2D games.
- Give your project a name and hit create.
Becoming Familiar with the Unity User Interface
In this section, Mark introduces the default Unity user interface and explores the different panels.
The Project Panel
- Contains everything that is in our game like sprites, sound effects, scripts, tiles, fonts, etc.
- Drag and drop files from elsewhere on our computer to add them to the project.
The Hierarchy Panel
- Contains all of the stuff that's in the current scene (which will be a level).
- Right-click and choose Create Empty to make an empty GameObject.
The Inspector Panel
- For messing with GameObjects.
- Once we've selected our new empty GameObject we can put a name in the top field.
- Add components to fill that container with extra features.
Making the Bird Appear on Screen
- Add a Sprite Renderer component to our empty GameObject.
- Slap the bird image onto the GameObject by dragging it from Project panel into Scene view.
Adding Physics Components
In this section, Mark explains how physics work in Unity and adds physics components to our bird object.
How Physics Work in Unity
- Everything in Unity has a Rigidbody component attached by default which makes objects fall down due to gravity.
- We can add other physics components like Box Collider or Circle Collider for collision detection.
Adding Physics Components to Our Bird Object
- Add Rigidbody 2D component so that bird falls down due to gravity
- Add Circle Collider 2D component so that bird collides with other objects
Adding Movement to the Bird
In this section, Mark adds movement to our bird object.
Adding a Script Component
- Add a new script component to our bird object.
- Name it "Bird" and open it in Visual Studio.
Writing the Code for Bird Movement
- Write code that moves the bird up when spacebar is pressed.
- Use Time.deltaTime to make sure movement is consistent across different devices.
Creating Pipes and Spawning Them
In this section, Mark creates pipes and spawns them in the game.
Creating Pipe Sprites
- Create pipe sprites in Photoshop or use assets provided by Mark.
Adding Pipes to Our Scene
- Create an empty GameObject called "Pipes".
- Add two child GameObjects called "TopPipe" and "BottomPipe".
- Add Box Collider 2D components to both child objects so that they can collide with other objects.
Spawning Pipes
- Write code that spawns pipes at regular intervals using Instantiate method.
- Randomize the height of pipes so that they appear at different heights each time.
Scrolling Background
In this section, Mark adds a scrolling background to our game.
Creating Background Sprite
- Create a background sprite in Photoshop or use assets provided by Mark.
Adding Background Object
- Create an empty GameObject called "Background".
- Add a Sprite Renderer component and attach the background sprite.
Making Background Scroll
- Write code that scrolls the background continuously using offset property of material attached to Sprite Renderer component.
Game Over Screen
In this section, Mark adds a game over screen when player collides with pipes.
Creating Game Over Screen
- Create a new scene called "Game Over".
- Add a UI Text object and write "Game Over" on it.
Writing Code for Game Over
- Write code that switches to the game over scene when player collides with pipes.
- Use SceneManager.LoadScene method to switch scenes.
Adding Sound Effects
In this section, Mark adds sound effects to our game.
Downloading Sound Effects
- Download sound effects from website or use assets provided by Mark.
Adding Audio Source Component
- Add an Audio Source component to our bird object.
Playing Sound Effects
- Write code that plays sound effects when bird jumps and when player collides with pipes.
- Use AudioClip property of Audio Source component to attach sound effect files.
Unity Interface Overview
In this section, we get an overview of the Unity interface and its different panels.
Unity Panels
- The Unity interface has four panels by default.
- Project panel holds all the assets in our game.
- Hierarchy panel lists all of the GameObjects in the current level.
- Inspector panel lets us see and change those GameObjects' components.
- Scene view panel shows us what's in our current scene and allows us to move stuff around, scale it, etc.
Game View
- The Scene view has an extra tab for Game view which shows us what the game will look like from the main camera when it's running.
- From this dropdown, we can set a resolution or aspect ratio to get a better idea of what it will look like when played.
Adding Components to GameObject
In this section, we learn how to add components to a GameObject.
Rigidbody 2D Component
- We add a Rigidbody 2D component to make the bird into a physics object that is affected by gravity.
- This turns our bird into a physics object with gravity so that when we hit play, the bird drops and falls off the screen.
Collider Component
- We add a circle collider 2D component so that our bird can interact with other objects in the game.
- We can see the collider as a green outline in the Scene view.
Script Component
- We add a script component to make our own custom component using programming code.
- We create a new script called BirdScript and open it up in Visual Studio.
Writing Code for GameObject
In this section, we learn how to write code for our GameObject.
Start and Update Functions
- The start function is for any code that will run as soon as this script is enabled and runs precisely once.
- The update function runs constantly while the script is enabled and will fire off every line of code every single frame over and over again.
Talking to GameObject with Code
- We can use code to talk to the game by choosing someone to talk to - in this game, the GameObject - and then a topic of conversation - its name - and then a command - change it to Bob Birdington.
- Initially, a script can only talk to the GameObject's top bit and transform, so we need to create a special slot on this script for Rigidbody2D so we can then talk to it and send it commands. This is called a reference.
Adding Upward Velocity
- In the Rigidbody 2D's component under info, we'll see a greyed-out field for velocity, which we want to write some code to add upward velocity to make the bird fly into the air.
Changing Properties of a Component
In this section, the speaker explains how to change properties of a component using code. They demonstrate how to access and modify the velocity property of a Rigidbody component in Unity.
Modifying Velocity Property
- Use code to change properties of a component while the game is running.
- Access and modify the velocity property of a Rigidbody component in Unity by typing "myRigidbody.velocity = new Vector2(0, 1) * flapStrength" in the update function.
- A vector is two numbers that represent a position in 2D space. In this case, it's used to represent a direction for the bird to travel. The shorthand for zero comma one is Vector2.up.
- To give more power to the bird's upward movement, multiply Vector2.up by flapStrength instead of 10. This allows us to change flapStrength value in Unity's inspector even while the game is running.
Using If Statements
In this section, the speaker explains how if statements work and demonstrates how they can be used to make an action happen only when certain conditions are met.
Implementing If Statements
- Use if statements as gates that surround some code with fences so that every frame that code will be completely ignored unless specific conditions are met.
- To implement an if statement, write "if (Input.GetKeyDown(KeyCode.Space))" followed by curly brackets containing code you want executed when space bar is pressed on this frame.
- Use one equals sign (=) to make the thing on left be same as thing on right and use two equals signs (==) to check if the thing on left is same as the thing on right.
- Use Input.GetKeyDown(KeyCode.Space) to ask Unity if the space bar has been pressed on this frame and finish with equals, equals true.
Making Pipes Spawn into the World
In this section, the speaker explains how to make pipes spawn into the world, move across the screen, and then delete themselves.
Spawning Pipes
- Create a new GameObject called "pipe" and another object within it called "top pipe". The top pipe is a child of the first GameObject's parent.
- Make two pipes which move across the screen from left to right by using code in update function that moves them by changing their position property.
- Use Time.deltaTime to ensure that movement speed remains constant regardless of frame rate.
- Delete pipes when they are no longer visible on screen by checking their position against a certain value and destroying them if they pass it.
Adding a Sprite Renderer and Collider
In this section, we learn how to add a sprite renderer and collider for the pipe image. We also duplicate the top pipe object, call it bottom pipe, flip it upside down by changing the Y scale to minus one, and move it down below the bird.
Adding Components
- Add a sprite renderer for the pipe image.
- Add a Box Collider 2D.
- Duplicate the top pipe object and call it bottom pipe.
- Flip it upside down by changing the Y scale to minus one.
Moving Objects
- Add a script to make objects move across the screen.
- Create a variable for moveSpeed.
- Write code to move the object in update using Vector3 instead of Vector2 because Unity is still fundamentally a 3D engine and so it's keeping track of the object's depth with the Z value.
- Multiply by Time.deltaTime to ensure that multiplication happens at the same rate regardless of frame rate.
Creating Prefabs
In this section, we learn how to create prefabricated GameObjects or prefabs. We also create a new GameObject called Pipe Spawner and write some code to spawn new versions of the pipe prefab every few seconds.
Creating Prefabs
- Take parent GameObject from hierarchy and drag it into your project to create a prefab.
- Delete original in our hierarchy now.
- Make a new GameObject called Pipe Spawner just right of camera.
Spawning New Versions
- Write some code to spawn that prefab we just made using Instantiate method which creates an object at specified position and rotation.
- Make variables spawnRate (how many seconds between spawns) and timer (the number that counts up).
Timers and Spawning Pipes
In this section, the instructor explains how to use timers to spawn pipes in a game. They also cover how to randomize the height of the pipes and delete them when they go off-screen.
Using Timers for Spawning Pipes
- If statements can be used as gates in code.
- A timer can be used to check if a certain amount of time has passed before executing code.
- Code that is repeated should be put into a function to avoid duplication.
- The function can then be called from other parts of the code.
Randomizing Pipe Height
- Pipes always spawn at the same height, which is boring. We want them to come out at random heights.
- A public variable for height offset is created, and two floats are made for lowestPoint and highestPoint.
- The Instantiate code is updated with a new Vector3 that specifies X as the same as the spawner, but Y as a random point between lowestPoint and highestPoint.
Deleting Pipes When Off-Screen
- Pipes will continue running even when they're off-screen, so we need to delete them after they've gone past a certain point.
- An if statement checks if transform.position.x is less than deadZone (a float value), then destroys the GameObject that holds this script.
- Debug.Log can be used to send a message to the console when a pipe is deleted.
Keeping Track of the Player's Score
In this section, we learn how to keep track of the player's score and display it on the user interface. We also learn how to make a script that stores the player's score and changes the number on the UI to that score.
Creating a User Interface for Score Tracking
- We create a text GameObject in the hierarchy under UI.
- To ensure that the UI looks consistent across all devices, we set the canvas scaler component's UI scale to scale with screen size and choose a reference resolution.
- The text has a rect transform instead of a normal transform, so we change its width and height instead of its scale.
- We increase the font size and set the default text to 0.
Making a Script for Score Tracking
- We create a Logic Manager GameObject with a script that keeps track of high-level information like player score.
- We store an integer for player score in our script.
- We make a reference to our text component by adding "using UnityEngine.UI;" at the top of our script and dragging in our text GameObject into our slot.
- We create an addScore function that adds one to player score and changes the text on our UI to be this number using ".toString()".
- To test if this works, we use ContextMenu above our function name in Unity while running our game.
Triggering Score Increase
- We add an invisible trigger collider called middle between pipes prefab.
- In OnTriggerEnter2D function, we run addScore when bird passes through pipes.
Adding Logic to the Game Object
In this section, we learn how to add logic to a game object and set up tags.
Adding Tags
- Choose "Add Tag" from the drop-down menu.
- Create a new tag called "Logic."
- Go back to the GameObject and set the new tag.
Finding GameObjects with Tags
- Under Start, write
logic = GameObject.FindGameObjectWithTag("Logic").
- This will look for the first GameObject in the hierarchy with the tag "Logic."
Making Components More Versatile
- Take the AddScore function in Logic Script and write
int scoreToAddin empty brackets.
- Instead of adding one, write
scoreToAdd.
- In PipeMiddleScript, write a 1 in brackets after AddScore.
- This allows us to make a function more versatile as it can be used in different ways from different places.
Connecting Scripts
In this section, we learn how to connect scripts and check collisions.
Connecting Scripts
- The pipe's middle script can find and talk to the logic script.
- Write
logic.addScoreto run that code.
Checking Collisions
- Put bird on a layer and check if colliding object was on that layer.
- On PipeMiddleScript, add an if statement around AddScore and check if collision was with a GameObject on bird's layer.
Adding Fail State
In this section, we learn how to add a fail state when bird hits pipes.
Creating Game Over Screen
- On Canvas GameObject, add an empty one called "Game Over Screen."
- Then add text for game over and button under legacy.
- Resize button and change text on the button.
Restarting Game
- Make another public function called "restartGame."
- In this function, write code to restart the scene.
- Add an event to the button in Unity.
Turn any video into a summary like this
YouTube links, meetings, lectures. With transcripts, search, and chat.