Unity 2D Master: Game Development with C# and Unity [Part 1/2]-Beginners | Mario Korov | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Unity 2D Master: Game Development with C# and Unity [Part 1/2]-Beginners

teacher avatar Mario Korov, Game Developer | Teacher | Mathematician

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

37 Lessons (1h 50m)
    • 1. Introduction

    • 2. What is Unity Hub?

    • 3. Install Unity Hub

    • 4. Install Unity and Visual Studio

    • 5. First Project

    • 6. First Script

    • 7. Variables

    • 8. Functions

    • 9. Challenge- Functions

    • 10. Challenge Review- Functions

    • 11. Classes

    • 12. Access Modifiers

    • 13. Execution Order in Scripts

    • 14. New Scene and Layout

    • 15. Conditions + [Mini Challenge]

    • 16. Loops

    • 17. Challenge + Review- Loops

    • 18. Arrays

    • 19. Unity Documentation

    • 20. Project Setup and Version Note

    • 21. Sprite Creator

    • 22. Rigidbody and Gravity

    • 23. Colliders and Triggers

    • 24. Collision Matrix and Layers

    • 25. Challenge- Collision

    • 26. Challenge Review- Collision

    • 27. Import Input Package

    • 28. Get Component and Move Player

    • 29. Scene Navigation and Setup

    • 30. OnCollision Functions

    • 31. Prefab

    • 32. Instantiate (create) objects

    • 33. OnTrigger and Destroy functions

    • 34. Sorting Layers

    • 35. Exit The Game

    • 36. Quick Export / Build

    • 37. End of Part 1 and Discord Server

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

(Note that this is PART 1 of Unity 2D Master Course)

This course is made in Unity 2020 using Unity's newest tools and packages. I promise you that this is one of a kind course with short, informative easy-to-follow lessons. No time-wasting, that's why I call it Time Saver.

This course covers all skill levels and therefore is divided into two classes:

  • class for complete beginners in C# and Unity (this class)
  • class for game developers with experience (next class)

If you just started with Game Development, then the beginner class is for you. The lessons will gradually increase your knowledge and improve the skills that are required for the next part of the course.

The beginner sections include:

  • Installation (Unity Hub, Unity, Visual Studio)
  • C# and Unity Fundamentals
  • Physics 2D Mini-Game 

(Note that you can skip beginner class if you already have a fundamental knowledge of Unity and C# )

First, you will learn how to correctly set up your 2D projects and get familiar with the Unity interface.

In the C# and Unity Fundamentals section, we dive into C#, an object-oriented programming language that is the standard for game development in Unity. You will learn everything about:

  • Classes
  • Functions
  • Variables
  • Conditions (if,switch statements)
  • Loops
  • Arrays
  • Access Modifiers

Then we apply that knowledge to make Physics 2D Mini-Game in which we also learn about the Physics Engine. You will learn everything about physics forces, gravity, collision matrix, layers, Rigidbody, and other Unity components. This section is also a slight introduction to the new input system. It's important to say that the Mini-Game section will teach you how to correctly move objects in Fixed Update and avoid common beginner mistakes. After you managed to complete all beginner sections and challenges, you are ready to continue with the 2D Platformer classes.

The 2D platformer class (second part) is the main part of the course in which you learn how to fully utilize the new input system with controller support.

The class will also start as beginner-friendly but it will slowly lead you into advanced programming techniques and solving complex tasks (melee attack, climb ladders, inheritance, virtual functions, lists, singleton pattern GameManager, etc). It will give you a foundation and a great concept on which you can build your games.

In the end, make your games stand out with 2D lights and post-processing effects (third part/class)

If you get stuck anywhere or need help, I respond to all messages. 

You can also join the community on the Discord server and connect with other enrolled students. Also, don't forget that this course will expand over time and you can vote for the next lessons.

Trust me, you won't be disappointed.

Thank you for your time and see you in the classes.

Meet Your Teacher

Teacher Profile Image

Mario Korov

Game Developer | Teacher | Mathematician


Hello everyone, my name is Mario and I'm a self-taught game developer with a Bachelor's degree in Mathematics from the Department of Mathematics, Osijek. Through six years of freelancing, finding quality and short tutorials was my major stepping stone, so I decided to put all my knowledge and passion into unique, straight-to-the-point videos to help other developers. You will save so much time, learn faster, and in the end make a game faster.

To sum up, all my love and passion are in Unity courses with the goal to help you in your game developer path. Thank you and see you in the lessons.

I'm really looking forward to this journey.

See full profile

Class Ratings

Expectations Met?
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Introduction: Hi everyone and welcome to the unity, to the master course. The course is made in Unity 2020, and this class is the first part of the course. This class is for beginners in programming in Unity, you will learn how to set up 2D projects and get to know the Unity interface or so, we'll learn C-sharp fundamentals that are essential for every game. Knowledge we applied to make your first simple to the mini game in which you also learned about the physics engine, how objects collide and have the correctly move objects. This mini game will show you the process of making games from start to finish. And it's a slight introduction to the platformers section. If you already have fundamental knowledge about C sharp and unity, you can start from part two. To sum up, in this class, you can expect to learn how to code in C Sharp from scratch and get familiar with the Unity engine in general. Thank you for your time and see you in the class. 2. What is Unity Hub?: What is the Unity Hub and why to use it? Unity Hub is a management tool that allows you to install multiple Unity editor versions. And it gives you easy control over your projects. As you know, Unity has many versions. In each version has its average, those and so on. For example, maybe we want to develop a PC game in the 2019 version because of the long-term support that unity provides. And in the latest version, we want to experiment with the new features and learn new things. This is where Unity Hub comes in handy with Unity Hub, we can install and manage these unity versions. Also, we can add modules in by death, I mean support for Linux and mobile development just for diversion, we want another example. If you are developing again inversion 2020 and version 21 just came up and it has some new features that will make your game better. You can upgrade your project and continue to develop your game in the New Unity version. Although if the gap between inversions is too big, I wouldn't recommend it because of possible errors. It's good practice to make backup before upgrading your project. For us the basic information about unity hump. In the next video, I will share with the installation a registration process. 3. Install Unity Hub: In this video, I will show you how to download, install Unity Hub and go to your account registration. First open your search engine and search unity. Try to find page download. Unity usually is the first one. This is how the page looks. Now, unity may change it over time, but you want to find the download Unity Hub button. After downloading is complete, you can start the installation and it's a standard procedure like any other program or game, except terms and choose a destination for the files. Once the installation is complete, run Unity Hub and you should see something like this. It's a license window. It says you have no valid license to get a license, a free license, of course, we need to create unique TID. Go to Sign In button. It will give you the option to sign in if you already have Unity ID. If not, you need to create one. Feeling the information in create Unity ID. Also even need to confirm your email. Now, you should be logged in. You can see it by our initials in the upper-right corner. The next step is to manage the license, go to activate new license and a new window should open. Unity plus or pro are paid versions, so you don't need them now, you should choose one of two options from Unity personal. If you are planning to make commercial games, choose the first option. And the last step under the general window, you can choose a very Unity Hub will install Unity Editor. And that's it. You're successfully created Unity ID and activated the license. In the next video, we will install Unity Editor and Visual Studio. 4. Install Unity and Visual Studio: After registration, if you want to start a new project, you will need Unity editor. So under installs, click Add, and the new window will pop out. Here. You can choose which version of Unity you want to install. Select the latest official version. For me, it's 2020, 0.1 and something maybe there will be long-term support for debt version by the time you start with the course. So if there is a 2020 version with LTS, choose that. The next step is to choose the modules you want to install. You will need Visual Studio for this course. We don't need Android and ios support, but if you want to make games for Linux and Mac, choose Linux and Mac build support. I will skip that for now. Also, you can include documentation so you can use it even without an Internet connection. When you selected all the modules you want, and don't worry, you can easily add them later if you miss something, just start the installation. Unity Hub, your first download Unity, installed it, and then do the same for Visual Studio. After installation, if you ever needed to add modules for Unity version, you want click on these three dots and aid modules. Then select modules you want to add. If you already have other unity versions installed, you can easily add them to Unity Hub, click on locate, find installation folder, and select Unity Editor. At this time, I don't have any other unity versions. Now you've got everything to follow this course. In the next chapter, we will start with the first project, Anglo through Unity and C sharp basics. 5. First Project: Now, when we have everything to start a new project, go to the project window. Currently, there are no projects. You can add some of your old projects or create a new one by clicking the New button. And then by clicking just near the New button, you can choose in which version you want to start a project. For now, I have only one version. In the new window, you should see some project presets that can also change with time as Unity updates Unity Hub, but we want to select a 2D project since we will make 2D games. Give it a name in the choose the location for your project or limit as it is. Click Create. Now Unity, we'll set up your project. Let's go to full screen. I won't do the Update now. This layout to Logs. Let's go through the most important windows so you can get an idea of how to work with Unity. Here is the project window under assets, we usually import all sprites, sounds, create folders, and scripts is good for the organization of your files. Then we put some of the sprites in the scene and make game objects like players, enemies, and so on. The scene view is like your game world. Here GameObjects heard their position, visual representation, and more. Every game object is listed under the hierarchy. By default, only the main camera is in the scene. If we click on it in the inspector window, we can see details about the object. In this case, the main camera inspector shows components and their properties, such as position, rotation, and scale from the transform component. The next window is the game window. It will show you the view from your camera in the actual game. By pressing the Play button, you can simulate your game and play it to see changes you made. For now, we just see the blue background from the camera. Click again to exit the play mode. The next window is console. Here we can see messages, alerts, and errors from our scripts. Before we go to the next three though, if you want to manually start the update from the beginning of the video, go to help check for updates. In the next video, we will make a first game object in the script. 6. First Script: Before we create a script, and let's create a folder so we keep things nice and organized in the project window under assets, right-click create a folder. I will call it Scripts. In the scripts folder, right-click create C-sharp script. Let's call it general information. Before we open it, check if Unity recognizes Visual Studio as an extension. Go to Edit, Preferences, external tools, and choose Visual Studio for the script editor. Now we can open the general information script. If it's your first time opening a script, you will need to create or login into a Microsoft account. Next, choose your color mode. I will go for the dark mode is better for the eyes. And this is how our default script templates should look. As you can see, by creating a script general information, we created a class, a general information more about the classes in the next lessons, the most common mistake for the new users is to rename a script. The class name and the name of the script must be the same. To rename the script, slowly double-click it, or right-click and choose rename. And if you do it, remember to change the name of the class in the Visual Studio, otherwise, you will get errors. As you can see by default, there are two functions, start and update more about functions later. But now you should just know that start function will be called just once when we start the scene. That means when we press the Play button, when I say that a start function will be called, that means that every line of code between these two curly brackets will be executed from top to bottom. The update will be called or executed once per frame, which means many times over and over. To demonstrate that, let's use a debug dot log so we can print some messages to the console. Right? Debow dotCloud parenthesis, and use quotation marks for the text we want to see in the console, a text always needs to be between quotation marks. Also put a semicolon at the end of the line. Let's add a debug love in the update function. Now we have two lines of code, one in the start and one in the update. We need to save the script. You can also press Control S. And let's go back to Unity and press play. And nothing happens. We did a second common mistake within it. Attach the script to a GameObject. Let's select the main camera and aid the general information script. Click on Add Component and type general information squared. Now press play. Click on a collapse to group the same messages and to get a better view. As you can see, the message from the start function was called once. And every frame we print our message from the update function. Press the button again to exit the play mode. To remove a script from the game object, click on three vertical dots, also called More Options menu, and remove the script. Also, you can manually erase the messages from the console by clicking on the Clear button. And by default, they will clear every time we press Play or build a game. Now, when we remove the general information script from the main camera and let's edit to a new game object under hierarchy, Right-click and create empty. This will create a game object in the scene. We can also create a new object by clicking the plus button. Notice how our new game object has only the transform component. Renamed object, how you want. And also there is a possibility of adding an icon to your object so you can see it in the scene. To add a script, we can repeat the process by clicking Add Component, or we can just drag and drop it. Press play. And you should see the messages in the console. Here is the reminder for this video. In the next video, we will learn about variables. 7. Variables: We use variables when we wanted to store information. You can imagine a variable as a box in which you can put some data in when you need it. Just open the box and use data that we stored before. Of course, as there are many types of boxes, there are many types of variables. Imagine a different type of box as different type of variable. So in a square box, you can put just a square information in the round books. You can put just around information. We can conclude that variable type indicates what type of information we want to store. In C-Sharp, we use four main types. Integer, float, string, end bowl. An integer or int is a whole number. It can be positive or negative. Afloat is a decimal number. Also it can be positive or negative. Afloat can also store whole numbers, but int is faster. So try to use float just when you need a decimal number. A string is a text such as the classic Helloworld. We write text inside quotation marks and note that any number inside the quotation marks is considered as text, not as integer or a float. Ball has only two options. It could be true or false. Let's go through the syntax of variables. First, we write the variable type, for example, int. Next is the name of the variable. Usually it's something that describes the information we want to store. Then we write an equal sign followed by the value we want to assign to the variable. And of course, a semicolon at the end. Now, let's use variables in the script. Open your script. I want to store personal information about me and printed to the console about the start function. Let's create a string, my name. Now, write equals sign quotation marks. In your case, your name and semicolon at the end. Outside of the quotation marks. You don't put a semicolon at the end. You will see one error notification in the bottom left corner. To open more detailed information about errors. Go to View and select the error list. Double-click it and it will show you various the error exactly. Let's use an integer to store my age. With float, we can store my height when storing flows and f after the number value. That is just the syntax. With the bowl. I can set my superpowers to false. Maybe you will set it to true. I don't know. Now, when we have variables, let's use them. In DeBow Datalog. I will just comment the box from before using a double slash, so we don't see them in the console anymore. In the new debug dot log, write some texts. My name is Anne, outside of the quotation marks, right? Plus sign followed by the variable my name. With a plus sign. We in some way merge texts in the variable. Let's see it in the console. Great. We can continue our new sentence and add another plus sign, some text in quotation marks. The plus sign and the variable my age. Press play in Unity. This is the printers out in the console. Also, we can debug just the variable we want. For example, debug dot log superpowers. Now in the console, you should see true or false depending on your superpower variable. Press play in Unity. Great. Also, you can see in the console there is a warning that we assigned my height variable, but we are not using it anywhere to hide or show messages, warnings, or errors. Toggle these buttons in the upper right console window. Here is the reminder for this video. The next video is about functions. 8. Functions: Functions are forms that group together code that performs a specific task. They are very useful when you need to repeat a piece of code in many places and many times, maybe you heard the expression method. Methods and functions are the same. I usually say functions because it's closer to my native language. Let's see how we define a function. First, we write our return type in this example, void, then the name of the function, usually it's something that describes what the function does. Then we open and close parenthesis. Inside the parenthesis, we can add parameters to our function for now, leave it empty. Next, we need the curly brackets. And between curly brackets, we write lines of code that defines what this function will do. Maybe you've heard the expression, write a statement. A statement is just another word for line of code. So in other words, we write statements between curly brackets. Let's talk about the return type. In this example, we use void, which means there is no return value. The next question is, what does that mean? No return value. Imagine if I give you some homework and you do it in the void function, you don't need to give back the result of your homework. To me. You will do the homework, make calculations, but you will not return it to me. I will not see it. If it's some other return type as int or float, you will give me back the result, which means the function will return a certain type. I hope it's understandable enough, but you'll receive in examples. Let's create a function in the general information script inside the class, but outside of start and update, right? Void personal information. This will be the name for our function. Some parentheses and curly brackets. Inside the curly brackets. And let's copy debug logs. Press Control X to cut and Control V to paste them inside. If we press Play in unity, nothing will happen. There are no messages in the console. Just by defining a function, we are not doing anything. We need to call a function. That means we need to decide when and where to execute statements. For example, I want to print messages when we press the Play button. That means we need to call the personal information function in the start. To call a function, write the name of the function, followed by the parenthesis semicolon. Now, when we press play, these lines of code will be executed. And here is the message in the console. Great. Note that we can call a function as many times as we want. Let's call it two more times. Functions are very useful when we need to repeat a piece of code. Here is the print result three times my name in superpowers. But in this case, it's not fun to call a function that will print the same name every time. This is great opportunity to use parameters. Inside the parenthesis. We write a type of the parameter and the name. Also, we can add more parameters separated by the comma. Now we have three parameters or variables that exist only inside the function. Which means between these curly brackets, we can't use them outside. Let's use those parameters in debug dot log. Replace my name with the local name, my age with age and superpowers with powers. Notice that we have errors now, that is because when we call a function that in definition has parameters, we must provide those types of parameters. We need to paste that information to the function. With every call, our function remains three variables. The variables we provide when calling a function are called arguments. In definition, they are parameters. When calling a function, they are arguments. So in parenthesis, we need to paste the correct variable types in the correct order. First is string, my name, then integer, my age. Then bowl superpowers. In the second call, instead of declaring n using a variable, I can just write string Bateman, integer 40 and false, no superpowers. The third call will be Superman. He's 36 and he has superpowers. What will happen when we press the play button with every call, these parameters will get the values that we write in parenthesis when calling the function. In the first call, local name will get my name value and use it in the bulk dot log. In the second call, local name will get the string Bateman is available and use it in debug dot log, and so on. Let's see it in the console. Nice. The function was called three times. You can see the information about me, Batman and Superman. Now we can create another type of function, a function that returns a value, right? Int double number, parenthesis, int num, parameter, curly brackets. And the key word we need is a return. Next we write what the function will return. Let's say we wanted to return parameter num multiplied by 2, place a semicolon at the end. This is just a simple example to see the difference between void return type. The return types have keyword return and return value. Also, we can call functions in debug dot log. In the start function, write debug dot log double number. And let's say we want to double number seven. The result will be 14. Press Play. And here is the result, 14. This is all for this video. Next is the first challenge for you. Let's wrap up things here. 9. Challenge- Functions: Here is the first challenge for you in the new script tried to make two functions. One function needs to return a product of two whole numbers. The second function needs to print the addition of three decimal numbers. The functions need to be called in the start function two times each. The solution to this challenge is in the next challenge. Review. Good luck with the challenge and see you in the next video. 10. Challenge Review- Functions: Let's create a new script in the scripts folder. The name will be function solution. It's up to you how you want to call it. Double-click it to open it. The first function needs to multiply two whole numbers and return the result. Right, int. Because we want to return int, the name can be multiplication. And in the parenthesis we need two int parameters. Now you can type in between curly brackets, returned a times b, use an asterisk for multiplication and the semicolon at the end is that simple. This function will multiply a and b and return the result. You can also make another variable and store the multiplication result in the variable c and then return the C variable is the same thing. Just a bit more work. In the next function, we don't need to return the result. So the return type will be void, void addition, three float parameters. And we can make another variable inside the function, stored the result and printed to the console with debug dot log. Now let's call functions in the start two times each. Say the script. Don't forget to attach the script. I will disable general information script by unchecking the box. Now, only the function solution scripts will run in the console. There will be just printed the results from the addition function. If you want to see the result of the multiplication function, we can debug it, right? The multiplication functions in debug logs. Now you should see all the results printed in the console. And that's it. This is the solution for the first challenge. I hope it wasn't too hard. Feel free to continue with the course. 11. Classes: To understand classes, let's talk about c-sharp. C-sharp is 0 p, which is short for object oriented programming language in OOP. And that means in C Sharp, we describe objects with code to describe them, we use classes. You can imagine a class like a blueprint of a specific object. Let's say we want to create a car in again, in that case, we would create the car class that describes a car. For example, we can describe a car with some variables like color and speed. Then we can add functionality to our car using functions, what car can do, it can accelerate, it can use a break, and so on. Now when we have a blueprint of a car, we can create a Car objects in a game. Creating an object using a class is called creating an instance of a class. Here is the table with examples. A car is a class and objects or instances in a game are different types of cars. You get the idea. The syntax is simple. We write class followed by the name of the class and curly brackets. When we create a script, unity will automatically create a class. However, it will add an access modifier. We talk about them in the next three days and it will add mono behavior. This is just unit the application programming interface or API. This column between the class name in mono behavior means that our class is a child of the mono behavior. We say that our class derives from 10 behavior, more about inheritance, parent and child closest later in the course. For now, just remember that functions like start and outdated come from the mono behavior and we can use them in our classes in our project by creating the script general information. We created the class general information. Then we added that general information script to a game object named first object. Doing so, we created an instance of the class. If we put that into a table, general information is a class. And on the first object, we created an instance of that class. Currently, we are using it to print some information about us. But what if we want to create more instances that printed different information about different persons, different names, age, height, and so on. To do that, we need to change the variables for each instance. How to do that? First, let's create two GameObjects with the general information script. Rename this GameObject. And the script. And press Control D to duplicate the gameObject, renamed the third game object. And now we have three GameObjects with general information script. Don't forget to enable the general information script on the first game object and disable the function solution script. Let's take a look at our screen. I will quickly comment unnecessary code by selecting it and pressing the Command button. Notice that the personal information function takes these variables as arguments. We need to change these variables for each instance, so we get different results when calling the personal information function. If I just change a variable like this, that will change it for all created instances. For the first, second, third, object. To change variables separately. For each instance, we need to know something about access modifiers, which I will explain in the next lesson. Here is the reminder for classes. 12. Access Modifiers: An access modifier is a keyword placed before a data type of variables, functions, or classes. Most of the time we use private and public access modifiers. They define the accessibility of those variables, functions, and classes. What does it mean that they define accessibility? That means they determine where you can see and use variables and functions. For example, variables, sweet private access modifier can be seen in change only inside a class in which they were created other scripts. And that means crisis again, see and edit those private variables. In C Sharp, our default access modifier is private, which means if we don't write an access modifier, variables would be private. And this, and this are the same, but it's always good practice to write access modifiers in the general information script. We don't have access modifiers, which means that variables are private. So we can't see private variables in the Inspector. Notice that we don't see variables under the general information script. There is one way to see them, but you can't edit them on the game object, click on the More Options button and select a debug mode. Now, you can see the private variables, but you can't change them here in the inspector. This is good for debugging and monitoring your private variables. But for now, let's go back to normal mode. On the other hand, variables with the public access modifier are shown in the Inspector. Let's make our variables public it public access modifiers before data type. Now in the inspector, we can see the variables and change them for each instance. Let's quickly do that. Press play, and in the console will be three different results, one from each instance. Also, public variables and functions from one script can be seen in changed from another script. Communication between scripts is something that you will often use in your game development. Let's create two scripts, place a and class B. In class a, let's create two variables, one private and one public. Also create two on public and private function. Just insert some debug log that prints text. Open clays be. Here, we need to store a reference to another script. More precisely, reference to an instance of a class. In this example, class a. We do that by writing public. Next is the name of the script we want to access, and then the name we want to use for reference. Note that reference to the instance of the class a is variable, which type is the name of the script? If you take a look at the syntax, we have access modifier, data type, and the name or the variable. Now we have access to variable and function from the class a by variety in reference to class a. That, and here we can see the public function example public and exemple public variable. Choose the function, put the semicolon at the end. And note that we can't see or use private variables from class a, only the public ones. We can see the public variable from the class a. And let's use it in the debug log. Go back to Unity. Disable stuff that we don't need because we don't want a mess in the console. Create two GameObjects. One needs to have class a and other clays be. Press Play. And we got one error, null, reference error. Why is that? Double-click it and you will see that error is in line 10 when we are calling the function from the reference which is not assigned. If we take a look at object and B on which is the script class B, the variable reference is empty. Now, in other words, we didn't assign an instance. We need to tell unity which instance we are using. And we want to use instance on object a. Just drag and drop objects a. And you can see that we are now using class a from debt object press play a. Now you can see the printer result in the console. For the next example, we can duplicate this object a and use it for reference. You can conclude that you need to specify which instance unity needs to use. Quick tip. If you have problems dragging and dropping objects, you can select Object B and click on the lock. When you select another object, the inspector will still show the locked object. Press Play. Great, Let's wrap up things for now. Here are the key pieces of information from this video. 13. Execution Order in Scripts: Let's go through the execution order to see in which order unity calls functions. That is very important to understand. The link to this page is in the resource folder. I will go through the most important functions. If we take a look at the execution order, you can see the start function. But as you can see, it's not called first actually is the awake function that is called first. These functions are called only once and then we go to the fixed object unit. The physics engine works with fixed update to simulate all physical forces and it's called on a fixed timer. This means it doesn't depend on the frame rate. This ensures the smooth movement of the objects. Then we have ONE trigger and on collision functions that we will often use. And here you can see the physics is going back to fixed update. That is one physical cycle independent or the frame rate. Most of the time it's called more than once per frame. Then we have the update function. The update function is called once per frame. Then we have some animation stuff, late update in rendering. Then we have the end of a frame. After death, we have another lobe is going way back to the fixed update is the start of a new frame. Basically is going through all this stuff. Again, you can see we have one big loop, four frames in one smaller loop for physics. After the frame, we have some functions when we disable or destroy a script. Let's see how we write some of those functions in the script. This is just an empty script. By default, we have start and update functions. For example, we can add the awake function. You can place it above or below the start function. It doesn't matter. The execution order is always the same. Awake is called before start. Generally, it's good to write functions in the order in which they are called. It makes it more organized. Also, in the same way, we can add a fixed update and other functions. Inside every function, the execution order is from top to bottom line by line. On the official Unity documentation site, you can see more detailed explanations. For the start. I suggest you to read first scene aloud before the first frame, update and update order sections. They are the most important for now. When you finish checking documentation, you can continue to the next lesson. 14. New Scene and Layout: Before we continue VC sharp, Let's create a new scene. Here. We have many game objects that we don't need anymore. We can delete them with the delete button. But to save previous work, Let's create a new scene. Go to File here. You can save your scene if you didn't yet. Click on New seeing. Notice that in the new scene and there are no objects from the previous one. The main camera is here by default, to understand the scenes, it's important to understand how games are made in Unity. You can imagine one seam is one level of your game. It's not the role, but usually we want to seen is a one level. Then we connect the scenes with scripts and we have again. Now, let's talk about a layout. At this point, you know, basic Unity editor windows, and for what we use them, you can move and customize windows as you want. In the upper right corner, you can choose one of the layout presets. Also, you can make a custom layout and save it. Feel free to use any layout through this course. There will be more editor windows that we will discover later in the course. And most of the time, I will use the default layout press Control S to save the scene in the Scenes folder. Now, we can continue with C sharp in the next video. 15. Conditions + [Mini Challenge]: In this video, we learned about if and switch statements. With the if statement, we check for a condition and execute different codes depending on if the condition is true or false. Let's take a look at the syntax. We write if, then some parentheses and curly brackets inside the parenthesis. We put our condition. For example, we can check if the ball variable superpower is true by writing superpower, the double equals sign and chew. If the bowl variable superpower is set to show the condition is met and everything inside curly brackets will be executed. In case superpower is false and the condition isn't met, we can use the else statement below curly brackets, right? Else followed by additional curly brackets. Notice that we used operator double equals to determine this condition. We can use different operators to set different conditions. Here is the list of operators. The double equals mean is equal to exclamation mark qubit equals sign means is not equal to. If we are working with numbers, we can use the greater than or less than signs to compare them. If we put an additional equals sign, it means greater than or equal to, then we have a less than or equal to. Let's try this in a script. Create a new script called conditions, and edit to the game object. Create the bowl superpower variable and set it to true. In the start function, we can write an if else statement, if then parenthesis with condition inside curly brackets, the backlog. And else statement with curly brackets. If we try this in Unity, this is the message in the console. To keep your code organized, place the statements inside a function. Great, much better. In a similar way, we can check other variables. Let's create public int variable speed. For example, if speed is greater than 0, then print VAR going forward. Or else we are going backward. Don't forget to call the function in the inspector, set speed to any positive number. Press Play. Great. You can see the message. We are going forward. If we set the speed to a negative number, we will get a different message. But what if we set the speed to 0? We don't want to print, we are going backwards. If the speed is 0. To resolve it, we will use the else if statement below the if statement, right? Else if parenthesis, if the speed is equal to 0, bring standing. Now we have three cases, negative, positive speed and no speed case. We can add as many else if statements as we want. Let's try it in Unity. Speed is 0 and we got the right message. One more thing about if statements, it's possible to use if statements inside another if statement. Also, you need to remember that only one of the statements can be executed. For example, here we have the variable speed and two statements. Both conditions are met. Five is greater than 04, but only the first condition that is made will be executed and others will be ignored. So be careful when constructing your statements. Now, let's take a look at the switch statement. The switch statement is more specific and a bit limited in comparison to the if statement, usually I use the switch statement when I have too many elseif statements or I know the exact values of variables. Let's make one int, variable grade, and a function for the switch statement. To use the switch statement, we write switch parenthesis, the variable we want to check in curly brackets. Inside curly brackets, we read specific cases. We are using variable grade and in my country, grades are from one to five. So we write case one followed by the colon. That means if the grade is one, we execute the code below Case 1. We can use the bug in, then write a break followed by a semicolon. Brake marks the end of case one and it will stop further checking of cases. Note that it's possible to write multiple lines of code in cases. Let's create the form our cases for grades. If the grade is any number other than one to five, for example, ten. We covered that with a default case. Don't forget to call the function. Let's try it in Unity. We can check with the grade five. Nice. Here is the mini challenge. Tried to write a switch statement to replace the first if statement. Pause the lesson and tried to convert IF statement to switch. Here is the solution. We want to check the superpower variable. Since it's a bowl variable, we have two cases, true or false. And that's it. Conditions are something that we use all the time, so it's important to understand them. Here is the reminder to help you. 16. Loops: We use loops when we want to repeat a piece of code and we repeat it as long as a certain condition is met, loops save you time and make your code more readable. In this video, we'll learn about for loops and while loops. For example, if you want to print numbers from one to five, you can do it by writing debug five times or use one of the loops. In this case, we will use the for loop because we know exactly how many times you want to repeat Tableau code. It's five times to print five numbers. Let's take a look at the syntax. We write for parenthesis, followed by curly brackets. Inside parenthesis, we write three statements. The first statement, we set the variable that we usually use as a counter for loop steps. This statement happens only once and before the loop starts. The variable that we defined is called an iterator. Next, we write the condition that needs to be met in order for the loop to continue. We want our loop to write the first five numbers. So we said the condition to I less or equal than five, followed by a semicolon. If the condition is met, the loop will continue as ACLU code between curly brackets, that is the parts of that for loop repeats. One repeat cycle is called iteration. Here we want to repeat the debug log that will print iterator. The last thing we need to do is to increase the iterator. So it's not one wit every print. Remember, we want to print the numbers from one to five. After each iteration of for loop, we'll isolate the third statement. And that statement, we change our iterator depending on what we need. In this situation, we need to increase it by one for each iteration. The short notation for this is I plus plus. And let's see how this loop works step-by-step. We set the variable to one. Usually we start from 0, but for this example, it's okay to start from one. This happens only once, and then we check the condition one is less than five, we can continue. Next. Print iterator I value is one, then increase the iterator by one. Again, check the condition two is less than five, print the iterator and again increase it by one. We keep doing this until the iterator gets a value of 66 is not less or equal to 5. The condition is not met and we exit the loop. Now, let's show it in a script. Create Script tubes, and edit to a game object. Create one function for the lobe. And inside the create the for loop from the previous example. Be careful about the syntax. It's a bit tricky at the beginning, called the function in the start. In Unity, press play. In the console, you should see the first five numbers. There are many possibilities fit for loops. For example, we can increase step through the loop by two instead of one. Or we can decrease it. We can change the condition and iterator value as we need. Take a moment and try to guess what will this loop print? Let's see it in the console. Now, let's take a look at the while loop. The while loop is designed to repeat an action as long as a certain condition is met. Usually, we use the while loop when we don't know how many times the loop is going to run. The syntax is simple. We have write a vile, parentheses and curly brackets. Inside parenthesis, we put a condition as long as the condition is met, the while loop will repeat code between curly brackets. Here you need to be careful because if the condition is always made, the while loop will never stop and it will crash your program. Trust me, it happens sometimes. Let's say we have a timer and we want to decrease it to 0, create a variable timer and set it to any positive number you want in the start function, right? Parenthesis. And we want to repeat code as long as the timer is greater or equal to 0. Between curly brackets, print the value of the timer and decrease it by one every time we repeated the slope. Save the script. Go to Unity and press play. You can see that the timer was decreasing as long as the condition was met when the timer value became less than 0, the while loop stopped. As you can see in the print result, there are no negative numbers. Great. Now you know something about loops. It's time for a challenge. Check the remainder and in Good luck. 17. Challenge + Review- Loops: For this challenge, try to make clothes that will print even and odd numbers up to 10. Also try to make a loop that will print even numbers in the reverse order from 10 to 0. There is more than one solution for this challenge, I will show you one, but if your solution is different and the prince the same results, It's fine. First, let's check the solution for even numbers. We start from 0, set the condition to less than 11, print iterator, and increase it by two. For odd numbers, we can start from one because it's the first odd number. Print i, as long as I is less than 10, because nine is the last odd number in the range from one to ten. In the last loop, we start from 10 and decrease the value as long as I is greater or equal to 0. I hope you all manage to solve the challenge and that you are ready for the next year. 18. Arrays: If we want to store multiple values of the same type, instead of writing variables individually, we can use arrays. An array is a collection of variables of the same type. We use arrays to store multiple values in a single variable. There are a few ways how to store variables in the array. One of them is to write the type of variables we want to store. In this example. It's a string. Then open and closed square brackets followed by the name of the array. Then we write an equals sign and curly brackets with a semicolon at the end. Inside curly brackets, we put the values we want to store separated by commas. Values stored in the array are often called elements. Each element we store in the array, we'll get an index number starting from 0. So the first element has an index of 0, the second element and index of one, and so on. We use index numbers to access elements of the array. For example, if we want to print something with Ironman, we write the name of the array square brackets with an index number inside. It's important to remember that index numbers start from 0. Another information we often use, usually in combination with lopes is RI, link. Array. Length is the number of elements in the array. So this array has a length of four because it has four elements. Note that when you make an array and set length, you can't change it anymore. It's not the dynamic. The list is dynamic and it can change in length or size more about ArrayLists later in the course. And let's see how arrays work in the script. Make another script arrays and edit to a new game object. Create string array by variety of string square brackets. The name avengers equals sign, curly brackets and some adventures inside curly brackets with the semicolon at the end. Let's create another array. As I said, there is more than one way to create an array. Now to the cross sign, we write the keyword new, followed by the type of the array and the number of elements in square brackets. In the start function, we can add elements by writing the name of the array and the square brackets with the index we want, I want to add the first element, so I will use the index 0. Next we write an equal sign and the value we want to store array avenger still needs to have three elements. So let's edit two more. Let's say we want to print all elements from the Avengers array. We can do it individually, but it's not practical. Usually we combine the loops with arrays. And let's make one for loop. It goes from 0. And remember that the index of the array also starts from 0. We know that array has four elements, so we can write I less than 4, but we can also use array length. This is better solution because if I want to remove an element from the array, I don't need to go back in the for loop and change the condition. Then we increase the iterator by one. And that is how we go through the elements in the array. The iterator will go from 0 to three. So we use the iterator as an index. In the backlog, we print elements by writing the name of the array and index number i in square brackets. Also, we can make a public array. In this example, array of ints. In the Inspector, we choose the number of elements and add values. There is one more loop that we often use with arrays. It's for each loop, it will go through each element in the selected array. In this situation, for each int in the random number array, the syntax is a bit different, but nothing complicated. We have a variable that has to be the same type as elements in the array keyword in and array. Now, we can print every element with debug log. You can test this in unity and go to the next video after the reminder. 19. Unity Documentation: Let's see how you can use Unity documentation, which is great by the way. Always read the documentation because you can't learn everything in lessons or YouTube tutorials. So for example, if you want to know more about the transform component, click on any object in the scene. In the Inspector, find the transform component and click on the question mark icon. That will open unities official documentation about the transform component. Here you can find all details you need. Also, it's important to use the correct version of the documentation. In the upper left corner, you should see what version you're reading. Make sure that the documentation version, Mencius, your Unity version. For example, when you use Google to find documentation, usually you will get the latest version, but you will also get a drop-down menu to select the version unit. And that's it. Again, my recommendation is that you always read the documentation because you can find great and useful information. See you in the next video. 20. Project Setup and Version Note: Quick Info, I updated the Unity version to 2020, point-to-point 1F1. So use that version or newer to start this mini game series, you need to create a new 2D project. And you can also create two empty folders, one for scripts and the other four Sprites. The purpose of this physics mini game series is to teach you about the unit of physics, engine and gravity, how objects collide, how to use layers for collision and so on. Also, we learn how to make controls using the new input system. It's just a slight introduction. You will also learn some fundamental stuff like using components in scripts, creating a prefab ends pounding objects using arrays. In the end, we export the game so you'll get a clear picture of how games are made in Unity. Everything you learn in this mini games section is necessary for every game you will make. This is the foundation. Once you have set up your project, feel free to move on to the next lesson. 21. Sprite Creator: Sometimes when we make games, art is not done by the time we need to use it to save time and to be more efficient, we use placeholders. Place holders are simple art shapes that will be replaced with real art later. But until then we can continue to develop our game using those simple shapes. And the cool thing is that Unity has its own simple sprite creator in the project window, in the sprites folder, Right-click. Create 2D sprites. And you can choose one from several shapes. And let's create a circle. You can rename it or leave a default name. For this lesson, we need at least one more shape. So create a square. Now, we can use those simple shapes in a game until real ones become available. Let's use the circle sprite to create a game object and just drag and drop it in the scene. As you can see in the hierarchy, we created the game mom should the name circle. And if you look in the inspector, there is one component we didn't see or mentioning it is the sprite renderer. Sprite renderer is responsible for the visual representation of a game object. It has many properties, color, material, layers, and so on. We will go into details later in the course. For now, just remember it's used for visual representations. If I disable it, I can't see the object anymore. Enable it again. If we press Play, the circle will not move. It will stand still and nothing will happen. What about the gravity and other physics forces? For that, we need another component which we add in the next video. Here is a quick reminder for this figure. 22. Rigidbody and Gravity: Unity has a physics engine that simulates physics through some components on game objects to simulate gravity and other forces, we use a Rigid Body 2D component. Select the game object. In a component. Rigid body 2D. Make sure you add a 2D component because there is a rigid body for 3D. When we attached a Rigid Body 2D to a game object, the unity physics engine starts to influence and control it. Let's just move the circle up and more to the middle. We can use the Move tool in the upper left corner. A shortcut is W, press Play, and now the game object falls into oblivion. You can see in the transform component that the position on y is decreasing, just a small digression in a 2D project, the x axis is horizontal, left and right, and y axis is vertical up and down. Exit the play mode. As you can see, the Rigidbody component has some properties and the gravity scale is one of them. There are many other properties like mass, material, body type, and so on. For now, let's talk about body types. There are three body types, dynamic, kinematic, and static. By default, when a 2D rigid body component is attached, the budget type is dynamic, which means that we can move it using scripts and also other forces can influence it, such as gravity or forces from other objects when we collide with them. In most cases, your player will be dynamic product type. Less common is our kinematic mode type. Kinematic body type also can move, but other forces can't influence this type of body. In other words, if you run straight, no one and nothing from the outside can change your direction or stop. You're not even our own. You can stop yourself with code in scripts. And example of a kinematic body type is a moving platform. You don't want other forces like gravity or collision with the player to influence your plate for a moment. But you want to move it in some direction from point a to be a perfect case of kinematic type. The aesthetic body is static. It's designed not to move. Also, gravity and other forces won't influence this body type. An example of aesthetic body type is the ground on which player a smooth. Let's create some ground. We can use a square shape, rename it to ground, and change the scale in the transform component, increase x scale by holding the left mouse button and move it to the right. Also, there are a few ways have to change the scale. You can use the scale or rectal a rigid body 2D and choose a steady body type. You can notice that the static type, it doesn't have the same properties as dynamic body. Let's move the ground so it's below the circle. Use the move tool again. Make sure the circle has dynamic budget type and press play. Again, and the circle is falling into oblivion. The ground didn't stop the circle. Why is that? We need to add another components called colliders. Death is our next three though. Here is a reminder for this one. 23. Colliders and Triggers: Colliders are components that are responsible for a physical collision between objects in order to collide with each other, game objects must have a collider component, and let's add one to the circle selected and go to Add Component. As you can see, there are many types and shapes of colliders, including 3D colliders. We need a 2D collider, in this case, a circle collider. Now you can see the green line around the circle. Maybe you ever see it better if I disable the sprite renderer. And the cool thing is that we can change the offset and radius of our collider. Colliders don't need to be exactly the same size as sprites. You can adjust them as you want. And let's reset the changes. Also, we can adjust the collider by clicking on the edit collider button. Now, I can easily change the radius while you are holding the left mouse button, hold out to change the radius without changing the center of the collider. Let's add another collider to the ground game object. Here we need a box collider 2D. Let me just quickly change the background color in the camera properties. Press play. And you can see the collision. Note that for the collision is enough collider on each game object and a rigid body on one of them. For example, we can remove the rigid body 2D from the ground and press Play. As you can see, the collision happened. There are cases when you need static rigid body 2D and when you don't more about that later in the course. Now, let's talk about triggers. Triggers enables objects to pass through them, but the collision is detected through scripts. Let's select the circle and set it to trigger. Now, the circle should fall without collision. Later in the course, we will see how to check and use triggers with scripts. And example of using triggers is a pick up. If a player goes through a pickup, it wants to blow him and collision will be detected in scripts. Now you know the fundamentals for colliders, and here is the reminder. 24. Collision Matrix and Layers: In Unity, we have layers for the camera, but we also have collisional layers with collisional areas. We have nice control over collision and physical interaction between objects before we start selling the circle and uncheck is triggered. So we have a nice collision between the circle and the ground. In the upper right corner, you will see a layer of section. Don't mix those layers. The layers in the sprite render and layers in the sprite render are for the camera. We call them sorting layers, and these are collisional layers. As you can see, there are five pre-made layers. Click on add layers to add another one. Let's call it player. Through the player collision layer, we can control what will collide with the player and what won't collide. Select the circle again and choose the player layer. Go to Edit Project Settings. Physics 2D. Here you can set your default gravity value for the whole project. And there are a bunch of other settings, but we are interested in the collision layer metrics. Let me just go to the full screen. As you can see, everything we'll collide with everything. If there is a checkmark between layers, they can collide. For example, the default layer will collide with the player UI water layer and so on. We can uncheck the collision between default and player layer. Make sure that the ground is on the default layer. Press Play. And as you can see, there is no collision. Go back to collision matrix. Set collision between layers to true. You can also add layers if you go to tags and layers. Also, here you can see options for sorting layers and tags. More about them later in the course. The next video is actually a challenge. So let's wrap up here. 25. Challenge- Collision: In this challenge, tried to create a new scene. In the new scene, you should recreate something like this. Create red and blue objects. Red objects collides Just to retread objects and blow of it. Well, the solution is in the challenge trivial. 26. Challenge Review- Collision: First, I will save the scene with a different name, so I don't need to create a game objects from the start. Now, I can create two collision layers. You can call them as you want for me, that will be blue and the red. Then I will change the color and duplicate again our job's select blue objects and add them to the blue layer, red on the red. The last step go to the collision matrix. Change collision in that way so that blue layer can collide only with blue layer and read iterate. Let's try it. Everything is good and that's it. See you in the next video. 27. Import Input Package: Let's import to the new unity input system package so we can move objects by pressing buttons. Go to Window, Package Manager, wait a bit until unit area pressures, packages. Now you can see packages that are already in the project by default because we selected the 2D project template and we need the input system package. So go to Unity Registry. This is the list of all packages from Unity search input system. And in the bottom right corner click Install. It will take some time. If you go back to in project packages. Now you can see the input system. Go to Edit, Project Settings player. And under active input handling, you can see that now we are using the new input system. Go back to the sample scene in the scripts folder, create a script, gather input, selling the circle and rename it to player, ed script to the player. Before we start, note, this is the simple use of the new input system in the bigger project, Tiamat platformer, we fully utilize it. But for a simple project like this, a simple input solution is okay. First, we want to use the input system namespace. Namespaces are a collection of specific classes and functions and we need special classes for input, right? Using Unity engine, that input system, see how the name is not white. It's a bit gray. When we use some class from the input system namespace, it will become white. Create one into variable that we will use a for the direction on the x-axis. And we want to move the player left and the right, that is the goal. So we need a positive value to go right and a negative value to go left. We check input in the update function because we want to check every frame if the player is pressing buttons, right? If keyboard, that current, that, Let's use the key on the keyboard that is pressed. Now, we can check if the key is pressed every frame, and if this is true, we set direction to one. Note that if you have only one line of code below the if statement, you don't need curly brackets. Next, we want direction value to go left. So in else-if we check if the key is pressed, set that direction to minus one. If we are not pressing a or D, then set direction to 0. In Unity, press Play. And as you press a or D direction, x value will change. As you can see, we are not moving. We're just changing some values. As we press buttons to actually move the player, we need to use the Rigid Body 2D component, and that is our next lesson, showed the remainder and see you soon. 28. Get Component and Move Player: To move the player, we need to get Rigid Body 2D component in the script. Let's create another script. Player move and edit to the player. In the script. Create one public flow to variable speed and Rigid Body 2D variable. With the rigid body 2D variable, we want to get access to some properties like velocity. Note that here we just created a variable of type rigid body 2D. To use it, we need to set up our reference. We need to tell unity, reach rigid body 2D we want to use because we can have more objects with the rigid body component in the start function, right? Rb, followed by the equal sign. Now, we want to get component rigid body 2D that sits on the same game object as this script. If you take a look at the inspector script and Rigid Body 2D are on the same GameObject. Because of that, we use Get component. Also see Get component in children, get component in parent and so on. Uh, later in the course, we use some of them. For now, as I said, we need get component because this script and the rigid body are on the same game object. Right angle brackets. And inside we write a type or the component We want access to. It needs to match with your variable. Then we write parenthesis followed by a semicolon. Next, we use fixed update instead of update. All physics calculations are done in fixed update by unity physics engine. This is really, really important in the update function. The time between frames can vary, sometimes is less, sometimes is more. That is why you have different FPS when playing games. The fixed update doesn't depend on the frame rate. It has a fixed timer. If you go to Project Settings under the time window, you can see fixed time-step. The fixed outlet is called every 0.02 seconds. You can change timestep, but usually default is good. What is the point to move objects with a physics engine in fixed update using a rigid body. Well, a fixed time step ensures that movement will be smooth and even so, use update to gather inputs and fixed update to apply moment with rigid body. The fixed update, we want to set the velocity of a rigid body. So we write RB, that velocity equals sign new vector two parentheses. And inside we said two values separated by one comma. The first value is the value for X axis left and the right. And the second is for Y axis up and down, we want to use speed on x and on y is 0. We don't want to move up or down. Don't forget a semicolon at the end. In Unity, change gravity to 0 so that the player doesn't fall. Press Play. And as you change the speed, the player will know for positive value to the right, four negative value to the left. Idea is to get direction x from the gather input script and use it in vector 2. As you know, a script is also component. So we can create the variable gather input AND gate component. Gather input. Now, we can get access to the public variable direction x in vector to multiply the speed with direction x from the Gator input script. In the inspector change speed and press play. Now when you press the achy direction x is negative, we go to the left with speed five. If we press the d key, we go to the right. Great. We have the simple controls for the player. 29. Scene Navigation and Setup: Let's create a frame around the player so we can restrict movement. To do that, we need to know how to navigate through a scene view and how to manipulate game objects. Let's go through some useful information. In the scene view, you can move around holding the Alt and left mouse button or just holding the mouse wheel to zoom in and out to eat the mouse wheel. Also, you can toggle gizmos. You can customize what I consume, want to see icons for the camera, colliders and so on. By default, the sea view is in 2D mode, but you can change it to 3D if needed. Next, we can toggle lights and audio, which we don't have right now. There is one new feature. We can hide game objects in the hierarchy, and let's hide the player. And now we can toggle hidden objects. Also, you can make a day mom should not selectable. Next, we can change grid opacity. In the game view, we can change the aspect ratio by default, it's free aspect. Set this to 16 to 9. Now the camera boundaries will change. Boundaries are these white lines in the scene view? Here we can change the scale to zoom into gave you. If you choose maximize on play, the game window will maximize when you press the Play button. Next, there are buttons to mute out. We'll see some states and gizmos. To make a frame, we need to manipulate to its size and position of GameObjects. Deleted this ground by selecting it and pressing Delete, create another square object. Reset the position. Now we can change the color in the sprite renderer. To manipulate game objects. We used tools in the left corner. Shortcuts are on the screen. We already mentioned some of them. Move it down and be aware of the camera boundaries, places just about the lifeline. Choose resize tool. And when you press the left mouse button on the edge, hold out to resize both sides at the same time. Control D to duplicate the gameObject, move it up. Then renewed gay mom juice for the sides, rotated by 90 degrees. Selected those for game objects and add a box collider. Now, we can collide rituals. That's it for this video. See you soon. 30. OnCollision Functions: Let's create a game object to that will collide with the player and disable his moment or collision. We can use a circle, changed size and color, and a rigid body 2D and increased gravity so that the object falls faster. Also, add the circle collider 2D. Press play, and you can see that the red circle will collide with the player. The next step is to disable a moment, go to the player moves script. We need to use the collision function. As you can see, we have on collision in Turkey, Udi. This function is called when this game object touches another gameObject with a collider. Then we have on collision exit 2D, it's called At the moment when this game object stops touching another game object. And the last function is on collision state to the dysfunction is called every frame as long as objects touch each other. We need on collision enter 2D because we want to stop player moment when the circle touches the player. You can see that function has one parameter, it's collision. Through the collision parameter, we can get some information about the object to be collide to it later individual, I will show you an example. For now, just set speed to 0 when we touch another object. Let's try it in unit two. Now, I can't move. But there is one problem with this. I will show you. I don't want to touch the red circle. It will stop my movement. So move it aside. Now, if we go touch the wall, it will also stopped my moment. How to get around this pretty simple. We need to specify to stop a moment only when we collide with the red circle. And let's rename the red circle and create one new layer. I will call it NMI. Set the circle to the enemy layer. Now in the script, we can store into number of the enemy layer. We get the number of the layer by writing layer mask, that name to layer parenthesis and then the name of the layer. So now when we have the number of the enemy layer inside, on collision enter, we can check if the object that collided with the player is from debt enemy layer. We use the collision parameter to get information about the game object and its layer in the if statement, right, collision and that gameObject dot layer double equals sign and an ML layer. Here, we check if the game object we collided with is from the enemy layer in Unity press Play and tried to touch the wall. As you can see, we can still move. But if we collide with the red circle from the enemy layer, speed will become 0 and we can't move anymore. That's it for this video. See you soon. 31. Prefab: If we wanted to create more enemy objects, we can duplicate them. But what if we want to create them at runtime while we play the game for debt, we use prefabs. Let's create a new folder. Prefabs, select the enemy circle in the hierarchy and drag it inside the folder. Now, we created a prefab set. It has all the components as NMI circle. Everything is stored in this prefab. Prefab are like a template from which you can create new instances in the scene. Just drag prefab in the scene to create instances. All instances are connected and if you select the prefab from the folder and change the color, all instances will change color. Also, we can select one prefab instance from the scene and change anything we want. If we want changes to affect other instances, we need to override them in the inspector. Some things we can change only in the prefab view. For example, let's create one child object. Right-click on one instance and create empty. That will create a child object. We can give it an icon. Now, if we move the parent object, the child will follow override for all instances. If I want to remove the child object, there will be one pop-out window. Click on Open prefab. That will open the prefab view. You can see that there is only enemy circle in the hierarchy. Here, we can delete the child object. Changes made in the prefab review will affect all instances in the game. There are a few ways to enter prefab view. We can use the arrow button next to the prefab in the hierarchy window. We can click on the Open button in the inspector, or we can double-click it in the project window. We use Prefabs to create objects at runtime, and also we can use them in all scenes. In the next video, we will use this prefab to span entry circus at the top of the frame. 32. Instantiate (create) objects: Let's delete this instance and create the game objects partner, ed scripts partner. In the script, we need a public variable gameObject that we want to span. To create an object at runtime, we use instantiate in the start function, right? Instantiate inside parentheses. First we choose the object we want to spout, and that is the enemy circle. Next, we need to choose the position and rotation of the object that we will create, right? Transform that position. We are getting the transform component and its position from the game object that has this script that respond enemy circle in the same position as the transform position of this partner object. Same, afford the rotation transform that rotation go to Unity. So we can set up a reference for the game object to a variable drug prefab from the project window to the game objects. Lot. Warn, warning. If you have prefab instances in the hierarchy, do not use them for reference, you must use prefab acid from the project to end up. There are some cases when we use prefab instances from the hierarchy. But for this example, we must use the prefab acid from the project window. Otherwise, you will get an error. Now, move this pounder about the player in that position and the reciprocal will be created. Great. Now let's create spawn points across the ceiling. Create a child gameObject. We can use the red icon. Use Move tool and hold Control to snap to grid. Actually, let's increase snapper value. Go to Edit grid and snap settings. Change the moon, increment, snap to one. Now continue to duplicate span of points. The idea is to randomly spawn enemy circle objects at these points. In the script comment instantiate function. We need an array of transforms for the span points. Also, we need a timer and spawn delay because we need some sort of control went to span objects. In the start function, set the timer value to Time.deltaTime. If you look in the description of the time, that time, you will see that it's the time in seconds since the start of the game. Then we want to add some spawn delay. I will explain it soon and just follow me for now. In the update function, we need IF statement, if timer it is less than Time.deltaTime. And then a spouse enemy circle and increase timer by spawn delay. Let's go through this logic. Let's say we play the game for five seconds and spawn delay is 1 second. At the start, we set the timer to five seconds and increase it by 1. The value or the timer is six. In the update every frame, we check if the timer is less than Time.deltaTime, which is the time since the start of the game. The timer is six and a time that time is five. Condition is not true. Nothing happens. Now, relate in the game, time passes and a time that time will increase. After more than 1 second, the timer value will be less than time. That time condition is met and we enter in the if statement. Here we do some stuff and immediately after we increase the timer by spout delay, which is 1 second. Now the timer is seven and that is greater than Time.deltaTime. Again, we read and repeat the process. This is not the most accurate timer. Maybe it's better to increase the timer in the same way as in the start. So you can replace this with this. Also, notice that there are more ways to create a timer. This is fine for this example. A random number. For that, we use a random that arrange. Set the first number to 0. And because the array index starts from 0 and for the last number, we can use this pound point length. This function will randomly choose one number from 0 to spout point the length minus one. As you can see in the description, the max value is exclusive. It can be chosen. So the maximum value is always released by one. This is great because if the length is five, the array has five transform elements. And index values are from 0 to four. By using the index, we can randomly access some element of the array. Now, we can instantiate enemy circle for the position. We use this pound point array and in the square brackets, we use a random index I. In other words, we choose PowerPoint from the array using a random index i. Next, we use a rotation from this GameObject. It's not important for this enzyme. In Unity, let's add the transforms to the array, alloc, the inspector, and select all spawn points. Drag and drop them to span point tare, set, span the LA to some value, and we are ready to play the game. We have our randoms partner, but as you can see, we need to destroy red circles as they touch the ground. This is material for the next video. See you soon. 33. OnTrigger and Destroy functions: To destroy circles that fall, we can use Trigger, select the bottom platform and set to the collider to trigger. Let's create a script destroyer script to the bottom platform. Here we want to use the own trigger function. As you can see, we have On Trigger Enter to stay and exit, same as the collision functions. But because we set the collider to trigger, we use a trigger functions. Let's use own Trigger, Enter to a D. And because we know that only the red circles can touch the trigger, we don't need any IF statement and conditions inside V destroy Kmart, just read the function, destroy. Inside parenthesis, we choose what object two we will destroy. We want to destroy objects that collide with this trigger. So we write collision that game object. In Unity. You can see that objects will be destroyed, but it looks a bit weird when we destroy them, the moment they touch their trigger, we can destroy them with a small delay in the destroy function at coma and delay time. Now, it looks much better. Objects go through the platform, a bit, collision is detected and we destroyed them after 0.5 seconds. That's it for this video, check the remainder. 34. Sorting Layers: As you know, the sprite renderer is responsible for the visual representation of 2D objects on the screen. Here I have one prefab instance. If you take a look at the sprite renderer under additional settings, we have a sorting layer. And order in that layer. With sorting layers, we determine what we see on the screen in front and what's in the bag, foreground, background and so on. The bottom plate form and a red circle are on the same layer in the same order. 0. Here is a possible problem in Unity editor. The circle is showing behind and that is good. But when you build your game EXE file, because they are on the same layer with the same order. The game can show the red circle in the front. Sometimes that can happen. And that's why we need to change the order in the layer or create another sorting layer. When objects are on the same layer, increasing order will move them in front. Decreasing the order will move them back. Now, when the order is minus1, we are sure that the circle will show behind the platform. We can also create a new sorting layer. Here, we can add and remove layers using the plus and minus buttons. Note that default layer can't be removed. Let's create enemy layer. Select the circle, changed the order to 0, and change the layer. Now the circle is in front, but it's on the enemy sorting layer. Let's go back to the list of layers. Here we can also change the order of the layers, move the enemy layer to the top of the list. Now the circle is behind. You can see the change. As I change the order. The layers will render from top to bottom, which means that the first layer on the list will be drawn at the beck. And indeed, you can see that circle is behind the plate for the last layer on the list will be drawn on top first on the screen. Keep in mind that there is another way to access the list of layers under Project Settings, tags, and layers. Also don't forget to override changes for all instances. Here is the reminder before the next video. 35. Exit The Game: We are almost done with the game. Of course, we need some way for the player to exit the game. In the gather input script, check if the escape key is pressed. You can use any other button you want. Then we use application that Quit function to exit the game. Note that if you test this in the Unity editor, nothing will happen. I am pressing the escape key and I can still play the game. However, it will work in the actual game when you build EXE file. This material for the next lesson. 36. Quick Export / Build: The last step for the physics mini game is to build an EXE file so we can run and play the game, go to File Build Settings. In the build settings. First you see since inbuilt area here you can see all scenes that will be included in your game. If you don't see any scenes, click on the Add Open scenes button and that will add your current open scene to the list. You can also see the built index associated with the scene. Later in the course, we use index number in scripts to change scenes. You can also add scenes by dragging them in the area. We don't have scripts to change scenes. So we can exclude this scene from the game by unchecking the box. Next, we choose the platform we need. In my case, this game will be for Windows. You can also choose architecture 32 or 64-bit. I will choose 64-bit. Now we can press the Build button. Create a folder where you want to build the game. Now, wait a few moments and you will have your game ready to play. Open it and enjoy. And if you can hit by the red circle, press the Escape key to exit the game. Let's say you discover some bug or you want to change something. For example, the white circle in some cases can touch the bottom platform and it will be destroyed. Then we can't exit the game because the gather input script is also destroyed. To fix that, we can freeze the position on y-axis, find the Rigid Body 2D component on the player. And under constraints, for his position on the y axis collides. It won't go down like before. Position on the y axis is always the same. Now, we need to build the game again to apply changes. Note that there are so many options for exporting the game that we didn't mention, like changing the icon splash screen at the start and so on. This is just to show you how to get a simple game working from Unity editor to EXE file. More details in the tile map platformers section. Congratulations on completing this mini game series. See you in the next chapter. 37. End of Part 1 and Discord Server: Congratulations on completing the first part of this course. Now you have fundamental knowledge about C-sharp and unity, and also you are ready for the next class, the 2D platformer. Here is the discourse server link. So you can join the community and connect with other developers. Have a nice day and see you in the next class.