Build A Complete Android Game Today - Unity Android Tutorial 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 going to 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 block 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 and 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 now 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 let me get 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 tool 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 zero angle it back to zero 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 our 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 gonna 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 going to go to add component physics 2D and I'm going to add a box collidatory to it so box collider 2D so same way we have added a widget party 2D and also a box collider 2D so with that done now I'm 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 color 2D and also a block tag attached so our player or a 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 insert 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 new 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 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 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 can 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 our 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 0 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 left half 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 with x2d and I'm going to add a box color to the component so now as you can see here we have a box collider 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 volume 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 and 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 we need to 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 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 function so 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 mistakes 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 what I need 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 I'm going to write if we are tapping 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 starts 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 spawner to one that means after every one second a new block will be spawned next we need to give a reference to the blockchain object as you can see here so for 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 the 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 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 collages the player the singers 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 you 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 tab 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 what I 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 gonna 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 Discord text go to this black 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 skin 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 drag 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 our 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 are 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 two 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 tap 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 test dot 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 game is 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 have to 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 have 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 going to 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 they need to 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 thank you
Info
Channel: Charger Games
Views: 107,090
Rating: undefined out of 5
Keywords: Unity Android Game Development tutorial, how to make android game with unity, create first android game, how to make android game fast, unity android tutorial, unity 2023 tutorial, easy unity game development tutorial, mobile game development tutorial, learn unity android game development, make an android game, complete android game tutorial, unity C# scripting, charger games, unity c# scripting in 15 minutes, unity android game in 1 hour
Id: 3y28i5n2g0Q
Channel Id: undefined
Length: 48min 19sec (2899 seconds)
Published: Wed Mar 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.