Unity Android Game Development - Full Course 2023 | Build 7 Android Games

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone this is Raja from Charger games and welcome back to this video so in this video we're going to build a simple cool little Android game and we're gonna learn how to build simple Android games from start to finish so here let's go and check out the game that you're gonna build so as you can see here I have the game and here I have the device simulator opened so it's showing us how it's gonna look in a real mobile device so I'm going to click on play and now as you can see this is the game here we are playing as a rabbit and here we have some carrots that we're going to collect and here we have a joystick on screen and we can control it control our player by moving the joystick so as you can see if I move the joystick and collect the carrots one by one the carrots will be disappeared and the game also has a boundary so you cannot go beyond that boundary as you can see this is the boundary I cannot move beyond that and now I'm gonna go ahead and collect the uh carrots one by one by one and slowly my score will increase and as soon as I collect all the carrots I will win the game so I think I have one more character left so let's go and collect that so as you can see as soon as I collect all the carrots you win message is printed and this is the simple game that you're gonna build this is great for beginners to learn or if you are new to Unity it's great to brush up your skills and build a simple cool Android game start to finish so I hope you're really excited to build this game with me so with that being said let's get started so in order to download Unity you can go to this unity.com download or you can go to Google and simply search for download Unity and simply go to the first link and from there you can download Unity so from here you need to create an account if you don't have a Unity account before you need that account for downloading and also for getting your license and from there you can get a free Unity license then you can simply go ahead and download for Windows or Mac and after that you need to install a Unity Hub so once you've done that the unity Hub will be installed once you have installed the unity Hub simply go ahead and launch it as you can see here I have the unity Hub installed and launched and this is the new Unity Hub so from there you need to go to this installs Tab and here you will have all the versions of unity that are installed on your computer so from there you need to go to this install editor Tab and here you will see all the versions of unity that are available to install here as you can see something called LTS or long term support So if you are building a long project then you need to use some of these versions that will be available for a long time and they will be supported by unity for long otherwise you can use the latest released version here or you can go to this pre-release and from here install the latest beta versions so as you can see here from here you can install the latest video version so you can go ahead and click on install and now here you need to install the components that are required for your game so visual studio is really required because that's what we're going to use for coding our games as you can see in my case this has been already installed that is why I don't have to check anything here but in your case you need to check hit here then if you want to build your games for Android then you need to check this Android build support and make sure that open jdk Android SDK indicate tools all these things are also checked if you want to build for iOS then you can simply click on this iOS build if you want to build for webgl then you have to check this webgl build support as well alright so once you have done that simply click on continue and then these things will be installed also another thing you need to check is that from here if you click on this settings from where you can actually check where your installs are saved as you can see inside this installs tab here you can choose where all your Unity installs will be saved so from here you can change the location so once you have done that Unity will be installed and then you are finally ready to start your new project so this way you can go ahead and download unity and then we are ready to start building games so here I have opened up Unity Hub and let's go and start creating our new project so from here I'm going to go ahead and click on new project and then I'm going to select the latest editor version that I have now from the templates I'm going to go ahead and choose this 2D core okay so we're not going to select any other thing just choose 2D core because we're going to build a 2d game and later we're going to convert it to mobile and then here I'm going to choose the location where I'm going to save my project and I'm going to name this give this a name so let's say carrot collector or something like that okay and then I'm going to choose the location where I'm going to save this project so in my case I'm going to choose it inside my C projects Unity okay all right so this is the folder where I'm going to save it and now I'm gonna go ahead and click on create project and it will create a new project for me so let's go and create project and wait for Unity to create a new project for us all right so as you can see now here we have a new empty 2D Unity project opened so first of all you need to make sure that you are in the two by three layout from the top right corner you can select this so I prefer this layout so I'm going to select this one now we need to change the platform to Android so that we can build it for Android so you can go to file build settings click on Android and then click on switch platform okay now if you don't want to build this game for Android then this is going to work on other platforms as well so if this is not working in your case you can simply go ahead and continue the video and later on you can switch it to Android okay all right so now as you can see the unity icon is beside the Android that means now our current platform is set to Android okay so now let's go ahead and start building our game so first of all as you can see here free aspect is written so from here I'm going to go ahead and select this 16x9 landscape because this is the resolution on which we're gonna build this game if you want you can also choose this 16x9 portrait this is gonna work here as well so but I'm gonna select landscape for now okay now I'm gonna go to the assets folder and as you can see here inside the scenes we already have a scene created so I'm going to go ahead and actually rename this scene and name these two game okay now inside the assets folder I'm going to go ahead and right click and create a new folder I'm going to name this one Sprites so here we will have all our Sprites then I'm going to create another new folder I'm going to name this one scripts here we will have all our scripts and then I'm going to go inside the Sprites folder and here we can import all the assets that you're going to use for this game now if you want you can download these assets from anywhere on the internet and I have downloaded some of the assets for free so as you can see here we have some free assets these are downloaded from the kenny.nl website you can go ahead and download them from anywhere and this you win text I have created by myself and this carrot is downloaded from theflat icon.com so you can go ahead and download them from anywhere or use any other asset that you want to so I'm going to go ahead and select all of them and then drag and drop them inside the Sprites folder to import them so as you can see now we have all of our Sprites imported in the project now I'm gonna expand this game scene select the main camera and from the clear Flags make sure solid color is selected and from the background color I'm going to give it a green light color because we are building a ground we're gonna add the ground but before that we're gonna also make sure that we're gonna give a color to the ground now we can also add this Leaf style to make it look like a ground so let's go and drag and drop the lip tile to our scene right here and as you can see here we have the lib style okay and this is small as you can see because this is a tile so we need to make it bigger to make sure that our whole ground is filled up in order to do that while selecting the lips from the Sprite renderer as you can see here we have the here we have the draw mode selected to simple so what you can do is you can go ahead and change it to tiled so once you have selected tiled now as you can see it is showing the width and height now I can simply drag this and make any other any long tile that I ought so make sure you have this Rec tool selected and now if you drag it as you can see now it is expanding just like a tile on every direction okay so this way as you can see I can create this big round of us now this white area is our camera view so this is the part where the game will be shown others will be extra so I'm gonna make it a little bit bigger because we're gonna keep our carrots scattered all over the game so that our player has to work a little bit to make sure he collects all the carrots okay so once our ground is ready now let's go ahead and add our player so I'm going to go ahead and select the player rabbit and drag it here and as you can see here we have a rabbit so now as you can see the rabbit is a little bit bigger so what you can do is you can either select the scale tool and make it smaller or you can select the rabbit go to the pixels per units and from here I'm Gonna Change it to 500 and then click on apply and as you can see now it has become small automatically so you can either do it from there or you can do it from here as well I think I'm going to make it about 400 will be good all right so now I've set the pixels per unit to 400 and now it is at the correct size you can also change the max size to about 256 because we don't need a larger Sprite so we're gonna make it smaller and then click on apply as well now these are optional steps you can do it or you can skip it you can also change the Sprites size directly by changing the scale or by using the Writ selection tool so now that we have the rabbit as you can see I can double click to zoom into the rabbit and as you can see this is the rabbit this is how it looks like now we're gonna go to add component physics 2D and we can add a rigid body to the component to it so let's go and add a rigid body 2D okay so now here we have a rigid body 2D now we're going to change the gravity scale to zero because we don't need a gravity here so let's go and change it to zero and then we're going to go to add component physics 2D and from here we're gonna go ahead and add a box collider to the component okay so if you find as you can see here we have a box collider 2D so just by using this box collider 2D now it has a collider and now it can collide with any other objects in the scene if we have some carrots in the scene of course it should be collided with the carrots only then it will detect the carrots and it will collect them so that is why it should need a collider okay and the rigid body is part of unity's physics engine so now without a rigid body the collider will not work so you you should have at least one rigid body in the scene in order to make the Collision work all right so now here we have the rigid body 2D and make sure that gravity scale is zero otherwise the rabbit will fall down we don't want that and here we have a box collider and as you can see here the collider is uh nicely added to it so we don't need to make any more changes here okay so now that we have the rabbit right here it's time to add our carrot so let's go ahead and add the carrots so as you can see here of the carrot let's go and drag and drop it in the scene and as you can see it's pretty big and huge okay so what you can do is you can select the carrot and you can go to the pixels per units and change it to about let's say 500. inch is the max size to 256 and click on apply and you will see here we have the carrot and actually it has gone beyond this ground and that is because all of our Sprites are on the same level so what you can do is you can select these leaves and as you can see this already layer is set to zero so what you can do is set the orange layer to -5 because our ground will be beyond all of our objects so our rabbit is at zero our carrot is at zero and our ground is at minus five that means our ground will always be behind all these objects okay so with that done now I'm going to select the carrot and if you want you can make it a little more smaller or you can simply keep it like this now we need to add some collider to the carrot so you can simply rotate your mouse wheel to zoom in to see how things are looking so I can select the carrot go to add component go to physics 2D and from here I'm going to add a circle collidatory to it and I think cycle Collide 2D should be good for this carrot so if you want you can also change the size a little bit change the radius a little bit so that we can make it a little bit smaller you can also add a capsule color to the but I don't want to add that here I'm just simply want to add a circle color 2D of this size and this should be good for now okay so now that here we have this carrot we have the rabbit we have the leaves everything is added already now another thing we're going to do is we're going to select our carrot go to tag go to add tag click on plus sign and here we're going to add a new tag let's say that we had the tag of c a r r o t carrot and here I have made the C capital and make sure you do that as well or whatever you write here make sure you remember that because it is going to be very very essential while you write some code so now that we have added the tag we're gonna select the carrot go to tag and select carrot and now we have the carrot tag attached to the carrot and it will be really useful for us when we actually write the code for the player okay so now we have the ground the carrot and the player ready so now we need to add some movements to the player now in this case since we are building it for mobile we can we can add some touch inputs but here we're going to use a joystick to give some input to the player so what I can do is I think I'm going to make the rabbit a little bit bigger like bigger than this because that might be good so I think something like this should be okay okay so now here we have the rabbit carrot and ground so let's go and add it joystick now you can go ahead and add any other joystick that you want you can use any free joystick that is available from anywhere so what I'm going to do is I'm going to go to the unity asset store and from there I can download a free available joystick and that's what I'm gonna use in this game so you can go to window and from here you can simply click on asset store and as you can see it will open up the asset store you can also go to assetsstore.unity.com and from there you can download this as well now make sure you have an account here otherwise you cannot download anything so make sure you create an account a an unit account and log into the asset store then here you can simply search for joystick and make sure you choose the free joystick and as you can see here it is called joystick pack this is the free joystick available there are other joysticks available as well you can go ahead and select the pricing to free assets and then you can choose from other joysticks as well but I'm going to go ahead and select this joystick which is the joystick pack which is completely free to use so thank you so much it's Creator finnurex Studios For You creating this joystick pack for us so once you are inside this joystick pack make sure to download it and then you can simply click on open in unity so once you click on open in unity it will say open Unity editor and then it will show up in the package manager okay so once you click on open annuity it will show up in the package manager and from the package manager you need to go ahead and download it so as you can see here it is asking me to re-download because previously I have downloaded for another project so for this project joystick pack select click on redownload or download in your case and it will download it and once it's downloaded now it's time to get imported so in my case it has been already downloaded so I'm gonna go ahead and click on import and then it will import it so click on import and now as you can see here we have the whole joystick pack and here we also have some examples that you can take a look at but you can also go ahead and skip this as well so I'm going to go ahead and click on import to import all the things and slowly Unity will import the whole joystick pack all right so our whole joystick pack has been imported let's go and close the window close the window and as you can see here inside the assets we have this joystick pack now inside which we have this prefabs scripts and Sprites and all those things so before we can use the joystick packs what we need to do is we need to add a canvas because these joysticks are part of a canvas okay so here we're gonna go to this here click on UI and from here go ahead and create a new canvas okay so as you can see here we have a canvas created and along with that we have an event System created as well filler because we don't want it to be built for only one screen size we want it to build for all the screen sizes so that it automatically gets scaled up for different screen size so canvas scalar scale with screen size set the reference resolution to 1920 and then click on Match value to 0.5 so that it automatically matches with the width and height in the same way okay so now that we have the canvas graded now we're gonna go inside the joystick pack go inside the prefabs and from the prefabs as you can see here we have some Dynamic joysticks fixed joysticks loading joysticks variable joysticks so you can go ahead and use any other joystick that you want to I'm going to go ahead and drag this fixed joystick right over the canvas right here and as you can see as soon as I do it automatically a new joystick has been created okay so if you want you can simply go ahead and change its size and change it to any other value that you want to but I think this looks cool for now so here we have our fixed joystick okay so now that we have our joystick done let's go and create a new script and add some player controls so that we can move the player so inside our scripts folder inside our own scripts folder I'm gonna right click and I'm going to create a new C sharp script and I'm going to name this one simply player because we need only one script in this game we don't need any more alright so now that we have the player script created let's go and select the rabbit and I'm gonna drag and drop the player script over the rabbit right here so here we have the player script now I'm gonna double click to open up the player script so let's go ahead and double click and open it up in Visual Studio all right so here we have our script opened so first of all we will need some variables so let's go ahead and create some of our variables so first of all we'll we'll need a reference to the fifth joystick that we have in the scene so here we're going to say public fixed joystick fixed joystick joystick so this is a variable that will hold the reference to our joystick so that we can take the input or take the movement input from the joystick so this joystick variable will hold a reference to the fixed drasticity that you have in the scene and then we can get the input from the joystick and according to that we're going to move our player so after that we will need another variable which will be public float move speed so this will be the speed by which we can actually move our player then we will need two more variables which will be float V input for H input and V input so this H input and V input will hold how much value we are getting from the joystick so let's say I am moving my joystick left and right so in that case the left and right movement or the horizontal movement will be stored inside this H input and if I am moving my joystick up and down in that case the vertical movement will be stored inside this V input okay so this way we will get the edge input and V input and according to that we're going to move our player in the horizontal x-axis or in the vertical Y axis okay so with that done now we're gonna go ahead and go to our fixed update function so here we're going to create our fixed update function so void fixed update and inside the fixed update function first of all what you're going to do is we're going to take our H input so we're going to say h input equals joystick Dot horizontal okay so what this will do is this will give us whatever input we are getting from the horizontal movement of our joystick that is the left and right movement of our joystick okay and then we're going to Simply multiply it with the move speed and that's it and the same way we need to say V input equals joystick dot vertical star that means multiply it with mob speed and that's it this way we have got the H input and V input as well so now all we need to do is we need to Simply move our player according to this H and V input and then outlayer will be moving so in order to do that we're gonna use a function called transform dot translate so you're going to write transform dot translate translate and for the X position or for the x value we're going to say h input for the Y value we're going to say V input and for the Z value we're going to input 0 because this is a 2d game so we don't have a z axis we only want to move our player in the X and Y axis that is why for the x value we can have given the H input for the Y value we have given the V input and for the Z we have given 0. so now that we have done this if everything goes right this should be able to move our player easily let me write up and down so let's go and save the code and see how this is working so let's go and save the code and go back to Unity all right so we are back inside Unity let's select the rabbit and as you can see here it is waiting for a move speed value and it is also waiting for a reference to the joystick so I'm going to drag and drop this fixed joystick right inside this joystick so this is very important otherwise it will not have access to the joystick and it will never know from where it will get the horizontal and vertical inputs then we need to give a move speed so for now I'm giving a very low value let's say 0.5 I think I need to lower the value even after that but for now let's check it with 0.5 so now if I click on play Let's see if I move my joystick and as you can see if I move my joystick left and right our player moves left and right if I move it up and down our player moves up and down so things are working pretty fine for now okay so as I can see things are working pretty fine and it looks pretty cool all right so now with that done so currently as you can see sometimes the rabbit is probably rotating while colliding with the carrot so in order to solve this what you can do is you can go to these constraints and from here click on freeze rotation to Z so now our rabbit will never rotate around Z so let's go and click on play and now as you can see I can move my rabbit left right up and down and I can collect the collide with the carrot and nothing happens so if you want you can simply go ahead and move with this value or if you want you can also change the value and make it much smaller let's say in this case I'm gonna make it about 0.08 or something like that and now if I click on play now you will see it moves much slower so if you want a behavior like that you can go ahead and do that or if you want a faster Behavior you can do it as well so I think I'm going to keep it like this and so now our rabbit is moving our player is moving and as you can see here we have our ground and now if I go ahead and select my rabbit and I can simply go ahead and drag and drop my main camera over the rabbit so I have dragged and dropped my main camera over the rabbit so now the main camera is a child of this rabbit so now wherever the rabbit will move the main camera will move along with it as well so now whatever the Rabbit Goes the main camera will move along with it as well so let me go ahead and press Ctrl Z and undo this to show you why do I need this so let's say here we have one carat so I'm going to select this carrot and right click and duplicate this and this time I'm going to select the move tool and move this carrot out of our boundary somewhere like this then I'm going to select this carrot and move it somewhere like this and then I'm going to select this carrot then duplicate it move it somewhere like this so now I have four let's click on duplicate and move this as well here so now we have five different cards in the scene and this is our camera view so now whenever the player will move and go out of the camera view we can not see the player in the game because the game will only see whatever the camera sees so now if I click on play and try to collect this carrot so if I go here over as you can see I am can I am I'm going to collect the carrot but I cannot see the player in the game so that is why we need to make the camera follow our player so the easiest way to do that is to make the camera a child of our player so that our camera will always move with the player so now I'm gonna select the camera and drag and drop it over the rabbit so now as you can see this main camera is now a child of this rabbit so now if I click on play now we'll see wherever I move the camera automatically moves and I can collect any carrot that I want to but in this way we also don't want to move beyond our boundaries so we're going to go ahead and create some boundaries so that I our player can never move beyond that boundary okay so let's see how we can do that so to create boundaries you can there are different ways by which you can create boundaries so in this case I'm going to go ahead and create them the simplest way so what I'm going to do is I'm going to go here and click on create empty game object and let's name it to bound one okay then I'm gonna go ahead and reset its position then I'm gonna go to add component physics 2D and I'm going to add a box color 2D so as you can see here we have a green Collider now I'm going to change the X size and make it much much larger something like this then I'm gonna drag it and move it somewhere like this so now we have a collider here that means our player will not be able to move Beyond this boundary now I'm going to duplicate this one and this time instead of 13.17 as its y value just put a negative sign just put a minus in front of it and now as you can see it automatically moves to the opposite side so now we have bound one here and Bound 2 here so the same way we're going to create some two more boundaries so I'm going to go ahead and create another new empty game object and I'm going to name this one bound 3 and this time again I'm gonna reset its position add component physics story and I'm going to add a box color 2D and this time I'm going to change the Y size to something bigger something like this and then I'm gonna move it to somewhere like this okay so now I'm going to go ahead and duplicate this one as well and this time simply go ahead and remove the minus sign so now as you can see here we have a bound one bound bound let's name it to bound two let's go and rename it to bound two this one will be bound three and this one let's rename it to bound four okay so this way we have all of our boundaries right here and I can create another empty game object I can name it boundaries and simply select all the boundaries by pressing the shift key and drag and drop them under the boundaries so now we have all the boundaries under this boundaries game object so now that we have the boundaries our player will not be able to move Beyond these boundaries okay so now all we need to do is we're going to detect the collation so whenever our player is colliding with the carrots we need to detect a collision and then we need to destroy that carrot and after destroying the carrot we need to increment our players score and then we need to check how many cards if we have in our scene let's say here we have five carats so if our player has more than 5 score that means it has already collected 5 carats then our player will win the game alright so it's as simple as that so let's go to our player controller and see what can be done all right so let's go and open up our player controller one more time so here we have our player controller script so now here what you need to do is you'll need to detect for Collision so here what you're gonna do is we're going to create a void on collision enter 2D function so this function gets called automatically whenever our player gets collided with any other game object and inside this Collision variable we have all the information about the Collision so we're gonna check if the collided game object has a carrot tag attached that means we have collided with a carrot and in that case we're going to destroy the carrot increment the score and check if the player has won the game so you're going to detect if Collision Dot Game object to attack equals carrot and here as you can see the C is capital and that is because we have written C capital in our tag as well so whatever spelling you have written in your tag that should be the same here as well otherwise it's not going to work so once we detect whether we have collided with the carrot or not we're going to go ahead and destroy the carrot game object and for that we're going to write destroy Collision Collision dot came object okay so whenever we collide with the carrot we're gonna go ahead and destroy the carrot game object and before destroying the carrot game object what you can do is we can simply go ahead and increment the score so for that here we're going to create two new variables one will be int score and it will be 0 by default and then we're going to create another game object which will be public game object and it will be our win text okay so by default this win text will be disabled and whenever our player wins the game this win text will be shown on the screen so that we can see if we have won the game okay so every time the player touches the carrot we're gonna say score plus plus that pretty much means we are incrementing the score so if the score is zero it will be 1 if it is 1 it will be 2 and this way our scores will be incremented after that we will destroy the carrot and then we're gonna check if our score is more than the winning score so in this case we have five carrots in the scene so that means to win the game we need to have five score so here we're gonna create another public float not float public int win score variable okay so if our score is more than the win score then we're gonna go ahead and say you win so here we're gonna check if score is greater than equal win score if our score is greater than or equal to win score then we're gonna do all the winning conditions so then we're gonna say win text win text dot set active to true okay so let me go ahead and try to explain to you explain it to you one more time so what we are doing here is we are giving a win score value so let's say here we have five carats so for each carrot we'll have one score so first one so when we collect the first one we get score one second two third three fourth four fifth five so when we have collected all the five carats we will get five score so in this case the value of our win score is five so when our scores value is more than 5 that is more than our win score then we're gonna display the win text on the screen on the screen we're gonna say U1 because you have collected all the five coins okay so that is what we are doing here so when we are colliding with the carrot we are incrementing the score destroying the carrot checking the score and then we are displaying if we have won the game or not so now our whole code is ready let's go and save this and go back to Unity and do all the necessary changes so now let's go and save it go back to Unity okay so now here if you select the rabbit as you can see here it is waiting for a win score which is currently zero so let's go and set the wind score to five for now not eight let's set it to five because we have five carrots in the scene and for the win text we're gonna select we're gonna create it later for now just let's go and check if the carrots are destroying or not and if it is working or not so let's go and click on play and try collecting the carrots so here we have the first one so let's go and collect it so first one as you can see whenever you collect it it automatically gets destroyed if we collect the second one it gets destroyed and if we're trying to move beyond the boundaries as you can see I cannot move beyond the boundaries so if I collect the third one so our score is now three collect the fourth one our score is now four so we have only one carat left so let's go and actually uh collect the last CAD as you can see as soon as I collect it we are getting an error which says the variable win text has not been assigned and that is because we have won the game and whenever we win the game a win text should be displayed but here we don't have a win text because we have not created it yet so let's go and create the win text so now our canvas I'm gonna right click go to UI and create a new image so here as you can see here we have a image let's name it win and now as you can see here it is saying Source image none because we don't have any Sprite attached so in my Sprites folder I have this you win text you can you you can download any other win text message from the Internet or you can create it by yourself and then drag and drop it inside this Source image so now as you can see here of the win you win uh written which is very small now we need to Simply click on this set native size so as soon as I click on it as you can see now it is showing pretty big so now I can go ahead and drag it not drag it double click it select the rec tool press the shift key and make the wind text much bigger and then put it in the center just like this okay so now whenever you will win the game this win text will be displayed and now now that we have this win text created now let's select the rabbit and here it is waiting for the wind text so let's drag and drop this win right here so now all the boxes have been checked the player script is attached the fixed joystick has been attached the move speed has been assigned the wind text has been given and the win score 5 has been created now let's select the win and disable it by default because by default we don't want to win the game of course when to play to win the game so now whenever we drag it we collect all the carrots outlayer will win the game so let's go and click on play and let's make the game more exciting so let's go and select the first carrot and let's go find the other ones so here we have the second one let's go and find the last three carrots so here this is so we have two more left so let's go and try to find this one and I think I have one more left let's go and find as you can see whenever I collect all the carrots it says you win text that means we have won the game so this game is pretty good so here we have learned how to detect collisions how to move joystick how to move your player with joystick how to display a text how to activate and deactivate objects and all these things so with that done now let's go ahead and make it a little more interesting so what you can do is you can select the carrots and as you can see you can go ahead and change their positions to make their game more interesting or hard for your player so I'm going to select the first carrot and move it somewhere like this and you can move ahead and move these cats as well and you can also duplicate and create some more carrots but whenever you create some more carrots you need to make sure you change the win score okay so let's go and create another carrot let's go and duplicate one more carrot so I'm going to go ahead and duplicate it and this time I'm gonna move it somewhere like this and now I'm going to select my rabbit and change the win score to 6 because now we have six carrots okay so whenever you add more carrots make sure you change the wind score okay now I'm gonna go ahead and select the canvas select the fixed joystick and as you can see here we have the joystick image so what I'm going to do is I'm going to make it a little bit transparent to make it look a little more professional as you can see currently it is showing a flat white color which I don't like so I can go to this fixed joystick go to this color and from the alpha value I can make it down so let's go and make it about 50 percent and the same way I can expand it select the handle go to the color and here as well I'm going to select the alpha and change it to about 50 percent change it about 50 something like that and if you want you can also make it a little bit bigger let's say 60 I think 60 or 70 or 80 I think something like that should be good as well so let's make it about 70. okay so here we have the 70 select the fixed joystick and set the alpha to 70 here as well and now I think it looks pretty cool okay so now we have all these things now our game is ready so now let's go ahead and finally test it on a mobile simulator so I'm going to go to this window go to General and go to device simulator and as you can see here we have the device similar open and for now as you can see it is showing an iPad so you can go ahead and test it as test it here as well but I'm gonna go ahead and select a Google pixel 5 which is an Android device and then from here I'm going to select here and rotate this device if you click on this option as you can see the device will be rotated and now I can sign I can finally play the game and test the game okay so now let's go ahead and click on play and now as you can see I can select this window and our game is running on a mobile device simulator so let's go and go ahead and collect the carrots one by one first one done the second one we are going the third one here it is the and we have three more left so here we have one more so the fourth one here we have the fifth one and we have one more I don't know where that is let's go and find the carrot here we have the last one let's go and collect it and as you can see as soon as I collected U Win text is displayed so we have won the game all right so this way we have created a completely working game which will work on any mobile device and here you have learned so many different concepts in this short time just by building this simple game you have learned how to create Sprites how to add 2D rigid body components how to add colliders how to detect collisions how to add a joystick how to display text on screen how to create a canvas and all these Concepts so with that being said I hope you really really enjoyed this video and learned a lot of new Concepts go ahead and build this game and show you to your friends and family so if you have enjoyed this video and you want to learn how to build some more cool games you can check out my Android game development courses all the course links have been given in the description of this video so you can go to the description take the courses build all the games and show you to friends and family publish into Google Play and become a better developer so thank you so much for watching this is Raja and I'm gonna see you in another video very soon hi this is Raja from Charger games and welcome to this video so in this video we're going to learn how to build a simple cool little balloon popper game so before we get started let's take a look at what kind of game we're gonna build so here as you can see I have the device simulator opened so I'm going to click on play and here as you can see we have multiple balloons coming here so if we click on this balloon as you can see the audio will get played and the score will get incremented and if I go ahead and miss any of these balloons you will see automatically the level will get reset as you can see the score is zero and the level starts from the beginning so I can go ahead and tap the balloons and increment this course and if I go ahead and miss any balloon automatically the level will get reset okay now in the video I'll show you the how to build the simpler version of this game and also a more complex version of this game with multiple balloons as well okay so I hope you're ready to build a game like this so let's get started now we're going to build it for Android but this is optional this game works on desktop and other platforms as well so with that being said let's get started so as you can see here I have an empty 2D project opened so if you have if you don't know how to do that you can check out my other tutorials so here I have an empty 2D Unity project opened so now what I'm going to do is as you can see I am in the two by three layout so from the layout I'm going to choose two by three so this is the layout that I'm going to use for this course and then from the scenes folder as you can see we already have a scene created I'm gonna go ahead and rename this scene rename this into game all right so this is our game scene so now as you can see inside the assets folder I have few folders created here we have a Sprites folder inside which I have only one Sprite and here I have only one balloon Sprite and inside the audio folder I have one simple balloon popping audio I have downloaded these things for free from the internet so you can go ahead and download them from free from the Internet or you can use this ones or any other one as well so you can see here I have a balloon popping sound which plays whenever I play here and this one I have downloaded from pixabay.com and the Sprite this one this balloon Sprite there are many free balloon Sprites available on the internet but I have downloaded it from this one from game developerstudio.com this is a great website to download free assets so you can go ahead and download this and thank you so much game developer studio for providing these free balloons for our for game building and from the pixabay I have downloaded this audio okay so with that done now let's go and start building the game alright so now that we have our assets ready let's go to file build settings and select Android then click on switch platform now if you are not going to build this game for Android then you can simply build it for Windows Mac and Linux this game will work on this platform as well but in this case I'm gonna specifically build it for Android so I'm switching to Android so it's optional you can either switch it or keep it to Windows Mac Linux so select Android click on switch platform and wait for Unity to switch the platform to Android all right so now as you can see our current platform has been switched to Android so with that done now let's close this window and from here as you can see from the resolution free aspect is selected from here we're going to go ahead and select this 16 is to 9 portrait so this is the resolution that you're gonna use for building this game okay so now with that done our next step is to select the main camera go to clear Flags make sure this solid color is selected and from the background you can give it any other color here I'm gonna give it a sky blue light color because I'm creating it like a sky if you are if you want you can also give it a yellow or orange or whatever color you prefer so in this case I'm going to give it a skylight color so that it looks like sky so something like this should look good all right so with that done our background is ready and here as you can see inside the Sprites folder we have our balloon so it's time to import our balloon so let's go ahead and save the scene by pressing Ctrl s now I'm gonna drag and drop the balloon right here and as you can see it looks pretty big so first of all let's go and rename this one to balloon okay so now I'm going to select this balloon sprite from here and for the pixels per units let's change it to 500 so that the balloon becomes small automatically so let's set it to 500 set the max size to around 256 and now if I click on apply you will see the balloon will automatically become smaller okay so now it looks pretty good for our game so I can double click to focus on the balloon so this is the balloon right here now I'm going to go to add component physics 2D and I'm going to add a rigid body 2D to balloon so let's select this rigid body 2D all right and now as you can see here we have a gravity scale that is set to one so I'm going to go ahead and set the gravity scale to zero because we don't want our balloon to be affected by gravity and fall down we want our balloon to go up so that is why we're setting the gravity scale to zero now we're going to go to add component physics 2D and this time I'm going to go to this circle collider 2D okay so now our balloon has a circle collider and this adding a collider is must otherwise we cannot touch or pop the balloons okay and now from the radius I'm going to make it a little bit smaller something like this I think something like this should be realistic for these balloons uh Collider let's set it to about 0.3 I think something like this is perfect okay so now whenever we tap within this collider the balloon will automatically detect collisions and then it will burst okay so with that done now I'm gonna go to add component and now we're going to add an audio Source component to the balloon so that we can play audio whenever the balloon gets popped so we're gonna go to add component audio and from here I'm gonna go ahead and add an audio Source component all right so now this balloon had an audio Source component and as you can see here we have something called an audio clip so what you're going to do is I'm gonna go to this audio folder where I have this audio already imported so you can go ahead and download any pop sound or any kind of sound from the internet and put it inside the audio and drag and drop it inside this audio clip slot okay so now this balloon pop sound is in the audio clip of this audio Source component that is attached to the balloon and makes your display on awake is not checked because if this player in awake is checked that means whenever the game starts this will play but that's not the what we want we want this audio to be played only when the balloon is popped okay so for that play one away should be disabled and now the balloon is ready okay so the last thing we do is we need to go to this scripts folder right click and create a new c-sharp script which will control the behavior of our balloon so let's go and create a new C sharp script and I'm going to name this one balloon or let's say make it balloon controller you can name it anything that you want in this case I'm naming it balloon controller so now I'm going to select my balloon and drag and drop the balloon controller over this balloon right here alright so here we have the balloon controller script so now let's try to understand what do we want to do with this balloon so let's select the move tool so in the beginning the balloon will be right here out of the screen whenever the game starts the balloon will slowly go up up like this and while it is in our game view if we tap it and pop the balloon then the balloon will go down again and from here it will start floating again from a random position so if we if it goes here and if I pop it like this it will go down go to a random position and from there it will come up again again if I pop it it will go down go to a random position and from there it will come up again so every time I pop the balloon it will go down and from there it will come again and if I miss popping the balloon that means if I somehow missed tapping the balloon then the balloon will go up and it will go out of the screen and then our game will be over and the scene will be restarted okay so every time I pop a balloon our score will be incremented and if I miss popping any balloon then our game will be over and their scene will be restarted or the level will restart again okay so that's how the balloon pop game will work in this case okay so with that done now what I'm gonna do is I'm gonna open up the balloon controller script so let's double click to open it up in Visual Studio all right so here we have our balloon script opened so first of all what we're gonna do is here we're gonna create a new variable which will be public float speed so this will be the speed by which the balloon will move up you can also name it up speed so for better understanding so let's name it up speed so this will be the speed by which the balloon will move upwards all right then we need another variable which will be int score which will be 0 by default later on we're going to change this score and make it much bigger than how it is all right so we have up speed and we have score as well then all then again we need a reference to the audio Source component that we have attached to the balloon and by using the audio Source we can actually play audio so here we're going to say audio source as a is a wrong name audio source and in small you can write audio Source okay and here we are creating a new variable of type audio source and then inside the awake function void awake void awake we're gonna get access to the audio source that is attached to this balloon so you're gonna say audio Source equals get component audio source so here what we are doing is as you can see as you have seen we have already added an audio Source component to this balloon now here we are creating a variable named audio source which can hold an audio source and here we're saying give us access to the audio Source component that we have attached inside this audio Source variable so now we can play audio or do anything with the audio Source by using this audio Source variable okay I hope that's clear and I think we don't need to start so for now we're going to delete this function and now we're going to come up here and come to our fixed update function so we're going to create void fixed update so inside the fixed update function what you're gonna do is we're gonna make the balloon go up for that we're going to use a simple transform dot translate function translate okay so using the Transformer translate function we're gonna make our balloon go up okay and to make it go up what you need to do is for the X we need to make it 0 because we don't want it to move in the X Direction but for the Y we're gonna make it move by speed value or up speed value and for the Z we're going to put it 0 as well okay so just by using this transform.translate function we're gonna move our balloon upwards by this up speed value using this fixed update function so automatically every frame the balloon will slowly go up okay so now that we have made the balloon move up now what I need to do is we're going to detect whether we are tapping on the balloon or whether we are clicking on the balloon or touching on the balloon for that there's a very easy function to use for that we're gonna use void on Mouse down and this function is very powerful just by using this function we can detect Mouse clicks touch clicks tabs and everything so whenever you tap on the game object it doesn't matter you tap it with mouse or you tap it with your fingers or whatever in touch devices you tap it every time it will be that it will get detected okay so whenever we tap our balloon with touches or our Mouse what you're gonna do is we're gonna destroy the balloon object okay but in this case we're not going to actually destroy we actually going to change its position and put it down to a random position so that it can come up again and every time it we tap the balloon we want to actually increment the score okay so you can say score plus plus so if you remember here we have a zero value given to the score so every time we tap a balloon our score will be incremented and after that we're going to reset the position of the mouse so for that what you're going to do is here we're going to create another new function which will be void reset not reset void reset position void reset position okay so this is the reset position function that we're gonna use to reset the position of our Mouse all right but before that let's go and test how this thing is working whether our balloon is going up or not let's go and test it and every time we tap on the mouse as you can see here we are incrementing the score value along with that let's go and press uh let's go and play the audio so that you can check if we are clicking on the mouse or not so what you're going to do is here we're gonna simply say audio Source dot play and that's it so whenever we say audiosource.play what it will do is whatever audio clip we have attached to the audio source that default clip will be played and in this case we have already attached the balloon pop sound so the balloon pop sound will get played whenever we tap our Mouse on the balloon all right so let's go and click on Save and Go back to Unity all right so now if I select our balloon as you can see here inside the balloon controller we have this up speed variable so what you're going to do is let's go and give it a very small value let's say 0.2 or something let's say 0.2 you can go ahead and experiment with different values and see whatever is suitable for you and next go ahead and put the balloon down and now if I go ahead and click on play you will see the balloon will go up and it is going up very fast so it's hard to click for us so let's go and give it a much lower value let's say 0.05 or something like that and now if I click on play let's see how it goes now it's good and if I click on it as you can see every time I click on it the audio is played and that is because in the balloons audio Source component we already have this balloon pop sound so whenever the balloon goes up we click it and as you can see the audio gets laid so now what you're going to do is whenever we click or tap on the balloon what do you want to do is we want to reset the position of the balloon to the bottom to a random X position so its y Position will be same let's say the balloon is going up we click it it will come down and go to a random X position then from there it will go up again then we click it it will go down and then it will go to another random Exposition again and from there it will go up again okay and if we miss touching the balloon and the balloon goes beyond this let's say the balloon goes this point as you can see currently the balloon's value is about 6.36 so let's say whenever the balloon goes beyond seven the value of balloons y position goes below I mean above seven at that time we want to say that game over happens and we will reload the game or reset the level okay so with that said let's go and reset the balloon to somewhere like this to its correct position and then we can go ahead and start coding so in the code now what you do is every time you tap on the balloon we need to reset its position and also increment the score Okay so let's go and do that so let's go and open up the balloon controller script where is that let's go and open up the balloon controller script here it is all right so now inside the reset position what you're going to do is first of all we're going to create a random Exposition so we're going to say float random X equals random dot range and what is the range let's go and find out let's go back to Unity and if you take a look at the balloon as you can see this is the range of the balloon as you can see if we take a look at the position currently it is a zero and if I move it to the extreme left the x value will be minus 2.5 and if we move with the extreme right the x value will be positive 2.5 so what you're going to do is every time the balloon goes down we want to actually reset its value to a random X position between negative 2.5 and positive 2.5 so the balloon will be spawned anywhere between here in this range okay all right so let's go and open up the script one more time let's just collapse the audio Source let's open up the balloon controller one more time and the random.range the minimum value will be minus 2.5 f and the maximum value will be 2.5 F now if you want if you want you don't need to hard code these values you can create a new variable and there set the value but here I'm just hardcoding it sir for quick doing okay so with that done now that that's done now the balloon will be reset to this random position I mean we have created a random Exposition now we need to set the balloons X position to this random X position and set the balloons of Y position to the bottom y position okay so let's go and create a another new uh variable uh or we can do it without creating a variable as well so here we're gonna say transform dot position equals new Vector 3 or you can also do Vector 2 as well because this is a 2d game so the third position or the Z Position will be automatically distracted or automatically it will be omitted whenever you create a vector 2. so for the X it will give random X and for the Y let's give minus 7f okay so as you can see whenever the balloon goes beyond -7f it already goes outside of our screen to the bottom you can go ahead and test it and give whatever value is suitable for your game or you can also create a new variable which will actually check how much you can actually give the value of the Y so in this case I am simply giving minus 7f in your case you might give it minus 9f minus 10 F depending on or your screen size or whatever game you are building all right so now here in the reset position we are simply creating a random x value and then we are setting the balloons exposition to the random x value and we're putting its y position to the 7 if that means it will go outside of a screen below the game View now whenever on Mouse down happens just after playing the audio we need to Simply reset position so reset position that's it so now whenever we click or tap on our balloon what will happen is it the audio will be played and the balloon will reset its position okay so now it should work so let's go and save this go back to Unity and check what's working and what's not so let's go back to Unity all right so here we are back inside Unity here we have our balloon and here we have our balloon script and now let's go ahead and put the balloon to bottom of the screen and set its exposition to zero and now let's go and click on play and you will see the balloon will go up and every time I click on it as you can see it is going down and every time I click on it as you can see a random position is created and from there balloon is getting up again so every time we touch it it is getting spawned from a random X position and it is going below the screen as you can see here every time I do it it goes beyond so now what you do is whenever we actually click on the balloon we want to show the score here so in the beginning it will be zero then it will be one two three and just like that and whenever the balloon goes out of the screen we want to reset or reload this level okay so these are the two things that you need to do to finish this game so let's go and do that so first of all we need to create a score texture which we can show up so for that I'm gonna go here go to UI and click on text mesh Pro text so please click on that and let's click on import TMP Essentials and it will import the text message Pro and create a new UI text for us all right so as you can see a new UI text has been created so let's go and close this window now select this text and from here let's go and reset its position now as you can see it is at the center now we need to set the select the canvas and change its UI scale mode to scale with screen size change the reference resolution to 1920 not 1920 this is a portrait game so we're gonna set it to 1080 by 1920 and then you're going to set the match value to 0.5 okay so this is the reference resolution which which we are working here then we're going to select our text you can go ahead and double click on it then we can select the red tool and change its size to much bigger value that we than what we have here so let's go and change it and make it much bigger something like this then you can simply rotate your mouse wheel to zoom out and then you're gonna drag it like here now we're gonna select the text and change its alignment to the center horizontally and Center vertically as well so now as you can see it is at the center now let's go ahead and change the text to 0 because in the beginning the scores value will be zero now let's go ahead and change the font size and make it much bigger than what we have here so let's go and make it bigger bigger bigger let's set it to about 300 I think something like 300 should be good okay so I think something like 300 should be good so if you want you can also make it bigger than this one but I think this one looks pretty good and then I'm going to Simply go ahead and set it to the correct position wherever I feel it is correct okay okay so if you want you can also make it much bigger than that I think 300 should be good for us so it is 300 and then I'm gonna go to this vertex color and change the alpha value and geek it a little bit Downer something like this just because I like this this is personal preference if you want you can also change the color to yellow or something like that or red but I think I'm going to give it to White okay so let's check it whenever the score will be incremented this 0 will be 1 will be 2 it will be 10 it will be 20 or something like that so in the beginning it will be zero all right so now our score text is ready and our canvas is ready let's go and rename this one to score text score text okay so here we have our canvas here we have our score text here we have our balloon so now what we do is every time the every time we tap on the balloon we need to actually uh increment the score value and show it on the screen and whenever the balloon goes up beyond the boundaries that means whenever the balloon goes more than y position of 7 as you can see if I select the balloon and double click it it is currently here and its y position is -6 if I go ahead and upwards and as you can see whenever it goes below 7 Beyond seven it automatically it already goes out of the screen so whenever the Y position of the balloon will be more than seven then we're gonna restart or reload the level so that the player can start playing again all right so with that done let's reset the balance position and set it to to the bottom right here now let's go and save it now we're gonna go and open up the balloon controller one more time so let's double click to open the balloon controller all right so now here what we need to do is we need to actually show up the score and in order to show up the score we need to use the UI elements and in this case we need to use the text mesh Pro UI elements so for that here we need to import a namespace we need to write using TM Pro so now we can use all the functions from the text mesh Pro Library and here we're going to create another new variable which will be public text mesh Pro ugly score text all right so this will hold the value of our score text okay so now whenever we are incrementing the value of score just after this we're going to say score text dot text equals scotex.txt equals score dot to string so let's go and understand what's this so this code text is the text that you have created in the game and this text is the text component of the score text UI so whenever uh we put 0 to the score value so this text value this code text will show zero so that is why what we're doing is as you can see here we're incrementing the score values in the beginning this will be zero so that is why 0 is shown in the score text in the UI score text as well so whenever it is incremented automatically we want to convert this score to its string because as you can see this one is an integer so we are converting it to a string and then we are setting the string to the value of our score text UI that we have in the game so now whenever the score will be incremented automatically the UI text score will also be changed to the same value so if it is 1 1 will be shown on the screen if it is 2 2 will be shown on the screen okay so with that done now our score text will be updated in the screen now the next thing to do is we need to check if our y position of the balloon is beyond seven then we're gonna reload the level or reset the level okay for that we're going to come to the update function and we're going to check if transform that position dot Y is greater than 7f that means if the Y position of the balloon is beyond seven that means it has already gone out of the screen then we can reset the level or reload the level and in order to reset the level or reload the level we need to use another name space in unity so here we need to write using Unity engine dot scene management okay so just by writing this now we can use all the functions related to scene loading and all that stuff so here we need to say in manager dot load scene and within that just by writing quotations we can simply write game and this will work so as you can see here in this case we have renamed our scene to game so whenever we write this the game scene will be loaded again so that our player can start playing it again now there's another way to do it and in this case I'm going to show it to you as well so let's go and comment this one out so just instead of writing this game level separately so you can also write seen seen manager dot load scene and instead of referencing a string name or name of a scene separately so you can also say scene manager dot get activesene dot build index get active scene is the function so get active scene and then we're gonna write a DOT and we're going to say build index okay so this one is a generic code that will work for any level that you create this one this code will work for only this level if you have named it game but let's say in your case you have in your game you have multiple levels so in that case you don't need to write game game one game two all that separately you can simply write this code and this could will work for any level doesn't matter whatever name it has so here what we are saying is we are loading the scene at a particular index and here we are saying just give us the active scene get the index where it is and load that level okay so if we are currently at the scene which has a built index of zero then the zeroth scene will be loaded if we are at a scene where the build index is one then the one number scene will be loaded okay so this is a generic code that will work for any level doesn't matter whatever you have named it so if you are if you are uh thinking that this one is simple to use you can use this one or if you want to use a more generic code that will work for everywhere you can use this code as well okay so with that done now everything should work here we are uh making the balloon go up here we are detecting tabs taps or touches or clicks to the balloon whenever we tap on the balloon we are incrementing the score displaying it on the screen playing the audio resetting its position to a random position and then we are whenever the balloon goes beyond seven we're going to reload the level okay so with that done I think everything should work right let's go and save it and go back to Unity and test our game so let's go and save it go back to Unity all right so let's go and test it and check how the game is working so as you can see here we have this if you select the balloon here we have the balloon controller script and here it is waiting for a score text reference so here we're gonna drag and drop our score text because otherwise it will not it will not update this code text and it will give us an error so we are dragging and dropping the score text here so now if I go ahead and click on play now the balloon will go up if I tap it if I try to tap it as you can see every time I tap it the score will be incremented and if I tap it again the score will be incremented and whenever the balloon goes outside of our boundaries let's see as you can see automatically the scene gets reloaded because we see Zero here now so one two let's try to do it one more time three four and if I miss any balloon if it goes up let's say if the balloon goes up and we miss it automatically the scene will be reloaded and we will start from zero again so this is how the whole game is working okay so the whole gameplay is working now there are many things that you can do here before saying that let's go and test it on a mobile simulator so see to see how this one is working there so I can go to window General and device simulator so here we have the device simulator so if you want you can also test it on the iPad or if you want you can also test it on Android phone as well so this time let's go and try to test it on iPad so see how this one is working so let's click on play and you will see the balloons will come one by one by one so the first balloon comes I tap it The Sound plays hope you are able to hear the audio and every time I do it the score gets incremented and as you can see the score gets incremented and whenever the balloon goes outside of our boundaries as you can see automatically the game will get over okay so now let's try it on Google pixel 5 as well so this is the Google pixel 5. let's try to play it here click on play here the balloon goes up I tap it and the score gets incremented and this way I can go ahead and keep playing the game and whenever I miss a balloon let's say whenever the balloon goes beyond the boundary automatically the scene gets reloaded so this is how the whole game is working now this game might be simple very very simple but as you can see you have learned so many Concepts you have learned how to move the balloon or any object upwards you have learned how to detect collisions tabs or touches you have learned how to create a new score system where you can display your score and increment it you have learned how to create a random position and spawn your object there and you have also learned how to create the canvas how to create this code UI and all these things so just by building this simple cool game you have learned so many different things okay now there are many things that you can add to make this game even better so what you can do is you can create just by just like this balloon you can go ahead and duplicate it and create another balloon which is of yellow color you can create another balloon which is a green color now all the balloons will have the same script so what will happen is every time you tap a balloon automatically it will go down and from there will go up again so you will see multiple balloons coming here and the game will be more challenging to do okay so let me go ahead and demonstrate to you to show you what can be done so let's go and duplicate this balloon for now so I'm duplicating this balloon I'm duplicating this balloon one more time so for the second one I'm putting it somewhere like this and for the third one I'm gonna put it put it somewhere like this okay so this way as you can see we have multiple balloons now so now what we need to do is we're going to select the second balloon as you can see here we have the score text already selected and the third menu as well we have the score text already selected so let's go and click on play and you will see now the first balloon goes the second balloon goes the third one goes and automatically as you can see multiple balloons are coming towards us and this way you can create already a playing game okay so let's go and test it on the device simulator as well because I like playing it in the device simulator so here we have the device simulator let's select Google pixel 5 or any other device that you prefer so now we are in the simulator now let's go and click on play and you will see multiple balloons will come to us one by one by one and every time I click it the blue will go down and the score will get incremented and it doesn't matter where the balloon is if I miss any balloon automatically our game will get reset okay so let's try missing some balloons and whenever I miss some balloons automatically the game will get reset okay so this is how the game is working all right you can go ahead and add some multiple colored balloons here as well so to make the game look even cooler and thank you so much for watching this is Raja from Charger games I'm gonna see you in another video very soon hi everyone this is Raja and welcome back to this video so in this video we're gonna learn how to build a simple cool Android game start to finish within a very short time so before we get started let's take a look at what you're gonna be building at the end of this video so as you can see here we have the device simulator opened so I'm going to click on play and as you can see this is the game this is the block Dodge game I can simply click on tap to start and automatically the player is here I can tap on the left to move left I can tap on the right to move to right I can dodge the blocks and if I collide with the blocks automatically the game gets restarted I can tap again and move it like this and as you can see here we have the score which gets updated and this way the game keeps running so this is a very simple game but it is it looks really cool and just by building this simple game you will learn how to build a complete game start to finish step by step and the explanations will be less because we're gonna go really fast so I hope you're really excited so let's get started building this blog Dodge game so as you can see I have Unity opened I'm going to go ahead and click on new project then from the top I'm going to select the latest version of the unity that I'm going to use then I'm going to select 2D because this is a 2d project then here I'm going to choose the location where I'm going to save my project and here I'm going to give a name so let's say give name to 2D Android project or 2D Android game anything that you want here we're going to select the location all right so I have changed the location then I'm going to click on create project and our project will be created and I'm going to wait for it to load so as you can see our project has been loaded now we're going to build this project for Android so make sure you have Android build tools installed in your computer so if you go to Unity Hub go to installs and select the version click on the gear icon click on ADD modules and make sure you have this Android build support installed otherwise you will not be able to build for Android so once that's done simply close this and now here we have our new project opted now if you don't see this kind of outline then what you can do is you can go to this layout and simply choose this two by three layout and then you can see something like this all right so with that done now I'm going to go to this file build settings and here as you can see Windows Mac and Linux selected I'm going to select Android and simply click on switch platform to switch our current platform to Android all right so as you can see currently the unity logo is beside Android that means our current platform is Android and then you can simply close this one and at the top you can also see Android written that means our current platform is set to Android all right so now I'm gonna go to this game Tab and from here as you can see free aspect is written from there I'm going to come here and simply choose 16 is to 9 portrait so this is the mobile resolution that we are targeting if you want you can also use other resolutions if you want you can also choose 1080 by 1920 but here I am simply using an aspect ratio all right so now as you can see here we have our project tab inside that inside the assets folder we have scenes inside this we have our default scene I'm gonna right click and rename this scene to game g-a-m-e all right so with that done now I'm going to go ahead and create few folders here where I'm gonna import some of our assets so first of all I'm going to create a new folder I'm going to name this one Sprites create another folder I'm going to do this one Scripts okay and create another folder and I'm gonna name this one prefab okay so this will be enough for now so I'm going to select the Sprites folder and go inside it here we're going to import a 2d Sprites that you're gonna use for our game so here as you can see some of the Sprites that you're gonna use in our game so I'm going to select all the Sprites one by one by one and drag and drop them inside the Sprites folder okay so these are the Sprites that you're going to use in our game I have downloaded them for free from kenny.nl gamer2d.com and I have created this one by myself so if you want you can go ahead and download them from anywhere from the internet so with that done now we're gonna go ahead and drag and drop our first asset into the scene so first of all I'm going to go ahead and drag and drop this tile this is ground trial into the scene I'm going to drag and drop this one as you can see here we have the ground tile I can simply move the ground tile to the bottom if you want you can select this move tool and move it to the bottom make sure it is at the center so 0 for the X and move it a little bit to the bottom like this as you can see this is the game view so you need to make sure that you can see only some part and some parties outside of the screen now I'm going to select this right I'm going to rename this one to ground okay now from the Sprite renderer I'm going to go ahead and select the draw mode from simple to tiled so the draw mode is now tiled now I'm going to select this direct tool and simply drag it at this side and drag it at this side okay so now as you can see we have selected the tiled mode and that is why the ground was styled like this so now our ground is ready so with that done I'm going to go ahead and drag and drop our player into the scene so here as you can see this peak this is what I'm going to use as my player so I'm going to drag and drop it in our scene it is pretty big so I'm gonna click here press the shift key and simply drag it to make smaller make it smaller like this I think something like this should be okay even if you want you can make it even smaller I think something like this should look good okay simply go ahead and position it somewhere like this now as you can see the pig is behind the crown in order to solve this you can select the pig go to the already layer from the Sprite renderer and set it to one so now as you can see this the peak is at the front so I'm gonna put it somewhere like this then I'm gonna make the X position to zero and this is our pick so I'm gonna go ahead and rename this one to player so player and that is our player all right so with that done now I'm going to go ahead and set this bigger so I'm going to go ahead and click on ADD component physics 2D and I'm going to add a rigid body to it to it so here we have a rigid body 2D and I have added it so now it will be able to interact with the physics engine of unity now I'm going to go ahead and set the gravity scale to 0 angular drag to 0 so that it doesn't fall down automatically then I'm going to go to this constraints and change the freeze rotation to Z and you can also freeze position on y as well okay so with that done now our player is almost ready I'm gonna go ahead and add a physics 2D box collider component because we want it to collide with other game objects so here we have a box collider and if you zoom in as you can see here we have the green line which is the Box collider so what you can do is you can simply click on this edit collider button and you can simply drag these handles a little bit to the inside to make it smaller because we don't want it to be so big so something like this should look okay for our player so now our player is almost ready so here we have our player and it is almost ready now I'm going to select the main camera go to the background and set the color to something like a sunny Sky color if you want you can also set it to yellow green black whatever you prefer but I think I'm gonna set it to some Sky color because I like this it feels like the game is happening in a way sunny weather so with that done our player our ground on our background is ready now we need to set up our obstacles for that here we have something called create so we're going to drag and drop this great object here and this is going to be our obstacle so if you want you can also make it a little bit bigger I think I'm going to press the shift key and make it a little bit bigger something like this so now I'm gonna go ahead and rename this one to let's say uh block okay and then I'm gonna go to add component physics 2D I'm going to add a rigid body 2D to it rigid body 2D then I'm gonna go to add component physics 2D and I'm going to add a box collateral to it so box collider 2D so same way we have added a rigid body 2D and also a box collider 2D so with that done now we're going to select our block go to this tag click on ADD tag and from here I'm going to click on plus to create a new tag and I'm going to give it a name of block okay now I'm going to select the block go here and choose block so now our block has a rigid body 2D a box square 2D and also a block tag attached so our player or block is ready if you want you can simply move it upwards and then click on play and you will see it will slowly fall down so click on play three two one as you can see it automatically falls down because of the gravity and if you move it to the left it automatically falls down below because then it will not Collide to the player okay so block is ready and our player is ready so now what we're gonna do is I'm gonna go to the scripts folder and create few scripts that we're gonna use in this case so I'm going to right click create a new C sharp script and I'm going to create a script named player okay then I'm gonna create a another C sharp script I'm going to name this one game manager just like this then I'm going to create another C sharp script and I'm going to name this one block okay so here we have a Blog script game manager script and our player script as well so now I'm going to select the block and in our blog we're gonna simply drag and drop our blog script okay if you want you can also go to this add component scripts and from here attach the block script so any way you prefer so here we have a Blog script attached so let's go and double click to open it in visual studio and write our first code so double click to open it all right so now as you can see our blog script has opened up in visual studio so now we're gonna write our very first code so here this is a simple code that need to write we don't need this start function so we're going to go ahead and delete this one so inside the update what you're going to do is we're going to check if the block is outside of the screen then we're going to delete it that's all we need to do in this code so we're going to say if transform dot position transform dot position dot Y is less than minus 6 F so it is below our camera level then we're gonna simply go ahead and Destroy game object okay so whenever our position goes outside of the screen and below the screen then you're gonna simply destroy the Box otherwise there will be so many blocks cluttered into the screen and they will never get destroyed so now that we have written it I'm going to save it go back to Unity and now that we have the script attached to the block you will see whenever I click the block will fall down go outside and as you can see automatically it got destroyed so now whenever I click the block will automatically get destroyed all right so now our block is ready now what you're going to do is we're going to go ahead and make it a prefab a prefab is a game object which we can reuse again and again and here we're gonna use this box as our obstacle again and again and that is why we need to set it as a prefab so you're going to go to the prefab folder and drag and drop our block right here now as you can see the block becomes blue that means it is now a prefab so I'm going to disable this from the scene because we don't need it in the scene anymore because we already have it in the preview app all right so now with that done now we need to write the code to make our Player move so let's go and start writing that so I'm going to go to the player and from here I'm going to go to add component scripts and I'm going to add the player script to the player now I'm going to double click to open the player script up and start editing it so here as you can see we have the player script opened so let's go and start coding so first of all we will need few variables here so for that here I'm going to go and write public float move speed so this will decide how much faster the player will move then we'll need a reference to the rigid body 2D that we have attached to our player so we're going to create a variable called rigid body to the RB so this will give us a reference to the rigid body that we have attached to the player and it will help us to move the player left and right now you can also create a wave function but here you can simply go and inside inside the start function right RB equals get component rigid body 2D so now we will have access to the rigid body 2D component that is attached to the player inside this RB variable so just by using this RV variable we can move the player do all the physics functions and perform all the fixed operations on the player okay so with that done now what you're going to do is we're going to come inside the update function inside the update function we're gonna check if our player is tapping on the left side of the screen then we're going to move the player left and if it is tapping on the right side of the screen then we're going to move the player to the right in order to check that first of all I'm going to write if input dot get mouse button down not button down with mouse button zero so this function gets called again and again whenever we are pressing on the screen with mouse or tapping on our screen with a touch finger in an Android device so whenever you are continuously pressing on the screen this function automatically gets called and whenever this function gets called this if condition will become true and then we will move our player now else that is whenever we lift our finger we are not going to move our player and set its velocity to zero okay so whenever we press the button we can move our player and whenever we are not pressing the button we can set the velocity to zero so here we're gonna say else RV dot velocity equals zero so whenever we are not tapping on the screen or touching on the screen we're going to Simply make the velocity variable for RB to 0 not 0 we're going to write Vector 2.0 so whenever we are not pressing our fingers our velocity will be zero and whenever we are pressing our finger first of all we need to check at which point we are pressing our finger or at which point we are clicking on Mouse in this case we are simply checking at which point we are pressing our finger for that we can create a new variable Vector 3 touch pose and inside that we're going to write input dot mouse position so this mouse position variable will give us the position where our Mouse is currently or where our touch finger is currently whenever we are pressing on the screen okay now what we will do is we need to convert this mouse position from screen point to World point and whenever we convert it to the bullet point automatically our Center becomes zero the left becomes negative and the right becomes positive so it becomes very very easy to handle the touch inputs and control the player so that is why we need to convert this one from screen point to World point to do that here we're going to Simply write camera dot main dot screen to World point and inside this function we're going to Simply put our input that Mouse position okay so so simply say we are getting the mouse position which is in screen points then we are converting it to the world points so that the center becomes zero the left becomes negative and the right becomes positive and then we are putting it inside this touch pose variable so now inside the touch force variable we have the position where we are touching on the screen okay so now we need to check if touch force touch pose dot X is less than 0 that means if we are pressing on the left half of the screen or else if we are pressing on the right right of the screen so whether we are pressing on the left top of the screen or we are pressing on the right of the screen so if we are pressing on the left half of the screen then we need to move our player to the left for that we're going to write RV dot add Force Vector 2 dot left multiplied by move speed so whenever we are pressing on the left side of the screen we can add Force to the left and whenever we are pressing the right side of the screen we're going to say RB dot add force and we're going to write Vector to the right or you can also write vector2 dot left negative okay so in this case we're going to write Vector 2 dot right and we're going to multiply it with move speed okay so whenever we are pressing on the right side of the screen we are adding a force at the right side with this move speed value and whenever we are pressing on the left of the screen we're adding a force at the left side so that our player moves to the left and whenever we are not pressing on the screen we are simply making the velocity to zero so just by writing this simple code now we will be able to move our player by touching left and right above screen so let's go and save this code and go back to Unity all right so as you can see here we have a player and here we have our player script and now we have this move straight variables for now let's go ahead and make the value 5 we want you can also give it some other values and now if I go ahead and make it a bigger little bit bigger click on play now you will see whenever I press the laptop the player slowly moves to the left whenever I press to the right the player slowly moves to the right and if I simply left leave my mouse the player automatically becomes zero or the velocity becomes zero so this way I can simply go ahead and make the player move left and right just like this and control the player if you want you can also change the value to little bit higher values let's say 10 and then you can click on play and as you can see it moves a little bit faster so according to your game whatever you prefer you can set the value to that and then it will be good to go but another thing you can see that whenever we are pressing left the player can move beyond our boundaries so this is not something that we want so that is why we're going to create some bounds to stop the player from moving to the left and right outside of our screen so now let's create some boundaries to stop the player from moving outside of the screen so as you can see here we have our game view not our game view our scene view so here I'm going to go ahead and click on create empty and I'm gonna simply leave it like this so this will be this is our empty game object I'm gonna go ahead and reset its position then I'm going to select the move tool move it to the bottom somewhere like this then I'm gonna move it to the left somewhere like this so that it is on the edge of the left side of the screen now I'm going to go to add component fill studio and I'm going to add a box color to the component so now as you can see here we have a box collided 2D in green so now what I'm going to do is I'm going to Simply go to this edit collider button and from here I can manually make it smaller or I can make the X size smaller 2 from X to 0.5 0.5 as you can see now it is thin and I can also make the y a little bit higher something like this and then I'm going to position this game object a little bit on this side somewhere like this at the edge of the screen so this is the position and here we will have this collider which will act like a wall so that our player can never move Beyond this so now I'm going to go ahead and duplicate this one and this time I'm going to select this game object and from the X position I'm gonna simply remove the negative so that it automatically moves to the right so this game object is at the left and this game object is at the extreme right so both of them will act as the boundaries of our game so now I'm going to click and create a new empty game object I'm going to name this one walls or you can also name is boundaries now I'm going to select both of these game objects and I'm going to drag and drop them inside the wall okay so now both of them have become the child of this wall so this is the wall game object and both of them have now become the child of this wall okay just for organization purpose so now that we have created the was let's see if our player can move Beyond this one so now I'm going to go ahead and click on play and now as you can see whenever I click the player can not move Beyond this because we have the boundary if you select these walls as you can see here we have the walls and whenever the player moves here it cannot move beyond the boundaries okay so our wall is working and our player cannot move Beyond this so with that done now our player functionality is almost ready so now we need to write some code so that the player can check whenever it collides with the block and then we can restart the game okay so let's go and open up the players player script and here outside of all these things what you're gonna do is outside of the update function this is the update function so make sure it is and here outside the update function here we're going to create the void on collision enter 2D function so this on collision Integrity function gets called automatically whenever our player or this game object gets collided with any other game object so whenever it gets collided with any other game object we're gonna check if collision.gameobject dot tag equals block so whenever it collides with any game object we're gonna check if that game object is tagged block that means we have collided with the block then what you're going to do is we're going to Simply reload the scene so that we can restart the game because the game is now over and in order to reload the scene here we need to first of all import some new things so you're gonna write using Unity engine dot scene management so this will help us to manage the scenes and reload the scenes so with that done now here we're gonna write scene manager dot load scene and inside that we're going to Simply write game within quotations or you can simply write zero as well so because we have only one scene currently in our build so that is why the zeroth signal the first scene will load automatically or you can also simply go ahead and within quotations right game which is the current name of our scene so whenever our block collides with the player it will automatically reload the scene and our game will get reset okay so let's see if that is working so let's go and save this and go back to Unity all right so now let's go ahead and test it so as you can see here is the block prefab I'm going to go ahead and activate it for now just for testing I'm gonna position it over our player now I'm going to click to play and automatically falls down as you can see as soon as the block falls down automatically the game gets restarted so whenever the block falls down let me try it one more time whenever the block falls down automatically the game gets restarted so every time the block falls down the game gets restarted so our code is working okay and you can see that whenever the block falls down outside you will see the block falls down and automatically gets destroyed but it doesn't really start the scene so our restarting code is pretty much working and our player code is almost finished so now what you do is we need to write a game manager code so that we can play the game again and again we can start the game whenever we tap for the first time and you can also keep the score so let's do those things so now let's go and disable the blog because we don't need it right now so here I'm gonna create another new empty game object and I'm going to name this one game manager okay manager just like this and then I'm going to create another new game object empty and I'm going to name this one our spawn point so this is the point from where our blocks will keep spawning so I'm going to select spawn point and reset its position now I'm gonna move the spawn point over somewhere like this outside of our camera view so that it can spawn blocks right from here now what you're going to do is we're going to create random spawn points from here we're going to create random spawn points over the x axis like here here here here here here here and randomly spawn blocks from here here so that our player can dodge blocks from anywhere okay so whenever we gonna reference this spawn point we don't need to change its y or Z position we only need to create a random X position on this line and then we can spawn the block from there and the block will fall down over the player okay so let's see how we can do that so I'm going to select my spawn point make its X position to zero then I'm going to select the game manager drag and drop the game manager script over it then double click to open up our game manager okay so here we have our game manager so let's go and first of all we need some variables that we gonna use here so first of all we're going to create a public game object block variable so this block variable will hold the reference to our block so that we can spawn the block and instantiate the block then we need a public float Max X variable this variable will decide the limit up to which point we can spawn the block on the x-axis okay on the left and right then we need a reference to public transform transform spawn point spawn point so this will hold the reference of the spawn point so that we can actually understand at which position we need to spawn the game object then we need another function public float not function a variable public float spawn rate so this bond rate will decide how fast we want to spawn the blocks let's say we want we spawn the first block then after one second we spawn the second block so that's what will be decided using this spawn rate okay so with that done now I think most of the things are ready so now what we need to do is here we need to create another new variable let's say we're going to name this one Bool game started so this will decide if our game has started or not and by default it will start it will be false and you can also manually set it to false and in this case I think we don't need the start functions I'm going to go ahead and delete this one so in the update we first of all don't need much things so later on we'll need something so first of all what you're going to do is here we're going to create a new function we're going to name this one for it spawn block so this function will be useful to spawn the blocks okay so first of all what you're gonna do is here first of all we need a position where we can spawn the blocks so for that we're going to write Vector 3 spawn pose spawn Force equals spawn point dot position and if you remember what is this spawn point this spawn point is the spawn point transform and here we will add the reference to the spawn point which we have created in the editor the spawn point variable okay or the spawn point object so here we have got the position of that spawn point now we need to create a random x positions over that spawn points x-axis so that we can spawn randomly blocks on the different positions and it will look that random blocks are spawning at random positions so for that we're going to write spawn point dot X so we only need to modify the X position of the spawn point as we have discussed already and here we're going to write random dot range so we're going to create a random value between minus Max X and positive Max X So within negative Max X and positive Max X we're going to create a random value and then we're going to put the random value inside this spawn point dot X variable okay so if we do that what we'll do what will happen is not spawn point actually I have made a mistake spawn pose spawn post.x so now we are creating a random X position over this spawn position and then we need to instantiate our block at this random spawn position to do that we're going to write instantiate within that we're going to write first of all what you're going to instantiate we're going to instantiate the block then at which position of course the spawn position that we have created the spawn pose and then we need to write the rotation which will be the default rotation that is quaternion. identity so because we don't need any external rotations in this case so this way whenever the this is not start I think I have made some mistake this will be spawn block okay so inside the spawn block what we are doing is we are creating a random spawn position then we are creating its random X position using this minus Max 6 and positive Max X then we are instantiating the block at the random spawn position so that our block gets spawned from random positions okay so with that done now our uh things are almost ready now I wanted to do is here we'll to repeatedly call this spawn block function because this formula function will spawn the block only once but we need to call it repeatedly again and again so that it gets called again and again for that here we're going to create another function we're going to call it void start spawning this is the name of the function and here what you're going to do is we're going to write invoke repeating so this invoke repeating function gets called can call any function again and again after a certain rate so first of all we need to write which function we need to call so within quotations we're going to write spawn block exactly as it is written here so it is best if you can go ahead and copy this from here and paste it right here because if you don't write it exactly like this then it will not work so spawn block then we will write after how much time this will start calling the spawn block function so for that we're going to write 0.5 because after just half seconds we want to call then we need to write how many times or after how much second it will get called again and again and again and again for that we're going to use the spawn red variable that we have already created so let's say the spawn width value is 1 that means first of all it will get called up to 0.5 seconds then after one second it will get called again after one second it will got called again after one second it will get called again so after every one second it will get called again and again and our blocks will be spawned one by one by one at random positions whenever this start spawning function is called all right so now all we need to do is we need to call this start spawning function whenever the game starts and let's see how we can do that so for that inside our update what you're going to do is we're going to say if input dot get mouse button down zero so that means whenever we tap on the screen or click on the screen for the very first time then we want to start the game okay so whenever we are tapping on the screen and the game has not started so that means if not game started so that means the game starter is false as you can see in the beginning the game start is set to false so in the beginning the game start is false so that is why whenever we tap on the screen for the first time we want to start spawning and then the game will be started so the game start will be true and then it will be not called again and if you don't give these conditions then what will happen is every time you every time you click on the screen automatically this start spawning function will get called but we want to call it only once at the beginning of the game so that is why we write if we are typing on the screen and the game has not started yet then we want to spawn this okay so whenever this happens what you can do is we're gonna call this start spawning function so we're going to write start spawning start spawning and this function will get called and then we need to Simply write game started equals true so now whenever we tap on the screen for the first time the start spawning will be called and the game started will become true okay so this code should work and these things are already set up so let's go and save this code go back to Unity add the references and see how this one is working so let's go and save this go back to Unity all right so now as you can see here we have the game manager and it is asking for some references so first of all we need to give a reference to the spawn point and as you can see here we have already created a spawn point so I'm going to drag and drop the spawn point right here next we it is asking for the spawn rate so let's say for now give the responded to 1 that means after every one second a new block will be spawned next we need to give a reference to the block game object as you can see here so from that we're going to go to the prefab folder and drag and drop the block right here and make sure you drag and drop it from the prefab and not from the scene because from the scene if you drag and drop it it will get destroyed and then it will not work again so only drop it from the prefab so that we can reuse it again and again next we need to give a Max Exposition which will be the maximum point up to which we can spawn the block so for that let's say I select the block and I can simply go ahead and enable it for now and let's as you can see whenever I move to the left let's say this will be the extreme left position and as you can see current position is minus 2.24 so let's say I can spawn it up to 2.25 okay so that will be the minus maximum position and the positive maximum Position will be 2.25 so now we're going to disable it so I'm going to go to the game manager and set the max X to 2.25 so this will be the maximum position up to which our blocks can be spawned so with that done now I'm going to go ahead and save this and let's see if our code is working so let's go are you ready three two one click and now as you can see our blocks start spawning at random positions and they get destroyed automatically and whenever I collide with the player as you can see the scene gets restarted so every time it collapses the player the scene gets started but it will start working only when I click on the screen for the very first time so as you can see currently no blocks are spawning but if I tap it start and the blocks start falling and the game starts and now as you can see the blocks are falling I can Collide and the game gets restarted so the main mechanism of the game is actually working pretty fine so now what we need to do is we need to add some scores to make it some interesting and we need to add some UI so that it can start tapping and we can create the menu so let's do that now so what you can do is as you can see here inside the Sprites folder I have this tab to start I can go ahead and drag and drop it right here if you want you can also use any other things that you want but in this case I have created this step to start function tap to start image by myself so I am using it and as you can see it is giving it some error so so what you can do is you can select it from the import settings select the Sprite mode to single and click on apply and once you've done that simply drag and drop it once again and now it will work so I'm going to delete the previous one and now I'm going to use this one so this will be our step to start text if you want you can also make it a little bit smaller or bigger so this text will be here in the beginning and later on it will go away okay so this is our tap to starts text now what I'm going to do is I'm here gonna create a UI and I'm going to create this UI text mesh Pro text so you can click import TMP Essentials so as you can see the TMP has been imported so now as you can see here we have our canvas and along with it we also have this text message Pro text so I'm going to go ahead and rename this one to score text so score text and as you can see currently you probably cannot see it in the screen so select this score text go to this direct transform and reset its position now as you can see it is at the center so first of all what you need to do is we need to select the canvas go to the UI scale mode and select scale with screen size give the reference resolution to 1080 by 1920 or any other resolution set the match to 0.5 so that our UI gets scaled automatically to different screen sizes okay so with that done now as you can see if you double click on the score text you will be directed to the score text on the canvas so make sure double click it now as you can see here we have our score text now we can select the red tool and dragged it and drag it to make it bigger something like this on this side and also drag it to make it bigger on the upper side as well so with that done now as you can see here we have our score text I can simply go ahead and make it move it to the top now I can go ahead and set it to zero set this text to zero because this will be your score and it will be zero then I'm going to go ahead and change the font size to much higher value as you can see currently it's 30 I'm going to make it about 100 or let's say 300 I think 300 should be good for this point okay 300. now we need to put it to the Center for that from the alignment I'm going to select Center from here and Center vertically as well so now our text is at the center and automatically whenever the score gets incremented we want to set it to 1 to 300 000 and all these things so in the beginning it will be zero just like this now I'm going to select the vertex color and change the alpha value to a little bit down so just to make it look a little bit cooler if you want you can also keep it to zero and you can also change the uh color if you want to I think I'm going to make the alpha value something like this because this looks good and I like this okay so with that done our score text is ready so now all we need to do is we need to actually write code so that our score gets incremented and automatically it gets updated on the score text on the screen so to do that I'm going to select the game manager and let's go and open up the game manager script so inside the game manager script we need some more references right here so first of all here we're going to create a new reference we're going to call it public game object text so this will store the reference to the tab text and it will disable it whenever the game starts then we're gonna say text mesh Pro public text mesh Pro ugly and for that we can create a variable called score text now whenever I wrote this text message for Yogi as you can see you automatically it imported the text message Pro in your case in if your editor didn't import it you need to write using TM pro at the top otherwise it will not work so make sure to write using TM Pro then public text message Pro UV score text so this score text variable will store the reference to the score text that you have created now here we need to create another public variable not public variable to Simply write int score equals zero so this will be our score variable now we need to find a mechanism so that the score gets incremented automatically to do that what you can do is every time we spawn a new block we can automatically increment this code value for that here we can write whenever we are spawning a block we can write score plus plus so this will simply increment the score value next we also need to update the score value on the UI element for that we're gonna write score text dot text equals score dot to string so here what you're doing is this score variable is an integer we are converting it to a string otherwise it's not going to work then we're setting out UI text value we are setting the text value of our UI element that we have created on the screen to this curve value so whenever it will be one it will become one it will be two it will become 2 and automatically our score will change on the screen so most of the things are almost ready another thing you need to do is as you can see here we have this tab text inside this game object so whenever the game starts we need to disable the tap text so here we're gonna say tap text Dot selective to false so whenever the game starts we want to disable the tab text and start the game and we're going to increment the score one by one by one and then we need to keep the game going so with that done let's go and save this code go back to Unity okay so now as you can see here we have our score text you have a game manager it is it is waiting for some references as you can see first of all we need to give a reference to the tab text so I'm going to go ahead and drag and drop this tab to start text right here then we need it is asking for a score text so I'm going to drag under the score text right here so make sure to drag and drop these references correctly otherwise it's not going to work so now with that done let's go and click on play and as you can see in the beginning the tab to start is showing and whenever I click the first time the tabster goes and the game starts and our score gets incremented one by one by one and whenever the block Falls or score gets increased and if I go ahead and Collide automatically the game gets restarted so our code is working out game is working and it is a complete really fully functional game that we can see here so as you can see our game is working pretty cool our score is working our player is working and everything looks cool so let's go and test it on the mobile device and see how this is working so we're going to go to this window General and we're going to go to this device simulator and from here I'm going to select Google pixel 5 you can use any other Android or iOS device if you want and then I'm going to go ahead and click on play and see how this will work in a real mobile device so I can click on play and now I can simply drag and as you can see when I click automatically the player moves and I can dodge it and whenever I Collide automatically the game gets restarted and it starts again so if I Collide the King has restarted and it is asking me to tap to start if I click it will start again simply just like this so this way our game is pretty much working as you can currently see if I go ahead and make it full screen as you can see this is how the game is working so another thing that you want to do is as you can see the tattoo start is going Beyond boundaries so what you can do is we can simply select this step to start from the from the image right here so you can select the tab to start and you can if you want you can simply make it a little bit smaller so I'm gonna double click here and I'm going to press the shift key to make it a little bit smaller just like this I think this one should look good okay if you want you can also make it a UI element just like our score text then it will look even better but I think this one should look good as well so let's go and convert it to a cortex UI text so canvas UI image so this will be your image and this will be tap text tap image tap image then what you're going to do is in the tap image I'm gonna go ahead and select the source Sprite and select our tap to text image right here or you can simply go to the Sprites folder and drag and drop the tab to start into this Source image of this step image okay so this is our tap image now I'm going to Simply click on set native size and automatically it will become bigger we are going to disable the tab to start that you have already created before disable this one and then I'm going to select this tap image from here double click to zoom in and make it bigger just like this so this way we have used the UI element instead of a random game object just to create the tap text okay just so that it automatically scales to different screen sizes now in our game manager instead of this step to start we need to drag and drop this step image right here okay so now let's go and test it one more time in our simulator so let's go to Windows General and device simulator let's select Google pixel 5. and now let's click on play and now as you can see whenever I go the UI is looking perfect because in this case we have used a UI text instead of a random image okay so this way as you can see our game is working our code is working and it is looking pretty good and it is a completely working Android game our game is almost done but here's one more step you can take to make it look even cooler so you can create a title text right here to make the game look really cool so I have created a title text and I have put it in my Sprites folder just now so this is the title text that I've created in Photoshop if you want you can create it or you can use any other title text that you want to so what I can do is in the canvas I'm going to go ahead and click on UI and create a new image okay and I'm going to name this one let's say title okay now I'm going to select the title and make sure I gonna reset its position now in the source image I'm going to drag and drop this block Dodge image that we have created and then I'm gonna click on set native size now it will become bigger now what I can do is I can simply go ahead and make it bigger to make it suitable for our game just like this not like this just like this so choose a size which is suitable for your game I think I'm gonna choose a size like this and this looks really cool so now I'm gonna what I'm gonna do is I'm gonna make this title A Child about tap image object so I'm gonna drag it inside this tab image so now what will happen is in our code we are automatically actually disabling this step image so whenever this step image will be disabled our tab title text will also be disabled along with it okay so now with that done our game is ready to test again now I can go ahead and go to window General device simulator let's select the Google pixel 5. and now here we have a block Dodge now let's see how this one is looking so let's go and click on play and this is the block Dodge whenever I start is you can see the title and the score title and the tap text both goes away and whenever I whenever the game over happens automatically they return so I can start I can play I can get score and I can die and the game gets restarted so as you can see this way the game is really really working fine and it's looking really cool right now so this way we have created an Android game so in order to build it what you can do is you need to go to this file build settings and from here make sure you have this scene added then you go to this player settings from here give a company name let's say I give the name Charger games you can give a name to your product this is the name of the game big dodge if you want you can also give an icon for now I'm gonna give an icon to of this pic that we already have here I'm gonna go to the Sprites folder let's go to the Sprites folder and drag and drop the pig image right here so this is our icon game Icon right now now I'm gonna come down to this other settings so now from the other settings if you want you can make some changes as you can see here we have the version so every time you create a new version you need to give a different version here different version number here you can select the target architecture and you can also select the version of the Android and as you can see override default package name this is really essential you need to give a package name otherwise you cannot export it and if you uncheck this as you can see automatically Unity will give you a default package name you can use this one and if you want you can also give your own package name just by clicking here all right so as you can see com.charger caves dot pigtch is our current default package name so you can either write it or give it like here next as you can see from the resolution representation if you want you can also change it to windowed or full screen if you want you can also disable this auto rotation so I want to play the game only in the portrait mode that's why I'm gonna disable the auto rotation so with these things done our now our game is ready to be built so now I'm going to go ahead and click on build and here I'm going to create a new folder called Android build inside this folder I'm gonna name this file to pick Dodge big dodge then I'm going to click on Save and now our APK will be created so let's click on Save all right so after waiting for almost five to six minutes finally our APK has been created as you can see here we have the peak Dodge APK so you can go ahead and put it on your mobile device you can transfer it to mobile device by USB by email by transferring it to Google Drive and install it and then you can play it on mobile device so this way we have created a completely fully working game start to finish in one video so I hope you really enjoyed and learned a lot of new things in this video so go ahead and start practicing these things and start working and show this game to your friends and family and enjoy so thank you so much for watching this video I hope you really enjoyed living this stream with me so if you want to learn more and build some more cool projects you can go ahead and check out all my game development courses where you can build Runner games shooting games racing games and all this kind of things for Android you can also check out my C sharp scripting tutorials to learn about C sharp and unity game development all my course links are given in the description of this video so you can go ahead and take the courses and build some more cool games really fast so thank you so much for watching this is Raja from Charger games and I'm gonna see you in another video very soon till then keep building and happy coding hi everyone this is Raja from Charger games and welcome back to another new video so in this video we're going to be learning how to create a cool little Android game within a very short amount of time so I hope you're really excited first of all let's take a look at what you're going to be building at the end of this video so here I have a simulator open so that I can show you how it looks in a real mobile device I'm gonna click on play and here I have the play button I can click on it and the game starts and the player starts running and here here we have our obstacles and we need to jump to avoid the obstacles and whenever we Collide our game gets restarted so we need to jump our player in order to avoid the obstacles and that's how we can score high and whenever we Collide the Game Stops and we can restart so that's how the game is it is a completely working game and I'm going to show you how to build this so let's get started all right so here I have opened up the unity Hub so what you're going to be doing is you need to make sure you go to this installs tab from here you can go to this install editor option and make sure to use the latest version or the latest LTS version this LTS is long term support and this one is the latest version so you can go ahead simply click on install and then from here make sure you select this Android build support Android SDK ndk and open jdk and then you can click on continue also you need to install this Microsoft Visual Studio if you don't have it installed already and make sure you install all these three things only then you will be able to build for Android so once you have done that simply click on continue and it will be installed so when you have installed unity and all the required components we're gonna go to this projects Tab and click on this new project now from here you need to choose either you're going to create a 2d or 3D project in this case we're going to create a 3D core so we're going to select this one and from the top make sure to select the latest version of the editor that you're going to be using in this case all right then from here to give a name to your project I'm going to name it Cube Runner and then I'm going to Simply click on create project and it's going to go ahead and create a new project for us so let's wait for it to create a new project all right so here we have a brand new Unity project opened up so this is the default layout that you see whenever Unity opens so what I'm going to do is I'm going to go to this top right corner and from the default I'm going to choose this 2x3 layout because that's the layout I prefer for building my games all right so now on the top as you can see we have window Mac and Linux written because this is the current platform on which we are working but we're going to build it for Android so we're going to go to file build settings and from here you have to select this Android and you will have this option only when you install the Android built options from the unity editor as I've shown you so now after selecting this Android simply click on the switch platform and our current platform will be switched to Android it will take some time so let's wait for it so now as you can see here we have the unity logo that means the Android is our current platform and if we close it on the top as you can see we have Android written here that means our build Target is currently set to Android okay so now that's done we're gonna go to our since folder and here we have a sample scene I'm going to go ahead and simply rename it to game okay so now the name of our scene is game okay so now with that done here on the game tab as you can see here we have free aspect written I'm going to click on it and from here we're going to choose this 16x9 16 by 9 or 169 portrait resolution because this is what gonna be suitable for our mobile devices all right now I'm gonna select the main camera as you can see in our hierarchy we have this main camera I'm going to select it and from the options we're gonna disable the sky box and select solid color and from the background you can go ahead and choose a suitable color for your game I think I'm going to choose something like this you can use any other color that you want to I'm going to select something like this for now later I can change it as well all right so now that we have the camera on the assets folder let's create few folders which you're going to be using first of all I'm going to create a new folder called scripts inside this we will have our scripts we're going to create another new folder we're gonna call it let's say materials so these are the materials that you're going to be using in this case so from here we're gonna go to this go to 3D object and create a cube all right so here as you can see we have a cube at the center of our game now this blue sign that you're seeing is the z-axis or the Z axis and the red sign that you can see is our red arrow that you can see is our x axis so you can simply press the ALT key on your keyboard and then press the left Mouse button to rotate your view like this okay so make sure to select a view where the arrow key the blue arrow key is at the front because our z-axis should be at the front always okay so now I'm going to select the cube and set the Z scale to let's say 20 20 is very small or let's say make it 200 at once so that we don't have to change it again okay now I'm gonna go ahead and rename this Cube to ground because this is going to be our ground all right so now I'm gonna go to this materials folder from assets I'm going to go to this materials folder and here I'm going to right click create a new material I'm going to name this one matte one let's say I'm at one and then I'm going to select it and press Ctrl D to duplicate it few times so that we can create three four materials that you can use later for the first material we're gonna go ahead and select this Albedo option and from here I'm going to select this green light color because that's what we're going to give to our project that's what we're going to give to our ground so I'm going to drag and drop this material one over our ground here or here anywhere we want okay and now again I can change the color to any other option that I want I think I'm going to keep it something like this all right so now here we have our ground I'm going to select the ground and from the tag as you can see currently we have it untagged so I'm going to select here go to add tag click plus and give it a tag ground just like this and then select ground go here and select that ground tag so now our ground has a ground tag attached and we will need it later now I'm gonna go ahead and create a 3D object a cube again and I'm going to name this Cube player then I'm going to select the material 2. first of all let's select our player and move it a little bit upwards I think let's make the Y position 1 and that will make it perfectly sit on top of our ground now let's select our material too and go to the El video and select a red light color something like this and now I'm going to drag and drop this one on our player all right so this is our player I can make it a little bit like this if I want to okay so now this way we have our ground and our player both ready now we need to set our camera so that it looks cool also when you can select our player go to the tag and as you can see already we have this player tag created so you can select the player tag so that our player has a player tag attached all right so now that's done we're gonna go here and select our camera and change its position a little bit so you can scroll or rotate your mouse wheel to zoom in or zoom out the scene view so I'm gonna scroll down to zoom out and here the red button is the x-axis and we're gonna move our camera at the x-axis like this and change the Y rotation of the camera to something like let's say 45. actually negative 45. negative 45 so this is the kind of view that we actually want I can also make the X position a little bit too far something like this and by selecting this one we're gonna put it a little bit upwards somewhere like this so this way it it can be a it's gonna be at the view where we actually want it to be okay so something like this should be all right this one looks like our game is at the correct position also we can select the field of view and we can make the field of view bigger or smaller from here so I think I'm gonna make it something like this and then I'm gonna make it something like this all right so this view looks cool and this is uh looking already like a game you can keep it like this keep it like this whatever you prefer for your game because it's your own game so I think I'm Gonna Keep it somewhere like this this one looks good so with that done we have our player with our ground let's go ahead and create a simple obstacle so I'm gonna go ahead and create it through the object and guess it right you can create a cube we're going to name it obstacle and let's move it a little bit on this side and we're gonna make its uh X scale to let's say two not two ten or let's say five I think five should be good and the Y scale to 2 is too good I think 2 is good and we can position it about at 0.5 all right so this this one looks good so this this is how our obstacles will be and this will come slowly towards our player and our player will have to dodge them now let's select the material 3 and select a color suitable for our player not a player for our enemy and I think I'm gonna select something like this and drag and drop it right here I can even change it like this okay so this is how this looks and this is how things are so this looks pretty cool this is our player this is our ground and these are obstacle also selector obstacle go to untagged click plus create a new tag name it obstacle the OS Capital remember that select our obstacle and select the obstacle tag right here all right so this way we have our player ground and obstacle ready so let's move our obstacle a little bit backwards and first of all let's create a script and program our player so that I can jump our player by tapping on the screen so inside the scripts folder right click and create a new c-sharp script and we're going to name this one player controller also you can simply name it player or anything you want to now select our player and drag and drop this player controller onto our player so now this player controller script is attached to our player now what we need to do is we also need to add a rigid body component to our player so you can go to add component physics and then you're going to add a rigid body to it so this rigid body is what makes this one interact with physics if you want to get the built-in functionalities of physics from Unity we need to add a rigid body component to it so now as you can see it already has a gravity it has a drag and all this stuff we don't need this we're going to make it zero we simply need this gravity and we'll keep it now from the constraints we're gonna change the freeze rotation we don't need to rotate our player at any axis we only need to make it jump on the y axis that's why we're going to keep the Y still and we're gonna freeze position at X and freeze position at C okay so now that's done we're gonna double click to open player controller in visual studio so let's double click to open it all right so now our player controller script has opened up in visual studio so first of all here we'll need a few variables which will decide how our game works so first of all here we're going to create a new variable called rigid body and we're going to name it RB then we need another variable called public float jump Force so this this will decide how much the player can jump okay and another variable here we're going to create is Bool can jump so this will decide whether a player can jump or not all right so with that done now here we're gonna create a work function so we're going to write void awake which gets called even before start when we start the game and inside the awake we're going to say RV equals git component rigid body this will get the rigid body that we have attached to our game object and store its reference inside this RB variable so now whatever we want to do with this rigid body we want to add gravity when we add Force we want to add velocity we can do it simply by using this RB variable all right so now that we have this what you're going to be doing is inside this update function we're going to check if we are tapping or clicking on the screen so to do that we're gonna say if input dot get mouse button down and within brackets we can write 0 that means whether we are clicking the left Mouse button this zero means left Mouse button and this one works for touch as well so you don't need to worry whether you were working on mobile or PC this one works for both and this one works greatly on mobile as well I have tested it multiple times so if input.get Mouse button down 0 that means whenever we are tapping on our screen or clicking our Mouse then we need to make our player jump okay so in order to make our player jump what you're going to be doing is we're going to write RB dot add Force so we can add a simple force and at which direction we're going to add Force we're gonna add the force towards the upwards Direction so that's why we're going to write vector3 dot up multiplied by jump Force okay and then after a comma we're going to write Force Mode dot impulse so this will make the force jump force a little more effective and quickly the player will be jumping higher okay if we don't make it impulse then we will have to give a lot higher jump Force like 300 400 but with it impulse we can simply make it 10 20 and the player will jump really high okay so now whenever we click the mouse button our player should jump up with this jump Force okay so now let's go to the editor and check how it's working all right so here we are inside the unity editor here we have our player here we have our rigid body and here we have our player controller so let's make the jump Force to about let's say 20 and see how it's working so let's click on play and now if I click on play you will see our player jumps a lot higher so let's go ahead and make the value about 10 and see how it's working and this is how the tail looks like but as you can see the it it seems like the player is floating and the player is not coming to ground quickly so in order to fix this what you can do is as you can see here we have a gravity defined by unity we can go ahead and increase the amount of gravity so that the player falls down faster to do that we need to go to edit project settings and then you need to go to physics and inside physics as you can see here we have the gravity and it is set to 9 minus 9181 which is default for Earth or or for real world for in this but in this game we're gonna go ahead and make it uh higher let's say we're gonna make it about I think minus uh let's make it minus 50. so now we have a gravity of about minus 50 that is a lot higher so now as you can see when I click on play if I press here as you can see it jumps really fast so now we're gonna go ahead and make the jump force a little higher let's say make it 15 and now if I click on play now as you can see the player can jump and it looks really cool so let's go ahead and make our obstacle somewhere like this and let's see if our player can jump on it yes the player can easily jump over it so I think 15 should be good or later we can change it to any value that you want to all right so now our player is okay but we have one more problem if we click again and again again you can see our player can jump infinite amount of time but we don't want it we want our player to jump only when it is grounded or when it is on our ground okay so we need to detect we need to find a way so that we can make the player jump only once when it is grounded and not again jump higher okay so for that we're gonna go ahead and open up our player script one more time open up our player controller script and here we're gonna check here we're going to call a function called void on collision enter so this function gets called whenever our rigid body or our player collides with any other object okay so in this case whenever it collides the ground we're gonna check if collision.gameobject dot tag equals ground all right so if it collides with the ground that means we are currently grounded and that is the time we're gonna make the can jump to true so whenever the player lands on the ground we're gonna say can can jump equals true all right and then we're going to create another function which will be void on collision exit and here we're going to say if Collision dot tag collision.gameobject dot tag equals ground so this function gets called whenever our player exits from the Collision that is whenever the player will jump it will exit colliding from the ground and that is the time we're going to make the can jump to false so now what will happen is whenever the player is on the ground the can jump will be true that means the player can jump now and whenever the player will be in the air the can jump will be false that means the player will not be able to jump anymore okay now in order to make this condition work what you're going to be doing is here we're gonna say if we press the mouse button or if we tap on the screen and we can write to Ampersand that means we are joining two conditions and can jump that means when we are pressing on the pressing the mouse button or tapping on the screen and we can jump that is when we want to add the force so this way we can prevent the player jumping multiple times when it is not grounded so let's go ahead and test how it's working so here we are in editor let's click on play and now as you can see when I click on play I can jump only once and we cannot jump multiple times that's because only when it is grounded out level jump and otherwise it's not gonna jump so that's what we have done and in order to make it perform correctly make sure you have this ground tag attached and also make sure your player has this rigid body and this collider and all these things otherwise these things will not work correctly so now that we have our Player working correctly we need to make our obstacle move towards the player just like this okay so now we're going to select our obstacle and from here as you can see we have a collider attached we're going to go ahead and check this is trigger option so when we don't have this is trigger our collider can collide with the player but when we have this is trigger option the collider will detect collisions but it can still go through the player okay because we have two colliders here the ground and the obstacle so we're going to make the ground Collide and the obstacle trigger okay okay so now here we have the obstacle we're going to go to the scripts folder create a new C sharp script and I'm going to name this one obstacle and then I'm going to select my obstacle and drag and drop this obstacle right here and then double click to open the script in Visual Studio okay so now here only thing we do is we need to Simply add a speed so here we're going to create a public float speed variable and inside update we're going to say transform dot translate that means we are simply moving the object or translating the object at which direction we can translate it to Vector 3 dot forward Direction the forward Direction means the Z Direction okay and then we need to multiply it with speed and then again we need to multiply it with time dot Delta time to make sure the movement is very very smooth and time independent all right so now with this code obstacle will slowly move towards the player but that will depend on the speed value okay we need to make the speed negative so that the obstacle actually moves towards the player otherwise it will go towards the Z Direction okay then we're going to create another function void on become invisible so this function gets called automatically whenever our obstacle is invisible to the camera so whenever it goes out of the screen we need to Simply destroy game object so whenever our obstacle goes out of the screen it becomes it actually gets destroyed so that's what we want okay so with that done now let's go to Unity and check how it's working so here we have our obstacle here we have a speed let's move the obstacle like this and let's change the speed value to something like minus 8 and see how it's working at the end to make it a little bit faster let's make it minus 10. and let's prove it somewhere like this is it okay I think this one looks pretty good okay so I think this one looks pretty good so I think I'm going to keep it -10 and if you make it simply positive 10 then it will go and move towards this axis which we don't really want okay so with that done now our obstacle is ready now we're gonna reuse this obstacle again and again for that we need to make it a prefab okay so prefab is an object which you can reuse again and again with the same properties we don't need to create it again okay so inside assets we're gonna create a new folder we're going to name it prefab and inside the prefab folder I'm going to drag and drop our obstacle now as you can see it has become blue that means it has become a prefab okay so now we have our player and we have our obstacle now what you're gonna do is whenever our obstacle touches the player we want to reload the scene because then the game will be over okay so in order to do that let's open up our player script one more time let's open up our player controller and here we're gonna create another new function we're going to call it void on trigger enter okay and here as you can see we have the on to the enter and here the collider with which we are colliding is inside this other variable so inside the other variable we have all the properties of the collider with which we are colliding that is our enemy so here we're gonna detect if we are colliding With the Enemy or not so we're going to say if other Dot Game object dot tag equals our obstacle all right so if we are colliding with the obstacle then we're gonna simply go ahead and restart the scene and in order to restart the scene first of all we need to import the scene management namespace of unity so on the top you need to write using Unity engine dot scene management all right and then here we need to write scene manager dot load scene and inside this we're going to write the name of the scene and the name of the scene is game exactly you need to write it with exact capitalization as you can see here with G's Capital then the things are small so whatever you have named your scene you can simply write it within quotations here and that's how the scene will be reloaded whenever this on trigger enter calls and whenever it collides with the obstacle okay so now let's go back to Unity so here we have our player here we have our obstacle and let's check if the scene gets reloaded or not so let's click on play and whenever the player collides as you can see the scene gets reloaded as you can see the scene automatically gets reloaded whenever the player collides with it now a lot of times this scene reloading doesn't work so what you can do is you need to go to this file build settings and make sure you add open scenes whenever you click on this add open scenes your scene will automatically be added to the build and that is when this will work correctly in your case or in every case so now that we have this obstacle and the player ready now we need to find a way to spawn the obstacles again and again so that it goes again and again and it comes towards the player and whenever it collides our player our game gets restarted so for that we're going to go ahead and create a new game manager and that will take care of everything so here we're going to create a new empty game object we can run it game manager and inside the scripts we're gonna go ahead and create a new c-sharp script we're gonna name it game manager as well now I'm going to select the game manager and drag and drop the game manager script on top of it so here we have our game manager now now let's go ahead and open up our game manager here we have our game manager script here we will need a lot of references so first of all here we're going to create a public game object obstacle so this will be a reference to our obstacle because we need to spawn it again and again so we need a reference to it then we need a public transform transform spawn point so this is the point from where our obstacles will start spawning this is a position from where our obstacles will start spawning okay then here we need a int score variable in the beginning it will be zero so this is the this will be the value of our score okay now here what you're going to be doing is here we're going to create a void game start function and we also need to make it public so you need to make it public void game start this will take care of everything whenever the game starts like it will start spawning it will start keeping the score and all that now let's see how we can actually start spawning for that we're going to use something called core routines because whenever we are spawning on obstacles we need to spawn it after some time after random times let's say first obstacle respawn it after one one second the second one will be after two seconds the third one will be after fourth uh let's say 0.8 seconds or something like that so that's why we're going to create a cool routine and for that I'm going to write I inumerator and I'm going to name it spawn obstacles okay so inside this iron monitor actually we need a pair of parentheses here so inside this I enumerator spawn obstacles what you're going to be doing is we're going to write a while true Loop so this is going to be an infinite Loop so this will run until we don't stop it uh externally so inside this we're gonna simply go ahead and spawn our obstacles at a random amount of delay okay so for that we're gonna first of all create a random wait time for our obstacles to respawned so we're going to write float weight time equals random dot range so we need to create a range between which we're going to create a random number and after that random amount of time we're going to spawn our obstacles so the two random amount of times in our case will be 0.2 and 2. okay 0.2 let's write an F here and if you're as well so whenever this code runs this will generate a random value between 0.2 and 2 seconds and our game will wait for that amount of time and after that it will spawn the next obstacle okay so here we're going to write ild return new wait for seconds wait for seconds and you're going to write wait time so our game will stop on another game this thing or this function this Loop will wait for this random amount of time and after that it will spawn the obstacles in order to spawn it we write instantiate and what you're going to spawn we're going to spawn our obstacle game object which we have a reference to here so here we're going to write instantiate obstacle we need to write the position where we want to spawn it and here we already have a transform component for we want to spawn it so we're going to write spawn point dot position and then it will write the rotation which we want to give to our game object but we don't want any external rotation we just need to Simply keep it at the same default rotation for that we're gonna write quaternion dot identity that means no rotation at all all right so now whenever this Loop runs whenever this coroutine runs automatically a random number will be generated and it will wait for a random amount of time and after that it will spawn the next obstacle and that's how random obstacles will be spawned so now inside the game start we're gonna start this core routine for that we're gonna write start codeine and inside this within double quotations we need to write the name exactly same so let's go ahead and copy it so that we don't make any mistakes let's copy it and paste it right here so now whenever the give start function gets called automatically this spawn obstacles will be called as well okay now inside our start for now we're gonna simply call our game start function because we don't have another way to do it for now so we're gonna simply call it inside our start function okay so now whenever the game starts our game start function will be called it will call the core routine and the core team will be started and our obstacles should spawn so let's go ahead and check how it's working in unity all right so here we have our game manager and as you can see it is asking for an obstacle so we're going to go to our prefab folder and from inside we have folder we're gonna drag and drop our obstacle and make sure to drag it inside the prefab folder from inside the prefab folder and not from the scene because this one will be deleted after some time and this is not going to work so we're gonna disable this obstacle from now because we don't need it right now we're going to go ahead and create a new empty game object and we're going to name this one spawn point and I think you want to position it a little bit away from the player because this is the this is the position from where we actually want to start our obstacles to spawn Let's uh let's make the Y position to 0.5 because that is the default y position of our obstacles as well so we're going to make the spawn points by position to 0.5 and the Z position should be about let's say 20 something like this should be okay we can change it later as well now let's select our game manager and drag and drop our spawn point directly here so now our game manager has access to the obstacle also it has access to the spawn point so now let's click on play and see if it is working or not so now as you can see automatically our obstacles start spawning up to random amount of times and that's how they're coming towards our player and the player has to jump between them and that's how things are so now as you can see the game is working the player can move on the obstacles come automatically but there are some problems so what you can do is we can simply go ahead and adjust the jump force of the player and make it a little bit higher let's make it 18 and also as you can see our obstacles are coming too fast so we're gonna go to our obstacle script and we're gonna make the random value not the obstacle script the game manager script we're going to make the random value okay so here we have written 0.2 we did write 0.5 which should be the minimum number okay so 0.5 and 2 should be good in our case and that's that thing should work right we can also adjust the player speed according to our own wish to make sure it's working correctly so with that done now let's go back to Unity so now we need to create a score mechanism and also we need to create a button so that whenever you click it our game starts so for that we need to create a canvas so we're going to click here go to UI and create a new canvas and as you can see automatically a new canvas will be created inside which we will have all our UI elements so from the canvas we're going to change the UI scale mode to scale with screen size that means our UI elements will be automatically changing towards the screen wherever we put the game so let's say we put it in a smaller screen the UI elements will be smaller will be put in a larger game the volume elements will be automatically scale according to our screen size okay and for the reference resolution let's give it 1920 by 1880 which should be all right and let's set the match to 0.5 okay so with that done now we have our canvas ready let's right click go to UI and create a new UI text mesh Pro text and for that we're going to import the TMP Essentials let's click on that and now we have the essentials imported and here we have a text as well now as you can see here it's hard to edit the text in this view so we're going to go to this 2D mode so that we can easily edit this UI and we can double click on this text so that it can focus on the text wherever that is so as you can see currently we have the text right here we're going to go ahead and rename it to score text so let's make it score text okay and for the text we're gonna text content we're gonna make it to zero because it will be a number which will keep increasing according to our game now make sure you select this direct selection tool or Rec tool and then drag the corners to make it larger because otherwise we cannot make it bigger because we need to make it bigger and then we're gonna position it somewhere like this and this is the font size to higher just like this as you can see currently the font size is 40 we can go ahead and make it about let's say something like 200 and I think make it a little more bigger 300 300 should be okay and now we need to keep it at the center of our screen and then we need to put this one at the center as well so for that from these options from this alignment options we're going to choose Center vertically and Center horizontally as well and then we're gonna make it a little bit smaller something like this and then you can simply put it somewhere like this now I'm going to select the color and change the alpha value somewhere like this so this one looks a little more professional than how it is so now from here the score will go to one ten hundred thousand ten thousand and all that and we will not have any problems okay so here our score text will be shown so along with the score text we will also need to create a new button which by clicking which we can actually start the game so you're going to select the canvas create an UI we're going to click on this button text mesh Pro so here we have a new button and we're going to name this one to let's say play button okay so now here we have the play button we can double click to actually zoom in it here we have the play button now here we can simply go ahead and use this button as it is and we can simply go ahead and change the play text to play let's say we write play here and whenever the game starts we can start playing but we can also decorate it a little bit to make it a little more cooler than how it is so for that we're going to go ahead and delete the text because we don't need it select the play button and as you can see here we have a UI Sprite image you can go ahead and download any other button image that you want or you can create your own using Photoshop or any other photo editor and then use this button image as the UI Sprite right here so I have downloaded a button image from flat icon.com and I'm going to use that so inside the assets I'm gonna import that one so here I have imported the button image and as you can see it has been imported as a default so I'm gonna choose the texture type and change it to Sprite 2D and UI make sure you have this practitioner UI selected and then click on apply only then you will be able to use this as your UI image now we're going to select our button display button go to this UI Sprite and drag and drop this play button right here so now as you can see we have the play button here now as you can see it is a little bit distorted so you can simply drag it to change it or you can simply click on this set network size to set it to the correct size then you can go ahead and make it smaller or bigger according to your own wish I think I'm going to make it a little smaller something like this and position it somewhere like this now whenever this button is pressed I want the game to start and we have everything inside the game start function so which will decide what to do when the game starts so now we need to select our play button and here we need to add an on click event to detect clicks on that button so I'm going to click on Plus and now here it is asking for an object where we have the functions which we want to call so we have the functions inside this game manager script so you're gonna drag and drop this game manager right here and now from the functions we're going to go to game manager and use this game start function okay so now whenever we press this button automatically this game start function will be called and make sure you have made this game start a public function because otherwise it's not gonna work so make sure to make it public only then this is going to work okay so now that's done we have this score set score text set and we have display button set so now all we need to do is whenever the game starts we need to keep updating the score and also we need to start the game whenever the play button is pressed so let's go ahead and do that inside our game manager all right so here we are inside the game manager here we need few references here first of all we need a reference to the text score object for that we also need to import our UI elements as you can see here we are using the UI from the text message Pro module you can also use the unity's default text in that case you need to Simply write using Unity engine.ui but in this case I need to write using TM Pro because we are using the text mesh Pro text and buttons now here we need to write a reference to the text message Pro button so you can write public text mesh Pro text mesh Pro ugly and then it writes score text so inside this we will have a reference to our score text that you have created and then we can actually use it to do whatever we want to then we will need another reference to our play button that we have created so for that here we're going to write public game object and you can write play button okay so now we don't need to start the game from the game start so we're gonna go ahead and delete this because we are already calling the game start function whenever we are pressing our play button okay so inside the gamestart function we need to start spawning the obstacles also we need to actually activate the player whenever the game starts because if the player is on the screen from the beginning it will not look good so we will deactivate the player in the beginning and then we will activate it whenever the game starts so here we will also need a reference to the player so we're gonna write public game object player and here we're gonna write player dot sit active to true so whenever the game starts we want to set the player to activate it and we also want to make the play button dot set active to false okay so whenever the game starts we want to activate the player and deactivate the play button because when the game starts we don't want the play button to be on the screen that will look really weird also we need to update the score but we haven't created anything that can update the score yet so what we can do is here we can create a new function we're going to call it void score up this function will simply go and do score plus plus so whenever this function is called the score will be incremented by one and then we also need to update the score text on the screen to update this score value for that here we're going to write score text dot text equals score now this is an integer and this is a string so this is not going to work so we need to convert this integer to a string so we can write dot to string so now whenever our score value is let's say it is 0 1 2 3 4 whatever the value is that value will be automatically shown on the score text on our screen so now we need to Simply call this score up function after a certain amount of times repeatedly so that our score gets incremented again and again for that here we're gonna create invoke repeating function so this invoke repeating can call any function repeatedly inside that within double quotations we're going to write score up so now the score of function will be called again and again after how many times we write that here so we need to start it after two seconds and then after every one second the score will be incremented okay so here we are saying is we need to call this score up function after two seconds and then every one second call it again and again and again and again so that way our score will be keep incrementing and our uh score text will also keep incrementing now here I have made a mistake as you can see the p is capital but here the p is small this is very important once that happens this is not going to work so make sure to copy this one from here and paste it exactly same as it is written here otherwise this is not going to work so now we have all things set we have the score up we have the we are activating the player deactivating the play button whenever the game starts we are calling the score and everything should work so now that we have done this let's go and save and see if it is working on Unity or not so now that we have done it inside our game manager we will need to add a lot of new references so for the score text let's drag and drop our score text right here for play button this drag and drop our play button right here for player let's drag and drop off player right here now in the beginning we need to select our player and deactivate it and whenever the game starts we need to activate it and let's see if that is working so let's click on play so here we have our button if we click it as you can see our player is here and our game starts our obstacles come and our score also keeps going up again and again so our game is pretty much working and if I Collide automatically the games gets restarted if I click where here again the game gets started again so everything looks exactly as we want it to so things are working pretty fine if you want you can also make it a little bit faster or slower and make the players jump a little bit higher to actually customize the game according to your own wish you can also go ahead and add a title right here so that it is shown whenever the game starts but I think I'm going to keep it right there so now that our game is like this and let's go and check how it's working on a little real device so I'm going to go to this General device simulator format I'm going to select this Google pixel 5. so now I'm going to click on play and if I click on here as you can see this is how it's gonna look in a real device so this is how the scores will be these are the player will be and whenever it collides automatically the game gets restarted all right so our game is ready so now all we need to do is we need to actually build it for Android and install it in our device so for that I'm going to go to file build settings and make sure our current platform is set to Android and once that's done I'm going to go to this player settings tab and here as you can see here we have a company name I'm going to give my company name but you can go ahead and give your company name or keep it default let's name it Cube Runner here and let's keep the name same you can give an icon to your game now from the resolution as you can see here we have the default orientation set to rotation but since it's a portrait we're going to set it to Portrait so that our game always states in the portrait and not goes to landscape now from the other settings as you can see here we have the package name if you want you can simply go ahead and change the package name this will be unique for your game so you can go ahead and change it or keep it same and when you create an updated version of your game you need to actually increment the version and the bundle version code okay so with that done now we can go ahead and click on build and then here I'm going to create a new folder called builds and inside the builds folder I'm Gonna Save it with the name Cube Runner game and then click on Save and it will be actually exported as an APK file which you can install in your device all right so after waiting for some time finally our APK file has been generated and here it is you can go ahead and transport it to your mobile device either via USB or you can simply email it to your device or transfer it somehow and then you can install it your mobile device and then you can play it also you can share this APK to your friends and family and let them enjoy your game all right so thanks a lot for watching this video I hope you really enjoyed the whole process of making a complete game in this very short time and if you want to make some more cool games like 3D endless Runners 2D endless Runners 2D catching game 2D Runner game racing game and all that I have a lot of different courses where you can learn to build all these games from these courses for your mobile devices for your PC and all that you can get all my courses from the links in the description of this video and from the pinned comments so go ahead and enroll in the courses and learn how to build some more cool games so thanks for watching this is Raja from Charger games and I'm going to see you in another video very soon if you like this video make sure to hit the like button and subscribe to this channel for more cool tutorials and I'm gonna see you in another video very soon foreign hey there welcome back to Charger games my name is Raja and in this video You're Gonna Learn how to make a simple Android game using unity game engine so before starting let's take a look at what you're going to be building at the end of this video so as you can see here we have this game and this is the first screen that you see so as you can see it says press any key to start so I can press on the left and the game starts and this way I can move the pedal to the left or the right when you touch on your Android device on the left and right the panel will move and the ball will bounce and as you can see as soon as the ball goes down the game restarts so this is the core function of the game and we're gonna build it from scratch so let's get started now first of all you have to give a name to your project so in this case let's give a name Unity Android project all right and then here you need to select the location where you want to save your project in my case I always save my projects here so you can save it here or you can create you can save it on any other location that you prefer then you need to select this 2D because in this case we are working on a 2d project so we're going to select 2D after that simply click on Create and it will create a new project for you so in my case I have already created a new empty project so here it is Unity uh Unity Android project here we have it so when you open Unity for the very first time and your very first project loads this is how it will look but probably in your case it will not look like this probably in your case it will look like this all right but I usually like to work on a different layout so to change the layout you can go to this top right corner click on here and select this two by three and I like to work on this layout because uh here I can see the scene view as well as the game view at the same time but you can use any other layout that you prefer so now the first thing we have to do is we need to save the scene so file save and as you can see our scene is already saved inside the scenes folder where the name is saved as sample scene so we're gonna go ahead and rename it and you're going to name it game all right and then reload and as you can see here we have renamed our scene to game now we're going to go to file build settings and as you can see currently our platform is set to PC Mac Linux standalone but we want to build for Android devices so for that we'll select this Android and if you don't have the Android versions installed or the Android components installed while installing Unity then you will not be able to do this all right so now you need to select this Android and simply click on switch platform and once you click on that you will see that Unity will slowly change our platform from Windows to Android so here as you can see the unity logo has been shifted towards Android so now our current platform is set to Android and if you see on top as you can see here Android is written that means this is our current platform that we are working on then we need to select an aspect ratio for our game so as you can see here free aspect is written from here we need to select this 16 is to 9 landscape or you can also select this uh 1920 by 1080 landscape I think I'm going to select this so let's keep it like this now we need to start working on our game so inside the assets folder we're going to keep all our assets or all our project elements like Sprites audios scripts and all these things here so we're going to create different folders here to organize all these things so here I'm going to create a new folder and I'm going to name it Sprite where are we going to keep all our Sprites or images we can also create a new folder and we're going to name it scripts where we're going to create all our Scripts all right so now as you can see here we have this game uh scene and here we have all the elements that are inside this game scene currently we only have this main camera so we can select the main camera and from here as you can see we have this background and the background clear packs are set to solid color so let's go ahead and change the color so select here and from here we can select any other background color that you want let's select a color like this it's your game so you can go ahead and select any other color that you want so you are free to use or choose any other color but I think you want to choose a color like this all right so or maybe like this so let's keep it like this we can change it later as well so whenever you make any changes make sure to press Ctrl s or go to file save to save your scene all right so now we're gonna create some boundaries for our game so we're gonna go inside the Sprites folder right click create a new uh Sprite so we're going to create a create Sprites and square all right so now we're going to drag this square right here on the hierarchy as you can see here we have our Square now I can move it on the top like this so to move it on the top make sure you have this red tool selected and then we can simply press the alt button and drag it to make it bigger like this all right and if you don't want to click on the alt button you need to do it like this manually but if you press the alt button then you can do both sides at the same time so press the ALT key and click and this is how we have created the long boundary all right now let's go ahead and change its color so I'm going to change it to a color of something like this I think this one looks fine or if you want you can also go ahead and change it and give it any other color that you want then I'm gonna rename it to boundary now I can right click and click on duplicate all right now I can select this tool this move tool and move it down something like this then I can go to this transform component and from the Z rotation I'm going to make it 90. all right then I'm gonna push it to the right side like this now before doing that I think I have missed one step so let's go ahead and select this boundary go to add component go to physics 2D and select box collider 2D all right so when we give a box collateral 2D as you can see here we have a green outline outside of our boundary so now our boundary can collide with other game objects all right so if you don't give it a boundary or if you don't give it a collider then our balls will pass through it and they will not Collide or Bounce from the walls but we want the balls to bounce from the walls that's why we want to give it a box Collider the same way we're going to select this boundary as well and we can go to add component physics 2D box collatitude so now we need to create a boundary on the left side and this is very simple I'm going to right click click on duplicate and now as you can see we have another boundary here but this one is at the same position so all we need to do is we need to change the X position from 8.88 to negative 8.88 and as you can see as soon as it click as soon as I give a negative sign before that it just moves to left side on the opposite side so now these things are looking pretty good and all of them have a box collider attached to them so now that we have created the boundaries now we need to bring the pedal and the ball in our scene so now we can add the pedals and the balls to our game so here inside the Sprites folder I have few Sprites of the paddles that I have downloaded from the internet and you can download them from kenny.nl website or from any other website that you want and from it I can simply drag this pedal right here and as you can see here we have our pedal now as you can see it is very small so to make it bigger we can either change the scale directly from here or the better way is to select our Sprite and change the pixels per unit from 50 to 100. and then click on from 100 to 50 I'm sorry and then click on apply as you can see the size has been doubled now I can simply select it and move it down like this all right the same way we can select our ball and drag it like this and then we can select the ball Sprite change the pixels per unit from 100 to 50 and then click on apply so now we have our panel as well as our ball right here on the screen now we need to find ways so that we can move our ball and move our pedal bounce our ball and do all these things that we want and whenever you make any changes make sure to save the scene so I'm going to press Ctrl s to save the scene now I'm gonna select our ball blue and I'm going to rename it to wall now we're going to add a physics component to our ball now we want our ball in the game to bounce to bounce to the walls to bounce from the pedal and then we want to we want it to collide with the pedal and the boundaries so that's why we need to add some Physics mechanisms to the ball all right to do that in unity we need to Simply add a component called rigid body 2D so here we're going to click on ADD component physics study and rigid volatility and what is that so here we have the rigid body 2D so as you can see when we added this rigid body to the component it automatically has these properties like Mass drag gravity scale and all these things so Unity is physics engine gives this thing to us already or by default when we add this rigid body to the component to any game object so in this case we have added to the ball and now if I click on play you will see that the ball will fall down and that's because it now has a gravity scale of one so the ball is now affected by physics and that's why it is falling down now we need to give a collider to the ball so that it can collide with other game objects so I'm going to go to add component physics 2D and give it a circle collectivity because our ball has a circular shape so if you go ahead and zoom in uh first of all I'm gonna disable the Gizmo of the camera because it's making problem so I'm going to disable it so as you can see here we have a green outline that means our ball has a collider attached to it all right so now we have a collider attached to the ball but still the ball will fall down because our paddle doesn't have a collider all right so now we're going to select our panel blue and let's rename it to Pedal and now I'm going to go to add component go to physics 2D and add a capsule collided to the object as you can see here we have the capsule added to the pedal and now we can click on this edit collider button and edit the collider directly from here and as you can see the direction of the capsule is set to Vertical so that means if we change the x or y size it will still remain vertical like this it'll still remain vertical but we want it to be horizontal so that's why we're going to change the direction from vertical to horizontal and now if I change the x or the Y values and as you can see it will shrink down like this and it will simply oh take the shape of any object that you want in this case we need to take the shape of this pedal you can also select this edit collider button and edit it directly from here so anything that you think is more suitable for you you can use that option and create a collider around it so now we have a nice capsule called lighter all around our pedal alright so now our pedal is ready to take all the collisions so now if I go ahead and click on play you will see this turn the volume fall down but it will not go through the pedal that's because our paddle has a collider attached to it now all right so now let's go ahead and move on to the next step now let's make the ball bounce to make the ball bounce we need to add a physics material to the ball so to do that we're going to go to this assets folder right click create and from here we're going to create a physics material 2D and we're going to name it bounce the bones bounce and now we're going to change its friction from this point to zero that means we don't put any friction I'm going to change the bounciness to 1. all right so now our bounciness is set to 1 that means the ball has the maximum bounce property okay so now if I go ahead and click on play you will see the ball will fall down and it will bounce but it is not bouncing and that's because we have created the property but we have not added it to the ball so as you can see here we have the circle collider 2D and here we have the material we can simply drag and drop our bounce property in the material so let's drag it and drop it on the material so our ball now has this bounce property attached to it now we can click on play and you will see the ball keeps bouncing and this is the first movable thing you have created and probably you will love this all right so the ball is the ball is bouncing so we have done the first step now what we need to do is we need to move our pedal and also we need to add some Force to the ball to do all these things we need to create our very first scripts all right the scripts give Behavior to our game objects in this case we want to give some Behavior to the pedal and also to the ball so first of all let's try to move our pedal left and right so let's go ahead and create a new script so I'm going to go to inside the scripts folder right click create a new C sharp script and make sure to rename it directly here because if you try to rename it later you will not be able to do it so let's name it pedal controller or simply you can name it pedal because that's simpler so let's make it paddle now we can select our panel and drag and drop the script right here so now the paddle script is attached to our panel that means anything that we write inside the pedal will control or will affect the pedal now I can simply double click on it to open it in visual studio now in your case if you have selected any other code editor it may open there so here as you can see our script has been opened in Visual Studio I'm gonna zoom in to make it a little bit bigger now here we need to write our very first code so first of all we're going to create a new variable called rigid body 2D so here we're going to create a new variable and we're going to write rigid body 2D rigid body 2D and we're going to name it RB so this variable will store a reference to the rigid body that we have attached to our panel and then we can use that reference to move our pedal so now here we're going to create a new function called void awake so this function gets called even before the start function automatically by unity so whenever we need to get a reference to any objects we need to use this awake function now let's go back to Unity and check few things so if I go back to Unity and selector paddle you can see we have not attached a rigid body to our pedal so first step we're going to go to add component physics 2D and add a rigid body to the component to our panel all right now as you can see this one has this gravity as well but we don't want our panel to fall down so if I click on play you will see our paddle will fall down along with the ball but we don't want that so that's why we're going to make the gravity scale zero and then from the constraints we're going to change the we're going to use the freeze position Z and we're going to check it freeze rotation Z and we're going to check it that means we don't want our pedal to rotate around Z axis like this so we want it to stay still and not move so like this so now what we can do is from our script we need to get a reference to this rigid body 2D that is attached to this component and then by using the rigid body 2D and by using the physics functions we need to move our pedal using some Physics forces so let's save it and go back to our Scripts and in the awake function we're going to say RB equals git component and within this angle brackets we're going to write rigid body 2D and then we're going to give a pair of parentheses so this is the syntax when we want to get access to any component that is attached to our game object in this case we are getting access to the rigid body 2D component that is attached to our pedal and we are storing it inside this RB variable all right then we can do anything with this rigid body attached to or anything with the rigid body that is attached to our panel using this RB reference all right now here we're going to create another variable we're going to name it public float move speed so this is the speed by which we want to move our panel to the left and right direction so here we're going to create a new function we're going to call it void touch move so inside this function we're going to give all the functionalities that we have for our pedal to move or for the touch input all right so first of all to detect if we have touched on the screen or if I clicked on the screen we're going to say if input dot get mouse button not BTT so input.get get mouse button 0. so we want to detect if we have pressed the left Mouse button on our Mouse and the left Mouse button on our Mouse will work as the touch input on our Android device as well so we want to detect whether we are pressing on the screen or whether we are clicking on the screen so to do that this is what we have to do all right so this one will check if we are clicking on the screen and if we are clicking on the screen now we have to detect whether we are clicking on the left side of the screen or whether we are clicking on the right side of the screen to do that first of all we need to get the position where we are touching so to do that I'm gonna create a new variable vector2 touch pose so this variable will store the position where we have touched and how we really how will we get the touch position we will get at from the mouse position so we're going to say input dot mouse position so here we have our Mouse and if I click on the screen it will call the input.get mouse button function so if you are pressing here it will call the input.get mouse button function and now as you can see the center of the screen is 0 0 and on the left and right we have the x-axis and on the left we have the negative values and on the right we have the positive values so here at the center we have 0 then minus 1 minus 2 minus 3 minus 4 here we have 0 1 2 3 4. so like this we have to detect whether we are pressing at a position where the x value is negative then we want to move the pedal to the left and if we are clicking on the position where the x value is positive then we have to move the pedal to the right all right so now we let's move back to our script and here as you can see we are getting the touch position and then we are getting the mouse position where we are touching but this will not work because this is in screen coordinates but whenever you are working on the games most of the time we need the world coordinates now to convert it to screen to word coordinates we need to write camera dot main Dot screen to no screen Point array screen to world point and then within parentheses we need to pass this position so now it will take this position convert it from screen point to World point and then store it inside this touch pose variable all right so now we need to check if touch pose dot X is less than zero that means we are pressing on the left side of the screen and else if touch pose dot X is greater than zero all right or you can simply write else here so if we are pressing at a position where the x value is negative then we want to move to the left move left and if we are pressing at a position where the x value is positive then we're gonna move to the right all right so how can we move to the left and how can we move to the right to move to the left we're going to say RB that is our rigid body it's not velocity so we're going to give a velocity value to our rigid body and how much is the velocity we can say Vector 2 dot left multiply move speed so here we are adding a move speed on the left Direction on our velocity value so now whatever value will give it will move to the left side of our screen the same way we're going to move it to the right side so we can simply go ahead and copy it and paste it right here and instead of writing Vector 3. left we're going to Simply write vector3. right so now we can add this force or multiply this force on the right direction of our screen and then we're gonna store the Velocity in our rigid body this is the amount of velocity that we are adding to our rigid body so now whenever we are clicking on the left it'll look to the left whenever we're clicking on the right it will move to the right but when we are not clicking anywhere we want our battle to stop otherwise it will keep moving so here right here with this if statement with this if statement the outer one we can add an else statement here we're going to say else that means if we are not clicking on the screen then we're going to say RV dot velocity equals vector2 dot zero so when we are not clicking anywhere we want the velocity of the pedal to be zero now let's move back to Unity and here we have our pedal and here we have the parallel script and here we have the move speed value so let's keep a value of 8 and then click on play but it will not work and when I click on the left and right nothing works and the parallel goes down so as you can see nothing works and the pedal goes down so what we can do is we can simply check the freeze position on X and Y so that the pedal will not move on the uh I think we're going to select the previous position to Y so that the pattern will not move down it will only move to the left and right and now if we go back to our script you can see that we have created this function touch move but we have not used it anywhere so you have to call it from somewhere so that we can use it to do that we can create another function void fixed update so here as you can see we are moving our panel using the physics functions or the physics properties and whenever we are using physics properties we need to call that inside this fixed update function so this fixed update function inside the fixed application we're going to write touch move so here we have our touch move function and now this fixed update function is automatically get called by unity again and again and again every physics frame so it will automatically get called every frame and this function will get called automatically because we have written it inside this so all this code will get called again and again and again and all these things will be happened again and again and again all right so now let's save the script and move back to Unity and this time we're going to click on play and I'm going to click on left and as you can see our parallel moves left I can click on right or file moves right so this way whenever I click on the left and right our parallel move and as you can see it cannot move beyond that because we have colliders attached to our boundaries the same way I can move it to the right and as you can see this is how we can move a panel if you want you can simply go ahead and move the change the speed from 8 to 10 and we can move it faster all right so you can adjust it according to your own wish and your pedal is ready so now we need to add some code or you know write a script for a ball so that we can make it bounce whenever we want and do whenever we want so let's start coding the ball so first of all inside the scripts folder right click create a new c-sharp script and I'm going to name it ball now I'm going to select our wall and drag and drop the ball script right here so here we have the ball script attached to the ball and then double click to open it in Visual Studio so the same way first of all we need to get a reference to the rigid body that is attached to the ball so here we're gonna first of all create a new variable rigid body 2D RV and then inside the awake function we're going to get access to it so RB equals get component rigid body 2D so here we are getting access to the rigid body 2D now we're going to use it to move or bounce the ball so now we're going to create another variable and we're going to call it float bounce Force and we're going to make it public because if we make it public then we can change it or change the value from our Unity editor now what we can do is we don't need this uh function so what we can do is we can first of all create a function named bounce so you can name it void bounce or to be more precise we're going to write start bounce so what this function will do is this function will make our ball move to a random direction or add four slower ball in random Direction so first of all you're going to create a new variable Vector 2 random Direction and how can we get the random direction to get the random Direction first of all to create a new Vector too new vector 2. then for the x value we're going to choose a random value and to choose a random value when you write random dot range and inside the range we're going to write negative 1 as the minimum value and positive 1 as the maximum value and then for the Y value of the vector 2 we need to write 1. all right so this is how we are getting the random Direction so this random direction is a vector to variable so we are creating a vector 2 here and the vector 2 has two values x value and Y value so this one before the comma whole thing is an x value and we are making the x value random between negative 1 and positive 1. and we are making the Y value as 1. now we need to get now we need to use this random value and add first overall in this random Direction to do that we're going to say RB dot add Force and we need to give a direction in which we want to add the force for that we can write random Direction and by how much amount we add when we add the force we're going to multiply it with bounds Force variable that you have created so that means we are adding a force in the random Direction by the amount of this bounce Force and then we can write a comma and here we're going to write Force Mode 2D dot impulse it will give even more Force to our ball and it will make it easier for us to make our ball move faster in lower uh bounce Force value so now all we need to do is we need to call this start bounce function sometime so in this case whenever we are clicking or pressing on our screen on whenever we are pressing any key we want to start this bounce function so inside the update function we're going to say if input dot any key down so whenever we are pressing any key or touching anywhere on the screen then we want to call this start bounce function so whenever we are clicking anywhere we want to make our ball start bouncing and from there we can start uh working on the game so now that we have done all these things so whenever we're gonna touch anywhere our balls to start bouncing so let's go back to our Unity editor here we have our ball so we can give a value to this bounce Force so for now I think let's keep a value of 10 all right and also I'm going to select the ball and change its gravity scale from 1 to 0 because we don't want it to move and fall down now if I click on play and if I press any key or just touch on the screen as you can see a random Force has been added to the ball so if I go ahead and start again this time again I can click on anywhere and as you can see here the force is added to the ball in a random Direction so I can click on play and start again and if I click random Force has been added and the ball keeps moving and goes down and I can also move the pedal to make the ball bounce so what we can do is we can make the bounce boards a little bit smaller like eight or five and that will be more accurate so now we have added the script to the ball as well as to the paddle so now we can move the pedal and also we can move the ball or make the ball bounce now we need to do other things like when the ball falls down we need to restart the game we also need to add scores and we also need to create the menu so that's what we're gonna do next okay so now let's see how we can restart the game when the ball falls down so to handle all these things all these different mechanisms of the game we can create a new script called game manager and we're going to create a new game manager object that will handle all these things so here we're gonna right click and you're gonna create an empty game object and we're gonna name it game manager then we're gonna create a new script inside the scripts folder and we're gonna name it game manager now we're gonna simply drag and drop the game manager script on our game manager game object let me wait for you to load it all right now let's double click to open it in visual studio so here we have our game manager now we want all our scripts to access this game manager script easily because here we will have all the controls of the game so that's why we're gonna make it a static instance and when we create a static instance all other scripts will have easy access to this game manager script so here we're gonna write public static game manager instance then we're gonna create the awake function and inside the work function we're going to say instance equals this all right so here we are creating a static instance of the game manager and we are setting the instance to this instance that is the instance that we have in the game to this game manager so if we write this code then all our other scripts will have easy access to this game manager and all its public functions okay so now here we're going to create a new public function public void restart so this function will help us to restart the game whenever something happens like whenever the ball falls down now to restart the game we need to reload the scene and to reload the scene we need to import or use another namespace because the functions that we have there are part of another namespace so first of all here we need to write using Unity engine dot scene management okay so now that we have imported the scene management now we can use different functions from this scene management namespace so inside restart we're going to say seen manager not mang oops not bank scene manager dot load scene and which scene we want to load we're going to reload the current scene that we have that is we have only one scene in the game and we want to reload that one so either you can write within quotation the name of the scene so in this case the name of the scene is game so you can write it or there are other ways to do it as well but in this case we're going to keep it simple and simply write the name of the scene so if you go to Unity you can see if you go inside the scenes folder let me wait for it to finish loading as you can see the name of the scene is game I need to write it exactly as it is written here so what will happen is whenever restart function gets called this game scene will get reloaded that is it will start from beginning and load again okay so now what we need to do is now we need to check when the pedal falls down on something that is when the pedal goes out of the screen so to check that what we're going to do is here we're going to create a new empty game object and you're going to name it uh what I'm going to name it we're going to name it let's say fall check okay we're gonna reset its position then we're gonna position it somewhere like this out of the screen so this square that we are seeing this is the camera or the camera view of a scene and anything outside of this is outside of the screen so we want the ball when what whenever the ball goes out of the screen then we want to restart the scene so we're going to select this fall check go to add component physics 2D and we can add a box caller 2D now we're going to click on this edit collider button and simply drag it to make it large enough like this so now whenever the ball goes out of the screen it will collide with this it will collide with this fall check and then we're gonna uh what you're going to do is we're going to restart the game so whenever our ball collides with this object which is outside of the screen then we can restart the scene all right now another thing you can do is we can go ahead and add a tag to this one so as you can see here we have a tag and currently it is untagged so you're gonna click here click on ADD tag click on ADD and here we're going to say false check okay now again we're going to select this one from the tag select fault check so now we have this fall check tag attached to this fall checked uh fall check object so now all we need to do is we need to go our we need to go to our ball script so here we have our boss script and we need to find a way so that we can detect whether the ball is colliding with anything so to detect that Unity has a very good function called on collision enter 2D so you're going to write void on collision enter 2D and exactly like this so now what we can do is we can simply check if the ball is colliding with the game object which has this fall check tag attached so to do that we're going to say if Collision Dot Game object dot tag equals fault check and if this happens then we're gonna say we can simply restart the game and to restart the game we're going to say game manager dot instance dot restart so this is how it will call the restart function from our game manager script from our ball script so here whenever our ball will collide with any other collider this function will get called automatically by unity and then inside this Collision variable it will pass information about the object with which we are colliding so from this object or from this variable we will get the access to whatever thing it has collided with so we are checking if we have collided with an object which has this false check tag attached then we're gonna simply restart the game or restart the scene so if you see here you can see here we have this fall check tag attached to this game object so whenever our ball will go out of the screen it will collide with this one and this one will call the restart function and our game will be restarted so let's see let's click on play click and ball goes out of the screen and it gets restarted again pull those out of the screen and it automatically gets restarted again so our restart mechanism is working fine as you can see here okay so now let's go ahead and create this coding mechanism for the game so whenever our ball will touch the pedal we're gonna increase the score so first of all let us go ahead and create an UI element that will display the score on the screen so to do that we're gonna click here go to UI and create a new text object so as you can see we have a text object here but we cannot see it in the scene if I double click on it we can see here we have a new text selected we can go here and click on reset to reset its position and now as you can see now it's in the center now I can double click to go here now whenever we create a new UI element a canvas object comes automatically with it so all our UI elements are part of this canvas now what we want to do is we want to make this canvas such a way so that it will scale with any screen size that we want so that our UI elements will not be so much smaller or bigger or go out of the screen so we're going to select the canvas go to Canvas scalar component and click on scale with screen size and for the screen resolution we're going to give 1920 by 1080. just the resolution that we are using so now it should work correctly now I can select our text go to this rect tool and increase the size of the box that we have inside which we have our text then we can put it on the center like this okay now we can reset the position reset its position like this so here we have our text position and now what we can do is we can select the text and from the alignment we're going to make it Center on uh vertical axis and Center on the horizontal axis as well so this is where our text is now now we can go ahead and change the font size and increase it like this and let's change the text to zero so that it represents our score now for the font we have I have downloaded few phones from the kenny.in website you can go ahead and download any other free phones from any other free phones website like da font or anything so I can go to font click on this small circle and now I can select the phones that I have here so I can select this phone or this font I think I'm going to use this one and then I'm gonna increase the font size like this then I'm gonna position it somewhere like this increase the fonts a little more I think I cannot increase it anymore so I think this one should look good let's check how much it can go so 10 100 so if we make anything more than 10 it cannot show it so let's go ahead and make the size a little bit bigger something like this and then position it on the center okay now it can show hundred thousand ten thousand anything that you want so let's make it zero and now for the color let's make it white and now uh the alpha value of the color we're gonna make it smaller something like this so that it becomes a little bit transparent something like this and whenever the score increases it shows somehow like this so this zero this one this is two this is 10 100 and this is how our score will be shown on the screen okay so now that we have created the score let's rename it to score text now we need to find a way to increase our score and also we need to find a ways to update the value of score on the screen okay so let's go back to our Scripts and here we have this game manager script and inside the game manager first of all we need to create a function called score up public void score up so this function will handle the scoring mechanism of our game so first of all here we need to create a new integer variable called int score which will store the value of our score so initially it will become it will be 0 and after that it will slowly increment to 1 2 3 4 like this and another thing we do is we need to create another variable to store the text element of this code that you have created the score text that we have created to do that here we're going to say text score text so this text is our UI element text but as you can see currently it says red that's because we have not implemented or we have not imported or you have not used the namespace inside which we have all the UI elements in unity so in unity if we want to use the UI elements then we need to write using Unity engine dot qi so now as you can see as soon as I write UI this text becomes green and now it is a valid statement so now what we can do is insert the score up function we're going to say score plus plus that means increment the value of score by one so whenever the score up function will get called we will increase the value of score variable by 1. and along with that we can also update the value on the screen so we need to update the value of our score text which is shown on the screen to do that we're going to say score text Dot text that is we are changing the text property of this cortex and we are setting it to score so we are changing the text property of our score text that you have on the screen and we are setting the value to the score value that we have so if it is 0 it will show 0 on screen if it is 10 it will show 10 hour screen but this one is an is a string variable and this score is an integer variable so we need to convert it to a string so I'm going to write score dot to string and now everything should work fine now our score will be incremented whenever this code up function will get called and it will also show the updated score on the screen now we're gonna make it public we're going to make our score text pretty well public because we need to drag and drop our score text in this reference so that we can get access to that scortex so now we need to go back to our wall script and as you can see here we are checking if our ball is colliding with the fall check the same way we're going to say else if Collision dot gameobject dot tag equals pedal so if our ball collects the pedal then we're gonna simply call the score up function from our game manager so here we're going to say game manager instance DOT score up all right so now whenever our pedal or our ball collects the pedal then this score up function will get called and our score will be incremented and also the updated score will be shown on the screen so here we have our game manager and here it is waiting for the reference to the score text so we're going to drag and drop the score text right here now I'm going to click on play and now let me click on here and when the ball touches it as you can see the score is not getting incremented so the problem is that we need to check if our ball is colliding with the game object which has the pedal tag attached but in this case our pedal doesn't have a tag so you need to go to this tag add tag create a new tag call it pedal and then select our pedal and add this pedal tag to it now if I click on play I can simply go ahead and start moving and our ball okay as you can see whenever the ball collides with this one this thing happens now as you can see there's a weird Behavior whenever you're clicking here the ball is going to another Direction and that's because whenever we are clicking on the screen the ball is getting a force and it is starting to get a new velocity in a new random Direction so let's go ahead and fix that so now we need to find a way so that our ball gets bounced only once when the game starts and then again no matter whatever key we press it should not bounce to do that we're gonna go back to our script we're going to go to this ball script and here we're going to create a new variable you're going to call it Bull game started so this variable has this variable can contain only two values either true or false and whenever the game starts initially it should be false and whenever the ball goes up we're gonna set it to True okay so initially it automatically becomes false whenever we create a bull variable then we're gonna say if not game started that is the game started is false only then if we are pressing any key then start bounce okay so if game has not been started only then if we press any key start bouncing and if the game has started then don't do anything because in that case uh we don't want to do that so after starting bounds we're going to say game started equals true so now the value of gamestart is true so this statement will not work and we gonna not add the bounce Force to the ball once again so let's see how that's working so here we have the unity scene I'm going to click on play and I'm going to click as you can see the ball goes up and the it restarts ball goes up it collides the pedal the score goes up and if I go ahead and move out the ball goes down and it restarts so our scoring mechanism and our restarting game mechanism is working fine so now let's start creating the menu items or the game name and the text and all these things so inside the canvas I'm gonna right click create UI and click on panel and as you can see a new panel has been created and I'm gonna call it game start UI so all our starting elements will be inside this UI element okay now initially we want to disable the score text so we're going to select the score text and disable it because it will help us to create this scene better now inside this game start UI I'm going to right click create UI and click on text so here we have another text and we're gonna call it we're going to give it the the name of our game you can give it any name that you want first of all let me go ahead and reset its position then I'm gonna give it a name ball then I'm gonna select direct tool double click on it and make it bigger something like this then we're gonna increase its font size Make It Center Center change its font to this one or this one anything that you prefer and you're gonna make it this one change its font size even more make it bigger on both sides Center it change its color from this one to a white maybe or yellow anything that you'd prefer um I think this one looks doesn't look that bad let's see if the white looks good or bad let's keep it like this ball then I'm gonna double click on it I'm gonna right click on it click on duplicate move it down and this time I'm going to change the text to bounce so ball bounce this is the name of our game and this is how it's gonna show here I think I'm gonna move the ball a little bit downwards let's double click on the ball put it a little bit downwards where is our pole we cannot even see it on the screen okay so I think we're gonna put it somewhere like this something like this okay on top of our pedal now that we have done it we need to create another text so I'm going to select the game start UI right click UI text or we can simply duplicate that one so let's delete that and duplicate this text so right click duplicate and we're gonna move it downwards and this time we're going to change the text this time we're gonna uh decrease the font size and this time we're going to change the text to press any key to start okay so this will show up here so that we know so that the player knows that whenever you press any key the game will start also go ahead and set both of this on the same line if we want let's see how that looks we can change it again later we can set it on the same line I think this one is looking better than what we had before so I think I'm Gonna Keep it like this so ball bounds it's all about adjusting you can adjust it and make it look like the way that you want I think we don't even need the second text then we're gonna disable this one select the first one make it bigger and change the text to ball bounce okay and then Center it like this increase the font size a little more increase it a little more so this is all about adjusting your game and making it look the way giving it the look you want so here we have press indicate to start and here we have the ball bounce and here we have the game start UI inside which we have all these things so if I go ahead and disable it as you can see all the elements will be disabled okay so that's what we cannot do so let's save the scene now we're gonna go to our game manager we're gonna go to our game manager here we're going to create public game object game start UI and then whenever the game starts we're gonna whenever the game starts we're gonna enable it or disable it depending on that okay so there are various different ways by which we can enable it or disable it so one way to do that is that whenever the game starts insert the ball function is that the ball script as you can see we are checking if input dot any key down then do this so here when the game started becomes true we can also say game manager dot instance dot games dot UI dot selective false so whenever you want to deactivate anything you need to call this set active function so now whenever the game starts we're gonna deactivate the starting UI and along with that we need to reactivate our we need to reactivate our score text okay so we can also say game manager or instead of doing all these things what you can do is we can do all these things directly from our game manager so inside the game manager we can create a new function we can create a public function public void game start and inside it we're gonna say game start UI dot selective false so whenever the game starts we're going to set it to pause and then we're gonna say score text Dot Game object dot selective to true so whenever this game start function will get called we're gonna deactivate the start UI and activate the score now from the ball script we need to Simply call game manager dot instance dot game start okay so now all these things will be controlled directly from here so now let's go back to Unity and here we have all these things now we need to Simply go to our key manager and see if we have any references to be given here as you can see it's it's waiting to get the game start UI so we're gonna drag and drop the game start UI directly here now we can click on play and whenever we click on whenever we click anywhere the game study goes on the score text appears and whenever the ball goes down it starts or restarts again so this is how the game will look like and this is how it is looking like so our whole game mechanism is ready so now I'm going to do is we need to build it and publish it and install it on our Android device so now let's go ahead and see how we can build and publish it on Android so we're gonna go to this file build settings and in order to make sure that the scenes are added to the build to do that you can simply click on this add open scenes then you can go to this player settings and here the player settings will open and here we need to do few things first of all as you can see the company name is given default company here you can give any company name that you want to give it you can also call it give it your own name I'm gonna simply write Charger games then here you need to select an icon you can either select this icon or this icon I think I'm going to select this icon then from the resolution and presentation as you can see here we have this auto rotation selected but we don't want that we're going to select landscape left because if we select auto rotate then whenever the mobile only vertical our game will get rotated and it will look very bad so now we need to do the most important things if you go to this other settings you can see here we have under identification we have this package name so this is very very important you need to give it a package name that's unique as you can see it is written com.chargergames.unity Android project and that's because I have already written Charger games there so now in newer versions Unity writes this automatically but if you if in your case it doesn't write it automatically then what you can do is you can write com dot name of your company or name of your domain like in my case charger games dot name of the game that is in this case I'm going to write Android project or Android test or let's say let's write ball bounce and then you can give it a version whenever you create a new version you need to give it give a new version here and then here we have few more settings make sure this arm V7 is checked and if you want to select this AR m64 then you need to change the scripting backend from mono to Il to cpv okay as you can see now this option is opened if you want to publish it on Google Play store then you need to do this otherwise you can simply clip it mono and then keep it like this and all these things should work fine as they are already here now we can close it and now I can simply click on this build button here we can create a new folder named build and open it and here I'm gonna give the file name ball s and click on Save and whenever you click on Save Unity will convert it to an APK and it will save it there and if you don't have the Android build support Android SDK ndk and jdk downloaded then it will not happen you will get a lot of Errors so make sure those things are installed so let me wait for it to build and then I can show it to you so now as you can see it has finished building the APK as you can see here we have the ball bounce APK file created so you can simply go ahead and copy and paste it or move it to your own Android device or you can send it email it to your own device and then install it and then you can play it on your mobile device so this way you can build it another way to do that is by clicking build and run so if you click on build and run then you can directly run it on your own Android device while after building it so to do that you need to make sure that you have an Android device connected to your computer via USB cable and once you have that only after that you can click on this build and run and it will automatically run on your mobile device also you need to make sure that you have USB debugging turned on on your device if you don't know how to turn it on then simply go ahead and Google search uh giving the name of your device company then it will show how to turn on the USB debugging or developer options in your mobile device now I have my Android device attached to my computer via a USB cable and I have also the USB debugging turned on so now it will be easy for us to install the build and run the Android app so we can click on this build and run and then it will ask us to select a folder I think I'm gonna create a new uh inside this builds I'm gonna name it Paul bounce to click on Save it is building and now it will find an Android device it will find my device and then it will install it as you can see it is creating the project creating the build and finally it will run it and install it on my mobile device here as you can see it says copying APK package to this device this is the name of my device so it's automatically installing it here and then it will run it So currently the game is running on my mobile device and I can play it here so in my mobile device I can simply click left and right and move it and the pedal will move and whenever the ball falls down it goes and restarts again the so the game is working absolutely fine on my Android device and it's working so this is how you can go ahead and build and Run the game on your mobile device okay so this way we have learned how to build a complete simple Android game from start to finish using unity game engine and c-sharp hey there thank you so much for checking out this video if you want to learn more and build some more cool Android games like 3D Runners 2D Runners 2D racing games and more of this stuff then you can check out my complete game development courses from the links in the description below so you can check the links in the description below join my courses and learn how to build more Android games publish them to Google Play and Implement video ads in your game and all this stuff so thank you so much for watching if you enjoyed you can subscribe to my channel to get more of this content and I'm gonna see you in another video hey everyone this is Raja from Charger games and welcome back to another new video now before doing the setup process let me show you what you're going to be building at the end of this project so as you can see here we have a very simple game I'm going to click on maximize and play and I'm going to click on play and you will see here we have the balls bouncing and we have to Simply tap on these balls to shoot the balls and destroy the balls and it's pretty hard because the balls are small and they are bouncing so it's not as easy as it seems so let me try and destroy the balls now this is the best part when you make your own game and you cannot play it yourself that's really awesome feeling so this way when you destroy the last ball and destroy all the balls you can see this you win message on the screen and you can simply click on this restart button and it will start playing again so this is how the final game will look like so now that we have seen what you're going to be building let's get started building our first game so now we need to go to this projects Tab and from here we need to Simply click on new and it will ask us what kind of project we want to create now in this case we want to create a 2d project so that's that's why we're going to select this 2d1 if you want to create a 3D project you can select this ready then from here you can give it a name let's name it Android game test and then you can select the location where you want to save your project I always save my Unity projects in this location but you can save it anywhere you want then you can simply click on Create and it will create a new project for us so let us wait for it so as you can see here Unity has finished creating a new project and this is how the new project looks like when you open it for the very first time now in my case I don't really like this kind of layout so what you can do is you can go to the top right corner and instead of default you can simply click on this 2x3 in this 2x3 layout we can see the scene View and the game view both together so the scene is the place where we create the game and the game view is what we see through the camera okay so now what we need to do is we need to convert it to an Android version as you can see currently it says PC Mac Linux Standalone so this is what platform your game will be uh when you open it for the very first time so now we need to go to file build settings and as you can see here we have this Android thing now if you don't have if you have not installed the Android build support while installing Unity then you will not see this option okay so make sure to install the Android build support otherwise you will not see this let me go ahead and show you two one more time let's say here I have my uh installed versions so what you can do is you can simply click here and click on ADD modules and from here you can simply select this Android build support and even if you have not installed it before it will install it from here and then you will be able to do anything that you want with unity so now you can simply click on this Android and then click on switch platform now this is really really essential otherwise you will not be able to build for Android you can do this tip after building this game but you have to do it at some other time so let's click on switch platform and switch the platform from PC to Android so now the build Target of our project has been converted to Android as you can see the unity logo next to Android now and here as well you can see the Android that means currently Android is a world built platform so now as you can see in the game view free aspect is written so from here we're going to click here and we're going to select this 16 is to 9 portrait and this is the resolution for which we're gonna build our game alright now as you can see here we have our scene already saved as sample scene you can go ahead and rename it to game and reload so now the name of our scene is game now what I'm gonna do is I'm gonna expand this game from our hierarchy click on Main camera and for the clear flags as you can see probably in your case Skybox may be selected you need to select this solid color and for the background color you can give it a nice orange light color or any color that you would like it to give I think I'm going to select this color because it looks good so I'm going to keep it here so now we need to add some balls to the game and also we need to add the boundaries in our game so to create the boundaries first of all I'm going to go to this assets folder right click create a new folder and I'm going to name it Sprites and inside this Sprites folder we're gonna keep all of our 2D Sprites or 2D images that you're gonna use in our game so inside this Sprites folder I'm going to right click create Sprites and then I'm going to select this Square and I'm going to name it boundary I think and this is the square that you're going to use to fill our whole game boundary so now I can simply drag and drop this boundary in our hierarchy like this and now we can simply go ahead and edit the size of this Square let's say I make it smaller like this and for the size let's say I'm going to give the excise to 10 so now that we have created our first boundary we have to give it a feature so that our balls can collide with the boundary currently this is just a 2D image so we need to give it a wall so that our balls can collide with it to do that I'm going to go to this add component and from here I'm gonna go to physics study and from here I'm going to select this box collided to the option so now as you can see if you zoom in you will see that here we have a green outline if I click on edit collider you can see here we have a green outline and this means this is a wall like collider which will enable this one to collide with other objects in our game alright now let me go ahead and change the color of it a little bit so as you can see here we have this Sprite renderer property from here we're going to select the color and we're gonna make it I think a little bit something like this of course you are free to give it any color that you want I'm just giving it as um of feeling right so now I'm gonna select this tool and then I'm gonna move it onto the top of our screen like this as you can see now the boundary has moved to the top of our screen now I'm going to right click and click on duplicate and as you can see now we have the second boundary I can simply go ahead and move it like this uh and let me unmove it by pressing Ctrl Z or I can simply go ahead and change the Y position to minus 4.89 and it will automatically be on the bottom okay now let me go ahead and duplicate it one more time this time I'm gonna move it like this then I'm gonna simply go ahead and change the Z rotation to 90 and now as you can see our square has been rotated by 90 degrees I can simply drag it to move it on the right side of our screen just like this all right now the same way I'm gonna right click and duplicate this one and this time I'm going to change its X position to minus 2.68 and it will be exactly on the opposite side of this boundary so now we have all these four boundaries set up now what we can do is we can create our new ball object and add it to our game scene now here inside the Sprites folder you can see I have this small image of a ball that I have got from kenny.nl website and you can get any image of wall from the internet and then you can use it so I'm gonna drag and drop it in our hierarchy like this and as you can see here we have our ball and we can move it like this now we need to add a collider to our ball as well so I'm gonna go to add component physics 2D and this time we're going to select the circle collided 2D and let me find where it is so here we have the circle Collide 2D so this circle collider is for the circular objects okay now what we need to do is we need to add a rigid body object to this ball so we want our ball to be affected by physics so Unity has some default things that handle physics and if we want unity's physics engine to handle the physics of this ball then we need to add the rigid body to the element to this wall so we can simply click on ADD component physics 2D and then we can click on this rigid body 2D so as you can see now we have this rigid body 2D component attached to the ball so now it has all the physics properties and as you can see here we have this gravity scale set to 1. that means now the ball will be affected by gravity and you can see that whenever I click on play so let me go ahead and click on play and you will see the ball gets affected by gravity and it falls down all right so now the gravity of the ball is working now we need to make the ball bounce because when the ball falls down of course we want it to bounce so to do that I'm gonna right click here and create a new physics material 2D and I'm going to name it bounce and I'm going to set the friction to zero and say the bouncing is to 1 that means now it will bounce for endless time now you can select the ball and as you can see in the circle collider 2D we have this material slot and it is waiting for us to drag and drop a physics material 2D here so I'm going to drag and drop the bounce material right here and now you will see when I click on play the ball falls down and it keeps bouncing so this is really cool so finally we have at least added a little bit of behavior to our game now what do I want to do is I want to make the size of the ball a little bit bigger because otherwise it will be very hard for us to tap on the ball and destroy the ball so let me go ahead and make the scale three three and believe me I was playing it in two and one and it's really really hard even in three it's really hard to play this game so I think I'm going to keep it three for now so let's name the ball ball one and it should be okay now we need to write a script and attach it to the ball so that whenever we click on the ball the ball gets destroyed all right so now inside the assets I'm going to create a new folder and I'm going to name it Scripts now you can simply create the scripts anywhere but it's always a good idea to stay organized and keep everything in the folders so that your project stays organized so inside the scripts I'm going to right click create a new c-sharp script and I'm going to name it ball and you have to name it exactly when you create the script otherwise it will not work so make sure the ball here and the ball here both are same otherwise it will not work so now I'm going to select the ball one and simply drag and drop the ball script on here all right so now the ball script is attached to our ball game object in the scene now I can simply double click on this ball to open it in Visual Studio or any other code editor that you have selected here so as you can see here we have our script opened in visual studio and let me make the font size a little bit bigger so now we don't need I think we don't need both of these things but still let's keep them there for now so here what I'm going to do is I'm going to write a new function called void on Mouse down all right so this on Mouse down function gets called automatically whenever we click with mouse on this object so in this case the script is attached to the ball so whenever we click our Mouse on top of the ball this function gets called and we can do whatever we want inside this function all right and this works for the touch as well so don't think that I'm using this on Mouse down function so it will not work on Android it will work on any touch device as well so here whenever the own Mouse down happens what we need to do is we need to Simply destroy the ball so here we're going to write destroy and inside the brackets we're going to write game object now make sure to write it exactly as I have written the G small and the O bigger are the boys it's not gonna work okay so now whenever the on Mouse down function gets called that is whenever we tap on the ball the ball should get destroyed so let's go back to Unity and see if this works or not so make sure to click or make sure to press Ctrl s to save the scene or save the script and now here we are in our Unity project so here we have our ball so let me click on play and you will see the ball keeps bouncing and let me click on it and as you can see as soon as I click on it the ball gets destroyed so that means our code is working as of now all right so now what else we need to do we need to also increase the score and we need to add few more balls and whenever we destroy all the balls we need to write on the screen that you win so now let's go ahead and create few more of these walls so I'm gonna select this ball right click and duplicate or I can simply press Ctrl D to duplicate it so here we have another ball I'm going to select this move tool and move it right here and I'm gonna I think I'm Gonna Keep the make the size about 3.5 and 3.5 and now I'm gonna select the Sprite color and I will change it to something like this then I'm gonna right click to duplicate it one more time I'm gonna move it right here and this time I'm going to change the color to let's say green and let's make it four and then again I'm gonna go ahead and duplicate this one move it to somewhere like this let's say and then I'm gonna change the color to something like this and now you can simply go ahead and organize the different walls as you like let's say I move it here I move it a little bit on this side like this so now we have all these balls ready in the scene and since we have duplicated it from the first ball so all the properties of the ball like the script like the rigid body and the bouncy material all have been duplicated to all these different walls so now if I go ahead and click on play you will see that all the balls simply go ahead and jump together in the game and now we can simply go ahead and click any of these balls to destroy them and you can see already it's a little bit challenging not that easy as it seems so as you can see here this is how we have destroyed all these files and what we want is whenever all this ball destroyed we want to print you win message on the screen now another thing you can do is as you can see the balls are falling straight so what we want to do is we want to make the behavior a little more interesting to do that I'm going to Simply select this boundary which is uh which is right here then I'm going to go ahead and duplicate it then move it like this then I'm gonna select the rotation tool and rotate it like this all right then I'm going to select the move tool again and I'm going to move it to a position like this so now the balls will fall on this and they will not bounce straight but they will bounce like this and this and this so this will give a more random Behavior to our game now I'm going to duplicate it one more time and this time I'm going to Simply change its exposition to not look in not the scale exposition to 3.69 instead of minus 3.69 and into change the Z rotation from minus point to three to from 1 to 3 to -1 to 3. all right so we need to Simply change the X position from minus to plus I need to make the Z rotation from plus to minus so now both of them are on the opposite sides now you can simply go ahead and move them by hand and place it anywhere you want there's no problem in that but I simply want to keep them same so now you will see if I click on play the balls will fall down and they will bounce crazy like randomly so this is not the same behavior that we had previously all right so this is what we wanted now what I can do is I can simply go ahead and select both of these boundaries by clicking shift so click the first one select shift and click the second one and then we're gonna select the color and then we can change the alpha value and make it zero so that means now we cannot see them on the screen but they are still present in the screen okay so as you can see if I click on play the balls fall down and we cannot see the boundaries but they are working under the hood so now we have all the balls bouncing and you can also destroy these balls so now we need to figure out a way to make our player win the game now to handle all this win and all these things we're gonna create a separate object named game manager that's going to manage our whole game so I'm going to click on create create empty and I'm going to rename it to game manager then I'm gonna go to the scripts folder right click create a new c-sharp script and I'm going to name it game manager and as you can see it changes its icon but don't worry this does whenever you name any script to K manager now we can select the game manager object drag and drop the game manager script on it and then double click to open it in Visual Studio so here we have our game manager script so first of all here we're going to create a new variable named score so I'm going to create float not float we need end so int score equals zero so this is an integer variable and the initial value of the score is gonna be zero now I don't need the start function so let's go ahead and delete it now here we're going to create another new function so we're going to write public void score up oops not start scored up so this is a function that will help our game to score up so whenever we will destroy the ball this will help our score to go up so inside this function we're going to Simply write score plus plus that means the value of score will be incremented so if it is 0 it's going to be 1 if it is 1 it's going to be 2. so every time the score of function is called the value of score will be incremented all right now we also need to check when the win condition happens that means whenever we win our game so to handle the win situations here we're going to create another new function called void win so inside this wind function we need to do and handle all things related to winning the game all right so now we need to figure out a way to call this score up function from our ball functions so that whenever we destroy the ball we call this score up function and increment the value of score so for that I'm gonna open the ball script here again as you can see here I have the game manager script and here I have this ball script so now inside this ball script before destroying the ball object inside this on Mouse run function what you want to do is we want to get access to the game manager in order to access the game manager we need to write game object dot find and inside it will write game manager so now it will find an object which is named game manager from our Unity scene all right so now when it finds will to access the game manager script from it so we need to write dot get component and write exactly I have written because otherwise it will not work game manager and then we need to call the score up function so write DOT score up so this is the whole code that we need to do so let me go through it one more time so when we are writing game object dot find game manager what it is doing is it is simply finding this game manager object that we have in our scene all right then we are simply getting the game manager script from this object as you can see here we have this game manager script attached so it is getting access to this game manager script after that it is calling the score up function from our game manager script that is this one all right so every time on Mouse down function gets called first of all the game managers score up function will get called and our score will be incremented and then the ball object will be destroyed all right so now in the game manager we can increment the value of score so now we need to check when the score goes above a certain value so in this case we have four balls so when we will destroy the four balls our score will be 4. and when the score gets 4 we want to make the player win all right so here we're gonna check if score is greater than equals 4 then we're going to call this win function all right so whenever the score gets greater than 4 we're gonna call this win function and the win function will handle all the related things that we need to handle related to score like displaying the score or restarting the game and anything that we want so first of all here we're gonna create a new game object or a new game object which will be our score text or our wind text so you're going to write public game object win text so this will be the winning text you win that will be displayed whenever we win the game so initially we will keep this text disabled and whenever we win the game we will enable it so that we can see it on the screen so inside the win function we're gonna write win text dot set active true so that means we are activating the win text objects win text object whenever the wind function gets called so now we need to go back to Unity and create this win text so here we are back inside unity and here we need to create the winning text so I'm gonna click create UI and from here you can either click on text or click on text text mess Pro text so I'm going to click on this text message Pro text and you can click on import TMP Essentials probably in the newer versions when you see this video this will come by default but right now you have to import it like this so now we we have imported the things we have everything set up as you can see here we have a text on the screen we can simply double click to see the text now we can we need to select the canvas and set it to a resolution so that it automatically scales to any other resolution otherwise as you can see when we make the screen smaller the text becomes like this so we need to make the text something so that it becomes small and big along with the screen so that we can run our game in all different screens all right for there we need to select the canvas for the render mode we're going to keep it at screen space overlay so now we need to add the canvas scalar component to it now in your case if you have the canvas scalar attached here already then you don't need to do anything but here as you can see the canvas scalar component is not attached so I'm going to go to add component and search here canvas scalar and simply click on it and as you can see the canvas scalar component is attached so now for the UI scale mode we need to select the scale with screen size and for the reference resolution let's give it 1280 by 720 so now our UI will be designed for this resolution and then for the match value we're going to give 0.5 that means it will match with width and height equally so now as you can see here we have our text we can select the text and let's rename it to win text then we're going to change the text value to U Win let's make it capital you win now we're going to change the font size to let's say 50 and then we can simply go ahead and change the size of the text by selecting this rigged tool like this we can select the rec tool and change the size this way like this and then we can simply drag it and as you can see the handles will automatically position it in the center whenever I keep it like this now you need to go to this alignment option and select the center for both vertical as well as horizontal so now our text will be at the exact center of our screen then I'm gonna change the font size to let's say 100 something like this and we can also make it bold and now it looks pretty good all right if you want you can simply go ahead and change the color as well like red or yellow or green anything I think I'm Gonna Leave it to White because that's looking pretty good in this case so this is the wind text now we're going to select this game manager and as you can see it is expecting this win text from us so we can drag and drop this wind text in this win text slot now we're going to select this wind text and disable it by default so at the starting of our game we're gonna disable this mean text and whenever the game is born this queen text will be activated automatically by our game manager because that is what we have written in our code so now let's go ahead and test and see if it is working or not so let me click on play and as you'll see all the balls will fall down and bounce so let me click on the false it's pretty hard and as you can see you win text is shown whenever we won the game and destroy all the balls so that means our code and everything else is pretty much working so now we have this working game where the ball Falls we can destroy the balls and wind text is already shown now one more thing that we're gonna do that is the last thing is we're going to create a reset button that will be shown whenever we win the game so when we win the game the reset button will be shown and we can reset the game and play it one more time to do that I'm gonna click on create UI and click on button and as you can see here we have a button we're going to go ahead and go to the rubric transform of the button click on this gear icon and click reset to reset its position then we can change its size a little bit and then you can drag it down like this I think I'm going to drag it down a little more to the bottom of the game screen then we can select the text property and change the text property to restart then we can change the font size to let's say 50 and now it looks pretty good we can also change the button uh or the text let's change the text color to something like yellowish or a little bit darker than that I think this looks pretty good and goes with the game so I think I'm Gonna Keep it like this so this is our restart button so now we need to add some functionality to the restart button so that whenever we click on this button the game gets restarted all right so to do that first of all I'm gonna click on our button and here we need to add an on click event all right but before that we need to add our code to our game manager so that we can operate our button so let's open our game manager in this case whenever we click the button we want to reload the scene and play the game again so in order to use those functionalities we need to use another namespace in unity so here we need to write using Unity engine dot scene management all right so now whenever we win the game and then we show the win text along with that we're going to create a new function here so we're going to write public void re restart so this function will handle the restarting of our game now make sure to make it a public variable or I'm sorry a public function otherwise we will not be able to access this from our button okay so inside here we're gonna write scene manager dot load scene and for the name of a scene neohost scene you need to Simply write the name that you have given to our scene in this case I have given the name game to our scene so I'm going to Simply write game I need to write it exactly as you have written all right so now whenever the restart function gets called our game will be restarted so let's go back to Unity and let's select the button now let's go ahead and click on this on click event to add an on click event to our button and now as you can see it is expecting an object here we need to Simply drag and drop our game manager because that is where we have our functionality or the script now from this no function button we're gonna go to this game manager and from here we're going to select this restart now make sure to create the restart a public function if you don't make it public you will not be able to select it from here so now we can click on this restart and as you can see here we have this restart function attached to this button so now whenever we will click this button the game will be restarted so let's click on play and check if it is working or not so you have clicked on play Let's click on restart and as you can see game gets restarted every time I play the game so now we don't want to show this button every time all right we want to show it only when the game gets over or we win the game all right so we want to show it only when we win the game so we need to do exactly same as we have done with our wind text we need to disable it by default and whenever we win this when the game we need to show it on our screen so let's go back to our scripts and below this wind text we need to create a public game object not game manager game object and we're gonna name it restart button and whenever we win the game we're gonna say restart button dot selective to true so this is exactly same as the win text so whenever we win the game we simply activate this button and before that we will not show this button in our game now we need to go back to the unity editor select the game manager and in the restart button slot simply drag and drop this button right here all right so now whenever we win the game the restart button will be shown so let me click on play and the balls are bouncing I can simply go ahead and click on them few more clicks and as you can see you win and the restart button is also shown and whenever I click on restart the game gets restarted again so we have finished building the game so now all we need to do is we need to build it and run it on a mobile device so that we can show it to other world and give it to our friends to play so let's do that now so now we need to do few more things before we can build it and run it on our Android device so we need to go to file build settings and click on ADD open scenes and make sure our game scene or the scene which you are building is added to the build settings if it is not added to the build settings then our restart function will not work and our game will not build as well then we need to click on this player settings and here we need to make few changes as you can see here we have this default company you can change the to name of your company I'm going to name it Charger games then I'm gonna change the name of the product I think I'm gonna name it ball bounce shoot and for the icon you can give it any icon I don't want to give this ball blue icon and for the default cursor I'm not going to change it then you need to come to this resolution and presentation tab because this is very very important so here as you can see auto rotation is enabled by default but we are building our game in portrait mode so we don't want our screen to rotate that's why we're going to select this portrait from here then we're going to go to these other settings and as you can see here we have something called package name and you have to change the package name and make it something unique every game in the world would have a different and unique package name now the easiest way or the most followed method to do this is to write the name of your company's website in backwards as an example my website is chargergames.com so I'm going to write com dot Charger games and then Dot and then we're gonna write the name of the game so here I'm going to write com dot Charger games dot ball bounce okay and then you can give it a version name and when you create an updated version of this game you can change the name of this version all right and then here automatic has installed is selected so you don't need to make any changes there and now we have all these things ready to publish our game so you can go ahead and click on build and build it and then you can run it on your device but another thing you have to do is now Google has made it mandatory to make a 64-bit build for your game as well so you can now simply update you cannot simply upload this 32-bit only version on Google play anymore so as you can see currently this arm 64 is disabled by default and in order to enable it you need to go to this scripting back end and from mono change it to Il to CPP and when it change it now as you can see that arm 364 is activated now you can simply go ahead and select this one and now as you can see our game will Target this arm 64. all right so now you can simply select this AR m64 and now everything should be okay all right so before we finally build it make sure you have installed the Android SDK and the indicate tools otherwise it's not gonna work so you can go to edit and preferences and external tools and make sure you have this Android SDK tools installed with unity Android Indica tools install vanity and jdk installed utility so make sure you have all these things installed or you can go ahead and add component from the unity Hub and install it as I have shown in the first section of this video all right so now that we have all these things set up we can simply go ahead and click on build and as you can see here I have created a new folder named build and inside the folder I'm gonna name our project ball bounce shoot and then I'm gonna click on Save and now you're gonna then you're gonna see that Unity will convert it to an APK and as you can see it says update or use highest installed so I'm going to click on use highest install now as you can see Unity has finally finished building the APK for us so here we have this ball bounce shoot app you can simply go ahead and copy it and move it to your Android device or email it to your device or do anything and transfer it to your device and install it manually now another way we can do is we can simply go ahead and click on build and run and before doing that you need to make sure that you have connected your mobile device to your computer via USB and then you can build it right here and install it on your mobile device directly so now I have connected my mobile device to my computer using the USB cable and I have also turned on the USB debugging mode on my mobile so make sure you have enabled the USB debugging as you can see here I need to do that from the developer options so you need to also turn on the developer options also you need to turn on the install via USB and also you need to check that you can install from unknown sources so once you have done that your mobile will be ready to build and run your game so now you can simply go ahead and click on build and run and let's rename it to ball bounce shoot 2 and click on the save and now you will see that it will build and use the highest installed it will build and here we have a mobile device you will see that as it keeps building it will come to our mobile device and we will be able to install it on our mobile device right here as you can see currently it says copying APK package to device and also here I have got an install option I'm going to click on install and now the app will be installed on my mobile device and it will start playing so now as you can see the game is going to start playing on my mobile device and here as well I can simply touch on mobile device and destroy the balls as you can see that's what I'm doing I'm trying actually so let me go ahead and destroy all the balls and as you can see as I have clicked and it says you win I can click on restart and start the game again so this is how I can simply go ahead and run it on my mobile device and this way you can run it on a mobile device or give it to your friends and tell them to run it on mobile device so this is how we have successfully learned how to build a complete Android game from start to finish and install it on your mobile device and run it as well so I hope you really really enjoyed how to build this complete game from start to finish with me so if you want to learn more and build some more cool games you can go ahead and check out my complete Android game development courses and other courses where you're gonna learn to build a lot more games like 3D zigzag game Fruit Ninja game tappy ball game endless Santa Runner game and 2D ball Touching Game and a lot of different games so you can go ahead and check the links for this courses from the description of this video and you can get them at the cheapest prices so you can take the courses learn a lot more like this and build your own Android games so thank you so much for watching this video I hope you really enjoyed and learned a lot so make sure to subscribe to the channel and like this video if you have really liked it and I'm gonna see you in the next video hey everyone this is Raja from Charger games and welcome back to another video now I have made a lot of videos about building Android games but in this video I'm gonna show you how to make a simple Android game in just 15 minutes so I hope you are excited and let's get started all right so let's start building the game I'm gonna click on New on Unity Hub to create a new project I'm going to click on 2D and I'm gonna simply name it let's say food eater tutorial and then click on create to create a new project and now Unity will create a new project for us okay so here I have created a new Unity 2D project and our project has been opened I'm gonna go to file build settings and click on Android and then click on switch platform so now as you can see after some time our project has been transferred to Android and now we're gonna start building the game so first of all from the resolution we're going to go ahead and select this 1920 by 1080 portrait for our game then I'm gonna go to main camera go to the background click on solid color make sure solid color is selected then go to the background color and select a dark gray like color something like this something close to this one all right then we're gonna go to our assets right click create 2DS right Square and then I'm gonna go ahead and right click create a Sprite Circle so now I'm going to drag and drop the square here and I'm gonna go ahead and rename it to player then I'm gonna select direct selection tool and go ahead and make the size much much smaller than how it is I think something like this should be okay a little bit longer on this side something like this then I'm gonna go ahead and change the color so select the player go to the Sprite renderer color and I'm going to change it to a color something like this all right now I'm gonna go ahead and select our Circle and drag and drop it here I'm gonna make the size I'm going to press shift and drag it to make the size much much smaller something like this all right then I'm gonna go to the color and change the color to a skylight color something like this then I'm gonna go ahead and rename it to let's say food then I'm gonna go to the tag click on ADD tag click on plus to create a new tag and name it food then I'm going to select the food and click on this and select the food tag so now we have the food ready so for the food we're going to select it we're going to go to add component go to physics 2D and click on Circle color 2D to add a collider to it all right so now we have our food ready we're going to go ahead and duplicate and spawn it all over the scene so duplicate right click and duplicate to spawn it duplicate then click on this and move it somewhere else anywhere you want to put it somewhere like this then again duplicate and put it somewhere like this then again press Ctrl D to duplicate and position it somewhere on the bottom somewhere like this select this one position somewhere like this and then again press Ctrl D and precision it somewhere let's say like this I don't have much time to design it correctly but you can take your time so here we have all our boots so now I'm gonna go ahead and duplicate the last food and this time I'm going to rename it to Danger then I'm gonna go to the color and select a dark black color for it so it is our danger and I'm going to go ahead and select on the danger and go to the tag click on ADD tag click on plus to create a new tag and I'm going to tag it to Danger then I'm going to select the danger and select the danger tag from here so now we have our danger obstacle ready I'm going to go ahead and duplicate again to position it somewhere else on our level then I'm gonna go ahead and right click and duplicate it again to position it somewhere else on our level something like this so now we have three danger elements if you want you can add few more of this you can duplicate it and position it let's say somewhere like this so now we have the danger elements and the foods ready so now it's time to create our player and add scripts to that so I'm going to select our player go to add component physics 2D and I'm going to add a rigid body 2D to it so rigid body 2D I'm going to go ahead and make the gravity scale 0 and the angular drag zero as well then I'm going to add component physics 2D and add a box collider tool to it so let's go ahead and select the Box color 2D here it is and now we have a box glider to the player now I'm going to go ahead and create add com I'm going to create a new script so right click create a new C sharp script and I'm going to name this one player player then I'm going to select our player so drag and drop the script on our player here we go then double click to open it in visual studio so here our script has been opened in Visual Studio first of all here I'm going to create a new Rigid body 2D variable rated body to the RB then I'm going to create the void awake function and inside that I'm going to say RV equals git component rigid body 2D all right so now we have the rigid body 2D now inside the fixed update function so you're going to create void fixed update and inside the fixability function we're gonna say rv.velocity equals transform dot up multiplied by mob speed and we have not created the blue Suite variable yet but we're gonna write it here and now we're going to create a public float move speed variable like this and then we're going to create another variable public float rotate amount and another one another one we're going to create float root so this will be the rotate value all right okay so now inside the update we need to check whether we are clicking on the left or right on the screen and depending on that we need to rotate our player so first of all we're going to check if input Dot get mouse button zero that means we are whether we are pressing on our screen or not and if you are pressing then we need to get the position so we're gonna save that uh here so we're gonna say Vector 3 the mouse rows equals input camera dot main Dot screen to World point input dot mouse position all right so now that we have got that we need to check if Mouse pose is less than zero then we're gonna move to the left or rotate to the left else we need to rotate to the right all right so we need to say it Mouse pose dot X is less than zero then we're gonna run it to the left else we can rotate to the right so if Mouse pose dot X is less than zero then we're gonna say root equals to rotate amount else we're gonna say root equals minus rotate amount row date amount all right so now at last we're gonna say transform dot rotate 0 in the X 0 in the Y and in the Z we're going to say root so this is the amount by which we're going to rotate our player all right so this code is ready so let's check how it's working so let's go ahead and go back to Unity so here we are back inside Unity we're gonna keep our rotation speed to let's say five and we're going to move our move our speed to 2 and click on play and here is our player we can click here it's gonna rotate on this direction we're gonna click here it's going to rotate on that direction so uh let me first of all let me try to play it once again so here's our player and I'm gonna click on this it's gonna rotate on this direction I'm going to rotate click here it's going to rotate on that direction so our player is moving and it's rotating as well so this is ready this code is ready so player is moving now we need to destroy these objects so let's go back to our Scripts and here we're going to detect collisions so we're going to say void on collision enter 2D if Collision Dot Game object dot tag equals food so if we are colliding with Foods then we're gonna destroy the food destroy Collision Dot Game object that means we are destroying the food and then if else if Collision Dot Game object dot tag equals danger then we're gonna simply going to restart the level so we're going to say scene manager scene manager dot load scene game so we're gonna reload the game scene and as you can see this has not been imported I can simply press Alt Enter to re-import it so now our scene management has been imported like this all right so when this happens we're gonna simply destroy it and then when the when we collide with the danger we're going to restart the level so here what you're going to do we're going to create another variable in score and then here we're gonna when this happens we're gonna simply go ahead and make score plus plus and then we're gonna check if score is greater than equal to five then we're gonna print level complete complete all right so this code is almost ready so let's go back to Unity to check how it's working so now I can click on play and you will see it's going I can click here to collect this and then I can rotate and come here and as you can see if I collide with this objects let's go ahead these objects we're gonna restart our level but currently it's not restarting because we have not added those things so we're gonna go to scene we're gonna rename our scene to game and reload and now if I click on play let's see how it's working so now I'm gonna go ahead and collide with these objects and as you can see game read starts every time I collide with these objects and every time I collide with these objects as you can see they get collided and our score will get incremented and when I actually collect all the objects our console will print you win or level clear in this case but currently we have only four objects here so level cleared will not be printed in this case so what I'm gonna do is I'm gonna go ahead and select any of our foods and right click to duplicate it and position it somewhere like here so now it should print to level clear whenever we get all these objects so now what I can do is I can go ahead and create a new UI text element to show up here so I'm gonna name this one win text and here I'm going to write level cleared then I'm gonna go to the text by double clicking it then I'm gonna go to here and click on reset to reset its position then I'm going to double click to open it here then I'm gonna change its color to white then I'm gonna go ahead and click the red selection tool and make it much much bigger then I'm going to change its font size to bigger something like this then I'm going to center it like this then I'm gonna make it something like this all right then we can simply say level cleared something like this that should show up whenever we finish the level okay so it should show up whenever we finish the level now we can go to our canvas and change it to screen space camera and you can drag our main camera right here all right so here we have our level select text we can double click to position it anywhere that we want all right so things are doing fine here you can also go to our canvas and go to scale with screen size and write 1920 by 1080 here so this is already looking good we can go ahead and change the font size a little more let's say bump it up like this so here we have bumped up our font size and made it somewhere like this something like this okay so this is ready now we need to disable it by default then we need to go to our script and here we need to create a new public variable public game object win text all right and when the score is greater than five we're going to say when text Dot set active true so it should show the wind text there so let's go back here and now whenever we collect all objects our wind text should show up here so last thing we're going to do is we're going to select our player and add a add a trail renderer to the player so that it looks good so we're going to go to add component effects and we're going to click on this uh Trail renderer right here so here we have a trail for the player we're gonna change the time from 5 to 0.5 we're gonna set the width to from 0.5 to we're going to click here and position it somewhere like this then we're gonna go to the color and we're gonna select this one and change the alpha to zero change the ending Alpha to zero then we're gonna go to the materials and select the default Sprites default material from here so now we can select the player and move it to see how it's looking as you can see this is how it's looking if you want you can make the size a little bit smaller something like this all right so something like this should work I think this is working right now so now what we can do is we can simply go ahead and click on play to see if everything is working so now our player is moving we can rotate our player by clicking here and it's it can collect the objects as you can see so what we have done is we have we need to actually select our player and here on the player script we need to actually drag and drop the win text object so we're going to go ahead and drag into the wind text object so now our game is ready to be played and we have finished it so now we can go to file build settings player settings and go to here and name the company I'm gonna name it Charger games I'm gonna name it purita tutorial as it is so let's leave it there now everything is ready now we can simply go ahead and click on build to build it I'm going to save it as food editor.apk and click on save to save the project or save the APK file so now as you can see the APK is here and we have finished building the game so it's complete so here as you can see the finished game is running on my mobile device and as you can see I can control the player by clicking on left and right on my mobile device and I can collect the objects and after collecting all the objects it says level finished and here I have changed the winning score to 3 to demonstrate it to you and when I collide with the black ones the game gets restarted so we have created a fully working game in just 15 minutes so congratulations for that now show it to your friends and family and enjoy thank you so much for watching this video I hope you really enjoyed watching this so if you if you want to learn more and build some more cool games and projects you can check out my complete game development courses all the links are given in the description so from there you can get a huge discount on all my courses so thanks for watching make sure to subscribe to the channel and I'm gonna see you later hey thank you so much for watching this video start to finish I hope you really enjoyed learning with me throughout the whole video so if you want to learn more and build some more cool games you can check out all my courses from the links given in the description of this video if you want to learn about c-sharp if you want to learn about building Android games Runner games 3D endless Runners 2D racing games zigzag car games all that I have covered in my courses so if you want to learn more about Unity build some more complete projects start to finish you can check out all my courses from the links given in the description of this video so with that being said I hope you really enjoyed learning with me and thank you so much and I'm gonna see you in another video very soon
Info
Channel: Charger Games
Views: 21,274
Rating: undefined out of 5
Keywords: unity android game development, android game development, unity android full course, android game develoment tutorial for beginners, unity android tutorial, build 7 games with unity, build 7 android games with untiy in 1 video, unity c# full course, best unity tutorial for beginners, complete unity course free, charger games, balloon popper game, unity touch joystick tutorial
Id: QetsheslhL4
Channel Id: undefined
Length: 304min 57sec (18297 seconds)
Published: Sun Jul 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.