Create 2D Target Shooting Game in 1 Hour

Raja Biswas
A free video tutorial from Raja Biswas
Charger Games founder , Passionate Learner & Teacher
4.4 instructor rating • 15 courses • 24,515 students

Lecture description

Create A 2D Target Shooting Game in Unity in 1 hour

Learn more from the full course

Unity By Example : 20+ Mini Projects in Unity

Learn Unity Game Development , Build 20+ Mini Projects Using Unity & C# . Learn Unity with this Project Based course

20:43:03 of on-demand video • Updated November 2020

  • Learn Unity The Fun Way by Building Mini Projects
  • Create 20+ Mini Projects Using Unity
  • Learn C# Scripting Basics
  • Create Some Fully Function Games
  • Learn To Build Games for Android
  • Learn Useful Unity Tips & Tricks
English [Auto] Heyday with them to this video. My name is Rajha from Charger games. And in this video we going to build a simple 2D shooting game in unity very very quickly. So I'm not going to explain a lot in this case because in this case we are just looking to get started with Unity quickly. So I will walk you through the complete process of building the game very fast. And later on in the later sections he will learn more and more in depth about coding about unity and all that stuff. So let's see what we can build in this case. So let me go here and click on play and you will see here we have a nice beach scene and here we have some targets. And here I have this cursor and I can simply click on it and he was here a short sound and I'll show you if you can hear the audio from the computer. And every time I shoot one as you can see here the score increases and I have to get a score of 10 in order to win this game. So let me click should enter the credit score and it says you win. So this is a very simple game. But while doing this we're going to learn a lot of new concepts. So I hope you're really excited to build your very first Awesome game quickly. So let's get started. So as you can see here I have opened up unity and here I'm going to create a new project. So make sure to click on this new button to create a new project then from here we can give you the name we're going to give it name do the not not add the dooly shooter course and for the template we're going to choose Studi because we are working on it to the game. And from here you're going to save are you going to use the location ready when you see this project. So now that we have done this let's go out and click on Create project and it'll take a lot of time before it can create a new empty project for us. So let us wait for that time. OK so as you can see our new product has been created. This is an empty project created by unity. And if you cannot see the layout that I'm seeing frankly probably you have a different setting in the city. You can go to the stop right corner and from the layout the two by tree layout and when you like to buy through layout you'll be able to see the layout that I am seeing here. So we are all set. Now we are going to get started building our again. First thing I'm going to do is I will set the resolution for our game so significantly free aspect is selected. I'm going to click on here and from here you can use any aspect ratio that you want. I'm going to use 19:20 By 1880 which is for HD. You can click on this plus sign and from here you can ride with to be 19:20 or anything that you want and the height to be in 80. And after that simply click on OK and you will see that it will take that resolution as you can see clearly it says 1920 by 1880. OK so now we have set up that next thing we need to do is we need to import the assets that he can to use in this case as you can see inside this project folder we have this assets folder inside the assets we have only one folder which is named as saints and inside that we have this all ready saved scene which is currently named sample scene automatically by unity. So I would right click and click on rename it and I will name it again. You can name it anything you want. So now that we have our scene set up let's click on the assets folder right click Create a new folder and here I will create a new folder named sprites so strikes inside the spites folder we will import all of our images course assets game assets that are going to use. So in this case we're going to use few of the assets that I have downloaded from the internet. I will give you links to download these things. So let's go ahead and drag and drop all of these things into our sprites folder. So none of that we have imported this the level we can use them in our game. So we have the scene set up and we also have the sprite set up so you can see here we have something called game background. I have downloaded this game background from graphics dot net and if you want to you can simply go ahead to the Internet and download any backbone that you want and in Vergano that you want to use. And there is no issue in that. So now I'm going to go and drag and drop this game background right here on the heart. So if I drag and drop it here as you can see our background nicely it gets nicely positioned on our screen. And if you select our main camera as you can see our main camera is the border of our main camera and our background is bigger a little bit bigger compared to the main camera. And that is why we are seeing the whole background inside our team view. So now that we have this background next thing we do is to select these to use these targets. Now we have two different target images here. And I have downloaded these images from Kenny dot in IL. And if you want to you can use any other image or any of these two images. I think I'm going to use this one because this looks cool. So let me go in and drag and drop it in our hierarchy. And as you can see here we have the target but sadly we can not see the target and that's because it's only back later or it's on a different Llyr compared to our background. So what we want to do is we're going to push our background to the back and put our down get to the front because we want to always see the target on the front. So in order to do that we have different ways to do that so we're going to do it in the simplest way. Let's select our game background as you can see the older layer is set to zero. Now let's look at our target colored outline which is our target. Let's rename it to target. And now in this gives the order. Let's make it one. And now you will see it will magically appear on top of your background. So the Adrea the optic with a higher value in order will always be on top. So in this case this is born and let's say if you want something that will be even over this or even on top of this target then you can set it's already to be two or three or anything that you want. So now that we have this target set up next thing we need to do is we need to add a few more things to this target. Now we want to shoot this target and if we want to shoot this target then we we have to have something in this target which can detect collision. So without detecting collision we cannot detect shielding or colliding or anything. And in order to detect collision we need to add a collider to this target. So let's go to this add component and from here we will go to physics to make sure you go to physics to D and not physics because it's a duty game physics degree and from here we are going to like this cycle collided today. And now if you zoom in you will see that we have a green outline on the outside of our target. So that means now our target has a collider. So now it can detect collisions touches and shooting whatever we going to say. So now that we have done that we are going to use this target again and again every time we are going to use our target. So we want to save it at our location so that we can reuse it again and again. And in doing it we have a concept called prefab. So when we want to use something again and again with the same features we save it as a prefab. So the same way inside the assets folder I will right click Create a new folder and I will name it pre-fab. And inside the prefab I will drag and drop our target and you will see now it becomes blue. That means it is now pre-vet. Now don't worry if you don't understand these things clearly because later on we're going to discuss about these things in more detail. Currently and just going fast so that we can follow along and build this game super fast. So now that we have this target set up next thing we need to do is we need to do something so that it can detect whenever we keep our mouse over it and click on it. Let's say Currently we keep up. Frankly we have a mouse here. And if a click here we want it to detect the shooting and after detecting the shooting we want it to destroy itself. OK. Now before doing that let's go ahead and add some cool looking features to our mouse as you can see the mouse shows only an arrow. But in this case since we are creating a shooting game. So what I want is every time our mouth moves in the scene we want to show a cross here instead of a mouse instead of a mouse arrow. So if you take a look at our sprites folder you'll see we have some images of crosshairs here. And I have downloaded these things from any DOT in Ill. And we can use anything that you want. I to use this one so in order to use it as our mouse cursor what you want to do is you need to select this one. And as you can see the texture type is that to sprite. So what you want to do is you need to click on this and from your select cursor. So no this is not only a sprite. This is a cursory. So now we can use it as a mouse cursor. And after doing that make sure to click on apply because if you don't you can apply the nothing will be applied and no changes will be saved. So the click on apply. So now this one has been saved as a cursor. So now let's go ahead as against it still our mouth shows the wide arrow. So now that we have done with we have converted it to a cursor where you build unity that we don't want to show the mouse arrow. Instead of that we simply we want to simply show this cursor to do that. I will go to File belt settings and click on play your settings and from the player sitting as you can see here we have a few of the settings. Let's go ahead and as you can see here we have a default cursor. So this is where we need to add a cursor so you can either click on select and from here select the cursor or you can drag and drop the girls through MacGuire and click on select and clicking select. I'm going to select this red or orange cross. You can also see legs white one if you want. I think I like this orange one more so after that I simply close it and close it again. Now you see whenever we keep our mouths on the game view instead of showing the mouse arrow it shows the girls here image and it looks really awesome It looks like we are shooting. So whenever we or wherever we move our mouths it moves along with it. And it is ready to shoot. OK so now that we have done this next thing when you do is going to make sure that this target detects our threats and whenever we click on the target or shoot on the target it gets destroyed. So let's see how that how we can do that. So now we are going to create our very first script. So here I will reichlich and create a new folder and inside the folder I'm going to name it scripts. So instead of scripts we will write our very first code. So I'm going to write click here create a new C-Sharp script and I will name it target because this will control everything inside our target or everything that you want to do in your target. We're going to write it inside the target. So now let's go ahead and select our target and drag and drop our target on our game object. Or you can also select the target go to add component scripts and from your select the target script. So now you're going to see the target script is attached to our target. So whatever code we write inside the target will be applicable to this target. So let's go out and double click on the target to open it in Visual Studio or whatever corridor you have set in your case. OK so now with your studio has opened our target script let me go ahead and zoom in a little bit so that you can see it clearly. I think this is more than enough. So now inside this target script we need to write some code so that it can detect the looks on it the most clicks on it or the shooting on it. Now in unity we have a very good or very simple method provided by unity which you can use to detect the mouse clicks on any object and that is known as O'Nan mounts down. And in order to use Deryni to simply write voyde own mouse down and make sure that you have capitalized it accordingly. You can also select this opposition and click on enter and unity or visual studio will automatically complete the code for you. Now make sure you write all capital in capital and the capital. Otherwise this will not work. So this is a function that gets called automatically whenever a mouse is clicked on. This target so in this case what is the target in this case the target is this image. So whenever we will click on this image this on mouse down function will they get caught and what we want to do whenever the most down gets called We want to simply destroy the target. So here we will. Right. Destroy which is another function majority which will simply destroy the game object. And inside this being tried game object. Now you might wonder what is game object. So game object is the object to which this script is attached. In this case the script is attached to our target image. So whenever on mouse down our mouth is clicked. Our target image will be destroyed. So let's see if that works or not. So let me click on maximize on place so that whenever clicking this whenever playing this this will be maximized. Now I'm going to click on play and wait for it and he was here have Dagget and here I have my cursive I will go here and click and residency as soon as I click on it it gets destroyed. Let me try it one more time. Here we have our image cursor. Click thing in small view. It doesn't break sometimes so make sure to click on maximize and play and let's play. And destroy it. OK. So you can see this way we can detect if our mouth is getting blocked on this object. And accordingly we are destroying the object. OK so now our target can detect clicks. So whenever you make any changes to any praetor optic that you have on the scene make sure to click on the supply button and this is very very important because if you don't click on these apply the dangers that we have done will not be applied to the pre-fab object that we have here. And we are not going to use this object again. We are only going to use it from the pre-fab folder. So whenever you make any changes to the prefab object was selected and click on apply so that the changes get applied to the pre-fab object. So now that we have our target set. Next thing we need to do this I thing I think we're going to create a game manager and the game manager will be responsible for spawning this object here here here here again and again also our game manager will count the score. And whenever we win or get a score more than 10 or 20 it will show you win next year. So let's go ahead and create our game manager. So from here I will click on create credit empty and I will limit game manager. You can name it anything you want. A good manager is a standard practice. Now inside the scripts folder I build right click and create another new C-Sharp script and I will limit game manager right this time. I will select Maggie's manager and drag and drop the game manager skipped over it. Just like just like this. So here we have a good manager script. So now let's go ahead and double click to open it in Visual Studio. So here we have our game manager script notes trying to understand quickly what we actually want to do inside the game manager. Now there are many things that you want to do but we need to go step by step. So first thing we want to do is we want to spawn this object randomly in different positions of our screen. And how can we do that every time. Let's say after five seconds five seconds. That's it after one second or two second we want to create a random position by using some function we want to create art are we want to get it a random position on our screen and it make sure that the random position is always inside our screen and not outside of a screen. Let's see if we get a random position here. But we're not going to see that in the game. So we need to make sure every once in a two second we get a new random position and in that random position we spawn or read or instantiate a new target so that every time we have oil we have to shoot on different place to get score and to win the game. So let's see how we can do that. So because we want to spawn our object. So that's why first of all we're going to create a spoon function. So we will limit voyde spawn. OK. So this is the Spohn function that will be responsible for spawning an object. Now what we want postponed. When did you know that. And because we to know what we want to spawn that is why we will create a public game object target. So inside the Stargate we will store our target pre-fab. And now you see this target pre-fab we can instantiate or spawn any copies of the target object. OK. So now inside this spawn function we need to spawn the Stargate in a random position. So the next thing that we need is the random position. So how can we predict a random position. So here in Spawn let's say we create two random position values and this will be flawed because we want to spawn. We need to store the decimal points in it because the position is always in the Smith point. So now the position is not actually exposition or y position. Similarly the position is something that you can represent is in x y and z. As an example let's say in this case the target is like the target. As you can see here we had the position and in the position we have the exposition minus one by 9 9 and the y position is zero. We can make the y position to move upwards. We can make the exposition and change it and it does move on the laterite. So if we want to spawn it in a random location we're going to change the x and y both of these positions. Now you might wonder why we are not talking about the Z position because this is a two day game and into the game we only work with X and Y and not with C.. Ok so now that you have understood the position thing we need to now know or create a random position where we can spawn our target. So here I will create two different variables float and them x. So this position will store their random exposition and floor random y. So this will store the random y position. OK so now let's see how we can create or how we can instantiate an object in the run position. So before we can actually spot it in a random position we need to get a random position. We have only created a variable but we don't have any value inside the variable. And in order to create a random value we need to write a simple function. When to say random dot range. So this is a function. If you select this as you can see this function. This function is asking for a minimum value and a maximum value because if we do just entered a random number it may generate 256 or two million or only two. So it can generate anything. So in order to generate a random number we need to give it a range within which it has to generate. So in this case for the value we need to give the minimum value and the maximum value of your screen and for Y as well we need to give the minimum value and the maximum value of our screen. So if you select our target and click on this Adul key or move to as you get to see if I move it to the far left thing about this I think this is the maximum position that you want. If you take a look at it you can see the maximum position is minus seven point two. Let's hold Let's take minus seven. And if we make it seven you'll see this is the maximum position on the right so we want the Doggett's minimum and maximum position to be minus seven and plus seven in the skates. Now you might want different values so now inside this Raynham range we're going to see minus 7 F and 7 f and we are writing F in order to specify that this is a lower value. So now this random mix will always generate a random number between minus 7 and plus 7. And now that we have the same thing for Y as well. So here we write random dot range. And now we need to figure out what is the minimum and maximum value for the Y. So if we select or object again if we move on the top. This is the top as it can see the y values 3.9 for let's make it about 3 so the minimum value will be minus 3 values minus three maximum value is plus 3. Let's make it two point five. Will it work or get 1 5. So now the minimum y value will be minus 3.5 F and the maximum and comma and the maximum y value will be 3.5 F.. So this way we are getting a random expel you between negative and positive x and we are getting a random y value but is minus 3.5 X and positive 2.5 x. So now that we have this random x and random y position. Now we need to create the random or we need to actually generate that to the position for our game as we have discussed previously. The position in unity is represented by three values x y and z. Now since this is a 2D game so we will keep the Z all with zero. So in order to create a position we need to get Victor 3 variable because if we want to represent three values you need to create a vector 3. So we will write vector 3 position or let's say random position which will be better. And equals do new vector 3 because we are creating a new victory and for the x value we're going to give random random x for the y value we're going to give random life for the z value. We're going to give just zero. So now we have finally created in a random position and we have stored it inside the random position variable. Next thing we need to do is we need to now instantiate or Spoonman or object in this random position. And in order to spawn we have a specific function called instantiate infinity. So we need to write instantiate. And as you can see if you click on the if you press the down arrow on your keyboard you will see the different versions of this function as you can see. We're going to use this version. First of all want to ride the object that we want to instantiate. So in this case meant to instantiate the Stargate when to spawn this target. So here we will ride target. Then we get to ride the position where we want to instantiate. Of course we want to instantiate in this rennen position. So we want to ride random position. Then we need to write the rotation that we want to do. Now in this case we don't want to vote it. So that is why we write a quarter million dollar identity. So whenever we don't want to rotate our available we want to make the rotation identity or zero. We're going to ride dernier our identity. That means there's no reason at all. So now whenever these Spohn function will be called a random position will be generated and a new target will be spawned in the random position. So till now everything is correct but now we need to figure out a way by which we can call this Spahn function again and again because as you can see currently we have only just Spohn function and we can not do anything about it. We can simply call it once from the start. But that's not going to work because we need to call it again and again. So now let's find a way to do that. So it is going to and they did one more time to see if the Spahn function is working or not. Salumi copied this thing and paste it in stock. This means whenever the game starts and the start function gets called this Spohn function will be called. And our target will be spawned in a random new position. So let's go back to unity for now let me go ahead and select the target let me select this target and disable it because we don't want it no I will click on play and because we have this thing we should get a new target every time we run the game in a random position. The one last thing that you need to do is before doing this as you can see if you select the game manager we have this target which is looking for a target game object that we need to spawn and make sure you don't drag and drop the target from here because we don't want to do that. Always We're going to drag and drop the target from the pre-fab folder because we Hepzibah it inside prepared for the using. So drag and drop it inside the target. And now we have the target here. Now if I click on play it will see it will respond in a random position as it can see currently despond here. And if I go ahead and played one more time you will see to this point in another random position and once again if I tried you will see it was born in another random position. So this way as you can see every time we run the game the thing is or the target is spawned in an interview in a new random position. So instead of running our game again and again or clicking on play again and again we need to figure out a way so that we can call the target. We can call the SP1 function from the target again and again after a few seconds so that it gets spawn or our target gets spawned again and again and again in a new position. And nudity it's very easy to do that using a function called invoke repeating using invoke repeating we can repeatedly call any other function any number of times after a set of interval time. So inside the start first of all let me go in and delete this thing because we don't need this or we can simply comment out so here we can say we can write invoke repeating and inside the envelope waiting for us to pull within string. What did you write the name of the function or the method that we want to call in this case we want to call this spoon function so let me go out and copy this spoon. And here we need to write double quotation marks and within double quotation we had to write the spawn. Now make sure you write this exactly as it is. So it's better to copy it from here and paste it here so that you don't make any mistake by chance. They need to write comma and for the second parameter interval the time after which this function will start calling. Let's say we want to start calling after one second. That means we start playing the game. One second goes. And after that this thing starts working and finally the most important thing. This is where you learn to write after how many time are up to how many seconds you want to repeatedly call this function. So let's say Here I get to half. That means after every two second this function will be called again and again. So this function is called Two second waiting called again to second waiting call again. And this is how it will work. So we can make it one if that means after every one second this function will be called again and again. Now if you want to you can simply go ahead and change it to any value you want and experiment with it so that you can see how different things are working. So now let's go back to unity and see if a new object or a new target is getting spawn again and again after two second or not. So let me click on play and you will see a new target is getting instantiated in random locations in different places on screen. After every one second and I can go ahead and delete all listings. No I can go out and shoot all listings. Now one thing you will notice is that even if I don't shoot the targets stay on the screen forever. And sometimes the overlap over each other. Now we don't want to do that. What we want to do is we want to remove the targets after few seconds from our screen so every kind that targets a small one. We want to let's say keep them on the screen for two or three seconds and after two or three seconds we want to delete them or destroy them automatically. If we can not show them. So if we're shooting them they are getting destroyed. But if we cannot show them we want to destroy them automatically after two or three seconds. So let's see how we can do that. So let's go out and select a target and DoubleClick on the target script to open it. And in some Dogood script is the start function went to arrived the destroy function. One more time. So he will write destroy game object but this time we need to also specify a time after which we want to destroy this object. So in this case we're going to destroy the object after two seconds so here we write to f.. So the object on the target gets instantiated and after instantiating it which spot two seconds for us to shoot in that time if we shoot the object it gets destroyed. And if we can not shoot after two second it gets destroyed automatically. So let's see how that is working in unity. So let's go back to unity and click on play one more time and this time you'll see the objects that she did end up at two seconds and not shooting at all but up to two seconds. They are automatically getting destroyed and a new object is getting instantiated after every one second. So the thing that we wanted to do is actually working. So now the things are pretty much working correctly. Next thing we need to do is to detect or we need to get the score. So whenever we shoot the object we want to increment the score value by one let's say. Previously it was zero so we never issued it gets one then two and three. And this way it goes on. And when it goes above 10 or 20 we want to show you when text. So that's going to come later. But for now we're going to do all these things. So let us go to our board game manager script and inside the game manager's script we will first people read every book which will show or we will start our score value. So here I will create an integer variable. So right end and then I will write score equal to zero. So now we have created a variable named score which has a value of 0. Now we need to write another function which automatically increases the value of score. So here we need to write a function and it will increase the score. So that right the function name void in crimen score. And if you want you can simply rename it to anything that you want. This is simply something that I think is logical in this case and that's why I am using that. So this in insight is incremental confirmation. All we need to do is every time this function gets called when it to increment the score value by 1. So the easiest way to do that is right. Score plus plus. And every time we write score plus plus the value will be incremented by one of the that we're we can also click right print score. So what this will do is this will print the new score value on our console. Now later on we're going to use some text to show the score on the screen. But for now just for testing purposes we are printing it in our console so that we can check if the score value is actually increasing or not. So now that we apply the function which increases the score. So now we need to call this function whenever we shoot on our target. So let's see where we are directing the shooting. So if we go Inside the Target script we see here we have this on MTS down function and inside this we are detecting they were shooting our optic. And inside this we are actually deleting our deleting our target as well. So inside these own mouths don't function we want to also call this incremental core function so that whenever we shoot on the object the object gets destroyed and also the score increases. So let's see how we can do that. Now this Stargate script is attached to a different object. The Stargate script is attached to a target object and it's give manager a script is attached to the manager object. So this way we have two scripts on two different objects. Now there are many different ways by which you can communicate between two different scripts which are attached to two different objects. Now here we are going to do. We're going to use the simple technique. So what we're going to do is first of all here will create a public game object public game object and we will name it. The team manager game manager inside the game manager we will get a reference to our game manager object. So instead the stock function we will write the manager equal to Kiem object don't find and this find function will actually this find function will actually search for it object with this name in unity hierarchy. So here we write Game claim manager. So now this fine function will find in no object which has this game manager name. So as you can see if you look at our hierarchies it can see here we have this object with the game manager name and after finding the object with game manager name Next we need to find a script which is attached to it. As you can see in this case this game manager script is attached to it so need to find the object they need to write a dot sign and then we'll write get component and inside then we to write game manager. So this is a little bit confusing compared to what we were doing till now. So this is a little bit complicated but for now just understand that first of all we are finding the object with the name manager and then from the game manager object we are getting the game manager script component as it can see in this case. This is a game manager object. And inside the game manager we are getting this game manager script that is attached to it OK. Simple. So when we get access to the script then we can get access to anything from the object that we want. So as you can see it is giving one error and that is because it is trying to store this game manager object inside the game object. So in this case we're going to simply change that type to game manager and the variable name is going to make the G smaller negligees. And heroes will be going to make the small. So now we are getting the game manager we are getting in reference to the good manager script and we are starting the reference inside this game manager variable which is of type case manager. So this is a little confusing but don't you just need to understand it. And later on when you go further and dig the hole dig the lessons on C-Sharp and go further you will learn more about these things and these things will become clear. So for now just understand that this is a public reference to this game manager. And inside this we are storing a reference to this give manager a script so that we can access this increment SQL function and increment the score. So now inside the game manager's script or manager Vrable we have access to this game manager script and now we can easily call this in command school function. But there's one more thing that we need do if we wanted to call it function from other scripts. We need to make sure that the function is public. I think I said fine. Well actually if we want to call any function from other scripts we need to make sure that the function is public. So in this case as well we need to make this public. So now this is a public function and we can call it from other scripts so we go to Target and hear our own mows down before destroying the object. Make sure whatever you do it will do it before distrained object record because when the object gets destroyed we can not do anything because everything the script and object everything will be destroyed. So before destroying the object we're going to say game manager. The smaller one dot in crimen score. So now whenever this milestone function gets called that is whenever we shoot on the object this manager also calls this incremental function from the game manager. And this will simply increase the value of our score. So now let's go back to unity and see how it is actually working. So let me go to window and console. Window in general and console. So this is where right now our values will be printed because we are printing it in the console. So if I click on play you will see that every time I shoot an object I shouldn't object as you can see if I shoot it as it can see the score becomes one if I should one more object as you can see the score becomes two. I should one more the score becomes three. And this way as you can see our school gets incremented. So currently as we can see our school is working. So the next thing we do is to make sure we don't miss something so that we can see our school on our screen by using some text. So let's see how we can use some text to show up or score on the screen. So I will go to create UI and click on text so it will create a new UI text for us. Then I will go to the skeer icon in the top and click on reset. So now it will reset the position of the text to the center of your screen as you can see here we have a very small looking text. Now I will double click on this text to focus it on the scene view then I will select this direct tool to increment the value of the. Increase the length of the score and then I will increase the school text books like this. As you can see the score value or the score text is still very small. So if you come here you can select the font size and let me change it to 15 or more. Let's make it to 80. I think this should work and the text value will be zero by default because in the beginning the score is zero. And as you can see the score is on the left side of our text box. We're going to make it on the center. So from the alignment to make it center and center in BORTOLI zone. And vertical ways now we can select this object this move too and move it on the top. I am dragging on and actually dragging my mouse mouse wheel too. I'm writing my mouse wheel to zoom out of the scene. And no I can simply drag it on the top so to show the score right here and then I can tell the collect anything I want. I think I might change the color to a little bit orange orange like color. And I think I will increase the phone to about 100 because I want it to show really big. And from the font style I will make it bulled so that it becomes even bigger. So this way as you can see we can see our score here and the text value is currently zero. If we change it to 10 or 20 or 200. We can change it to any valid we want. And accordingly the next value will be changed. So now we need to do something from our script so that every time the school value is trending on the variable school it is also changing on the text component here as well. So most of all is quiet and rename the sticks to school text so that we can actually understand of what is school and what is not. Now let us go back to our visual studio script and here boy need to do something so that every time this increment score happens the score also gets changed in our score text on the screen. So first thing we do is we do here create a variable of type text because instead the text variable we are going to store the text score text so we write text not text alignment in writes text school text. You will see that it is giving me an error and that is because in order to use anything like text or buttons or anything from the scripts to write here using a unity engine dot you. So only after writing this using unity engine that UI namespace we can use this UI elements in our code. So now we have the score texture. And I will also make it public so that we can select the objects from our inspector. So make sure to make it public then if what I want to do is every time the score is getting incremented I want to change the value of your score text as well. So here is the right score. Text don't text. That is we are changing the text value of the score text and we'll say equal to. And what is the value the value will be equal to a lower score variable which is changing from 0 to 1 to 2 to 3. So here we right score. Now one thing you notice is that this text is a string and this score is an integer. So need to convert this integer to this string for Hillary right dot to stream. So this function will convert this score integer to a string and it'll score that it store that inside the next value of our score text. So now every time the score changes it will be reflected on our score text as well. But it found that only one thing that we need to do now before doing that make sure you make it public. Otherwise this is not going to happen. So not if you select a game manager inside a game manager you will see another thing appears as you can see the score next extra verbal abuse. Since then the score text when it's a dragon or score clicks from the canvas or right here and if you don't make it public then it will not be able to seat you. So now that we have our school text here now it is connected to our script and every time the score changes it will be reflected on this text as well. So if we can play you will see that every time I shoot an object as against the score increases from 0 to 1 to 2 to 3 to 4 to 5 and all that. So this is this is how our school is actually working fine. So now what sort of things are working and our school isn't working as well. So now what you gonna do is we need to detect the winning condition for our game. So currently we don't have anything so that we can win the game we can simply play and keep playing it forever and ever and the score will be we'll keep incrementing. So when it do give a condition so that let's say after getting a score of 10 Here it is written that you win and the game stops and objects stop spawning. So let's see how we can do that. So inside these spikes folder I have this a you when text I would simply drag and drop this image here and then double click to see it on the scene as it goes you can not see it on the scene because it is on the back. So in the Areli let's Mikky you can see now we can see it. And let's change its X scale to two and the Y is going to do as well. Now it's a little bit bigger compared to what it was previously. And he can also right click or click here and reset its position to the center of the scale. You can reset its reset position to the center as you can see now it is in the center. So whenever we get a score of 10 we want to make this object appear on the screen. So let's change the objects name to win text. Now we go back to our scripts and here we have our game managers script. Now as you can see inside the incremental score function we are detecting the score increments. Now here we will dig the winning condition as well. So here we will create a boolean variable we'll name it win and we'll set it to false. So this win is a boolean variable a boolean variable can store only true or false. So in the beginning we have we have not won the game so that is why when is false but later when we'll get a score of then it will say Winnicott so true. And we have to do that so inside the increment to give a condition. So all right if score is greater than zero the score is greater than equal to zero. We're going to say win equals true. So when MBRD the score value becomes greater than non-zero Emslie when the score gets bigger the becomes greater than 10. Then you say when it goes through so we're going to take the score increment the score and whatever it becomes. When we say when equal is true the one thing we did do is whenever we have one we don't want to spawn the optics anymore. So whenever the game is born we simply want it to stop. So inside the update we will check for another condition. We'll see if when it was true. That means when we have won the game we need to stop these invoke repeatedly calling. We had to stop these repeated calls of the Spohn function and to do that right. Kancil invoke and inside the castle invoke we to again copy this form and pasted within quotation marks so now whenever when will be true this Gansu invokes one of the called and is repeatedly calling or does invoke the call will be cancelled and the Spohn function will not be called any more because whenever we have won the game we don't want to spawn these objects anymore. We want to simply stop the game stop the spawning and show you when text on the screen. So in order to show that you will text here we had to create another variable which will store you when text. So you write public game object not game manager public key Munchak when text so inside of when text was stored the Windex that you had just created. And whenever we have won the game here we labor we have won the game. We was simply here simply right when next door said active to true. If you take a look at unity as we can see here we have the text and by default we will make it de-activated says he can see we have deactivated it. So now whenever the score is greater than 10 we will again at the words text and it will show you win. So that is what we are doing here. One last thing we should do is win a district or game manager and you will see here we have this win. Text field here and you drag and drop this when text right here. So now our game manager is connected to a text. So whenever we win it should stop spawning the target game optics and it should show how you win text on a screen. So let's see how that's working. Let me go ahead and click on play and you will see here we have the objects. Click click. Shoot shoot shoot shoot Despoina show this photo shoot this morn 9 and 10 as it can see Chapter 10. No objects are getting spawn and it shows you when text on the screen so that pretty much means that everything that we wanted to do is actually Burts. That means our game is fully functional so now that our game is fully functional lets go out and add one last effect to juice up it a little more. So now we will add some audio effects or sound effects to make that shooting sound whenever we click our mouse overtook him object to do that. We need to use something called an old source component. So I was like the game manager and from the ADD component I will select audio and then I will select this order source. So this audio source component will help us to play some audio clip whenever the Whenever we do something say in this case whatever we want to shoot. We want to play an audio clip now from here we need to disable display on a week because we dont want to play it from the beginning whenever we click on a mouse. We had to play it but from the beginning we don't want to play it. So that is why play and awake set sorry to false next thing to do is to find some sound effects that we can use in this game. Now there are different places from where you can find free sound effects so you can go to free sound dot org which is a very good free source very good website for finding free sound effects and in the end the free sound dot org you can simply search for shooting or shoot anything that you want. And from here you can simply select any of the short sounds that you want. I think I have downloaded one of this should do or should one sounds eating any of this. You can select any of the sounds that you want to use and you can use it in your game. So here I have downloaded two different sound effects from that free sound on our website and I will drag and Rob both of these you know. So in the end as its fuller I will create a new Fuller and I will name it. We're going name it sounds for you anything that you want and it sounds I will simply drag and drop this. I think I'm going to use this door slam sound so I will drag and drop it here and you can select that and from the bottom you can click on play to actually see or hear how the sound if it looks like the probably in your keys the window may be like this. So you can simply click and drag it up to see it from here. No we need to go to our manager. And from the artist source as you can see here we have a option here you have an option bar or you clip. So inside the audio clip we can drag and drop our sound that we have downloaded and now we have access to the sound from our audio source. Now we need to play this sound from our script whenever we click our mouse. So let's go to our key manager script. So here we want to detect when we are clicking on the mouse. So inside the update function in our game manager I will say if input Daut get mouse button down and this is a function that helps us to detect when we are clicking on the mouse and inside the phone when it to the button that would detect. So in this case we really the left mouse button clicking or whenever we interdict. When we are clicking the left mouse button. So here we are right the zero the zero button. That is the left mouse button. So in this case whenever we are clicking the left mouse button we need to simply believe that or do you want time to do that for us to play to get access to the audio source component that is attached to this object. So all right get component audio source if there of parentheses Daut play and no. Automatically whenever we click our mouths this audio source component or whatever or whatever code we have not called whatever sound we have inside the audio source component will be played when Oracle Como's. So now let's go back to unity and see how it's working. So here we have a good manager and he our game manager we have this or you clip. So let me go ahead and maximize and play and then click on play you will see that here we have an object and every time we showed the object you can hear the short sound and every time we should do we have all scored increasing. And when we have our school then or more it sees you win. So our key is completely working as we want it to work. So our game is completely working and it's working fine. So the only thing now to do is we to build and publish it so that we can play it. So you can go to file settings and click on player settings and when you click on player settings you can see here we have lot of options here. So for the company name you can give any company an in that you're working on or your own company name. So I'm going to name here charger charging teams. But the product name of course is ok. I can you can give any I can. I think I'm going to give this one as the icon of the game and from here you can change anything that you want from the resolution. You can change the resolution that you want to allow your game to play on as you can see here we have this supported aspect ratios I think. Which one I want to do I think 16 by 9 or 16 button is OK but let's keep all these things later on we can change it from the play screen so now we have done it. We can also go to this web G.L. build and from there as well be contains the resolution in which we want this to play. OK. So note that we have done this. Next thing we need to do is to click on build to play it to build and played. Now as you can see in this case in my case the current build is set to web to yell in your case probably this is set to PC Mac and Linux stand alone. Let's see if we want it to Abdiel. You can simply select web Jill and click on switch platform and the platform will be selected. Or switch to web Geo. And then you can build it for an web Jill. But in this case I'm using this Bisi Meccan extend loan so that we can lead on PC market Linux. So once you have selected that you can simply click on Bill and here I'm going to create a new folder named build an insensible folder. I will save this I will save this object and as you can see the object or the game will be built and it will take obviously some time because it is gathering all the resources building the game. And then finally giving us an outboard which you can run whenever we click on it. So let me wait for it to complete and then we're going to play our game today a big so far so as you can see our game has been built and it is ready. And as you can see that I can hear. So you can double click and start playing the game so me a double click to open it and you will see whenever we open it we have the option to make it windowed or not windowed. We have a window that means it not. It's not going to be full screen. And for the resolution you can select any resolution that you want and when to use this will 80 by 720. And after that I click on play and you will see the level to play the game. And as you can see we are playing it and surely the sounds are working. And now whenever we finish playing it we'll see that this you when text is shown on our screen. Now one thing you noticed is that when we built the game and played it on it will be 80 percent resolution. You saw that we can not see our score anymore. Now here is where the folks like that will vary by 720 resolution. You will see that we can not see the score anymore. So what you do is do some things so that we can see the score in all the solutions to do that will select our canvass go to here and on the canvas skill component will said to you why skill more to scale with screen size and for the reference that illusion will give 19:20 by an 80 or whatever resolution you were working on. And for the match value we're going to give 0.5. So now you will see that our canvas or the UI elements will be scaled automatically with our screen size. So now if I go ahead and make the resolution pull that over 720 you can see that still we have the score here. We can make it any other resolution that you want and we will see we still have the score here. So now if you built the game to any resolution or a 720 or 1920 or 80 and played in the resolution in any resolution you will see that you will be able to see the score. And the problem that we have seen before will not be there anymore. So this is it for this video. I hope you really enjoyed this video. Working on the game step by step we have learned so many things by working on this game we have learned how to take inputs we learn how to make collisions how to detect the shorts how to change the mouse cursor. Did the win and 10 schools show the scores on the screen and all that stuff that you have seen. So we really enjoyed this with you and learned a lot of stuff. So thank you very very much watching this Rajha from Charger games and keep learning keep building more cool stuff. I will see you later.