Make Your FIRST COMPLETE Game in Unity | BEGINNERS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome hope you're having a good week so far and now I'm gonna make a unity game so if you're new into game development and you want to get started with unity this might be a video for you that you can follow along it's gonna be a lengthy one so it'll take some time to go through but you can pause rewind come back get some coffee go back and forth up and down left and right and hopefully you'll get some out of this video in the end if you're experienced in unity maybe this is not the best video for you but maybe you'll get something out of it too so I made a game called beat ball about 20 years ago huh I realized it's been quite some time and it's basically an Arkanoid clone and I think arkanoid was a clone of the breakout game that I think the first time I played that one was on the Atari 2600 those were the times Atari 2600 I remember I had this little controller where you spun spun a wheel to control the paddle that was a lot of fun that got me hooked on to me well eventually making games so here I am many many years later and I'm gonna make a game in the unity and it's not gonna take that long in comparison maybe a couple of hours or one hour or a three-horse I don't know what this video will turn out like I'll try to tell you everything that I do in it so you can hopefully learn some things from it some of the important things in this video that you might want to take away is how you can transition between different game states so it's important for a game to be able to go from for example a menu into initializing a game and then having the game play maybe switch levels and then eventually come to a game over screen and then revert back to the menu so if you pay attention to the game manager section in this video you might be able to pick up how you can implement that in your own games I haven't seen too many tutorials go through that bit because it's quite easy in unity to throw together a game that plays pretty well but it's just the one level and you don't really transition in and out so just the basic core mechanic might be easy to implement but you'll need some sort of a game manager in the end or some state management or a state machine basically this is an example of a very simple implementation there are more complicated and better ways to do it but for this purpose I think this should be quite good as a starting point for you another concept that I go through here is I have some menu screens as you transition between the different game States ill enable and disable different menus like the main menu screen and a game menu screen and also a level completed and a game over screen so that might also be something that you could you could find useful to implement in your own games tag along on the journey let's make a game together and let's have some fun in unity making a breakout clone I should say a beetball clone in this field I'm only gonna be using mainly unity itself and I'll make some really quick and simple objects in blender as well but those aren't gonna be super simple this time and you could I'll start off by basing on primitives within unity so you can get started and I'll show you just how you can swap those assets out for some simple objects as well you can pretty much use any unity version to follow this I'm using unity 2019 dot 2 and but again you can just whatever you have loaded should be sufficient for this we're not going to use any special features so I've got that one loaded and the first thing I like to do is to organize a few folders that we're gonna need so we put things in the right place so here under the assets folder we can create we're going to need a few folders so let's call one scripts and we're gonna need some materials and the textures can go in that one I think we'll just start like that the first thing I'm going to do is I'm going to take this camera and change it from solid color we don't really want that sky in the background for this game so I'll change the main camera to solid color and I'll switch this one to black the first thing we'll do is we'll throw a ball in here so I'll right click here in the hierarchy and I'll create a 3d object and let's make a sphere let's reduce it in size a little bit make it smaller it can stay white and we should also move the camera back a bit so we'll move it in the z direction back about 50 units and we'll also let's press f2 on this one and call it ball and then we'll also create the pad paddle create a 3d object we'll make a cube for that one so let's call on paddle we can move this one down a bit I'll hold the control key as I move it down it'll snap them and let's make it four wide maybe and 0.25 0.5 hi the depth is fine the way it is so now we've got our basic pad what I'm gonna do as well I'm gonna actually switch from since I want this to be a bit of a 2d looking game but utilize the power of 3d for this particular game if you look here if I slide this pad to the side you can see that it gives quite a lot of perspective on the pad also if you had a bunch of bricks sometimes you'd want maybe the 3d look but for this game I'm gonna switch this camera here to orthographic so I select the camera again and I use instead of perspective here I'll switch it to author orthographic have a look at the pad when I do this well first of all it shrunk it a lot so let's change the size of this one a bit let's change that instead of the orthographic camera doesn't operate on the distance it operates on this size here instead so we'll do that one and the pad we need to move back up a little bit because it ended up out of view so here let's move it - maybe 18 or 17 and here's our pad and as you can see now if I move this sideways zoom out a bit you can see that it keeps the perspective the same way all the time you don't really get that 3d look here so for this game I want to use this you can also see that when I move it let's see we have to view from this direction here because forward is into the scene so if I move it left now it'll move left on the screen so now we've got this is what the game is gonna basically entail user or the players gonna control this one left and right and the ball is gonna be bouncing or we'll use physics for that for the ball to move we're gonna add here let's select the ball and add something called a rigidbody oh you can just type it here and they'll filter it for you and we're gonna use this one just press play now you can see that the ball falls but we don't really want to use gravity for this we want to control the ball movement otherwise if we can actually let's see if we move this pad into place here and then we use this ball and then we have to if we just drop this now press play and see what happens it just drops down and hits this Collider and stops and we want the ball to always have a constant motion so we can't really use gravity for that so on the ball here I'm gonna deselect gravity and then I also want to change this from non to interpolate and that means that the movement will be a little bit smoother so I recommend using this interpolation if you're going to have objects that you want to look as smooth as possible when they're driven by physics this is an important one to do okay and if I press play now nothing will happen basically the ball will just be stuck there and we can't control the pad yet so the first thing we'll do is let's start with the pad to get so we're able to move this one actually oh no let's not do that since we've added the rigidbody let's put the ball into a bit of motion here and for this we're going to need a script so I right-click here in the script folder and I do create script and let's call this one ball and this is gonna be a c-sharp script and when it's added it I'm gonna drag it onto the ball and here we go and this one doesn't do anything yet it's just a touch to the ball so if I double click on this it'll load Visual Studio or whatever IDE you have configured want to give feedback no no ok so here we are if you haven't done any scripting before this is a good opportunity to get started let's delete some of these comments here and I'm gonna add a speed variable here so we'll do will define a float which is a floating value in other words numbers with the decimals and let's call this one speed and I'll sometimes I keep I start private variables with an underscore so I know this is private only used within this script and it's up to you if you want to do that or not and let's just fix that to 20 units per second then we want to define we added the rigidbody to the ball so let's define that one here as well rigidbody because we'll need to access this one and then we're going to define something called a velocity here which is a vector three and that's going to be the actual movement speed of the ball and it also includes that the direction so that's why it's a velocity the speed is the actual speed the magnitude and the velocity is the speed that it has but with a direction as well so let's start by doing this and in the starch omona behavior if you haven't done any mondieu behavior first start runs every time the game object is created in the sequence and it only runs once so here we can use this opportunity to define the rigidbody we want to use something called get component rigidbody and this is because we added the rigidbody to the actual ball here I'll show you as you remember here we've got this component here called rigidbody and we want to grab axis we won't have access to change them read this part and set these parameters here and then let's get some movement here as well first of all in we can set the ball into a motion here let's move it the ball straight down when it launches so rigidbody velocity we want to access the field velocity here and vector three down to start moving down times the speed variable so now if I press play in the start it'll grab the access to this rigidbody component and you'll set the velocity to move downwards at the speed of 20 units per second if I press play now now we have it and it doesn't bounce yet it's because it hits the collider and it's only keeps pushing it down already so we're not gonna see any any movement yet so we need to take care of that one we need the ball to bounce since the ball is not moving when it collides we need to add something here and there's another predefined method called or it's Imani behavior here called on collision enter and if you press ENTER you look predefined it with this variable here and what we need to do now is that when the rigidbody hits we need to have the velocity change and there's a good predefined feature or a function here for vector three called the reflex which is exactly what we want because we want if we have a an angle like this and the ball comes in from here we want it to be reflected and come out so if the normal of the plane or whatever it hits is perpendicular to whatever it is here like a brick or a wall and and it's coming from here we wanted to reflect them bounce based on that normal and here we're gonna use the rigidbody velocity that we had and then we're gonna grab the normal from the collision itself which is accessible through this collision variable here and it's stored in in an array of contact points and we'll just grab the first contact points that's where we put 0 here we only care about one impact and we get the normal from that which is the perpendicular angle of the actual impact itself so now the ball comes down and it it did probably bounce but what we need to do is that the impact probably absorb the velocity itself here which could be reduced to zero so let's let's create another there's another mana behavior called fixedupdate there's the one called update runs every frame so as fast as your game runs in whatever frame rate it will run every frame but for physics stuff you want to put that in your fixed update folder not in the folder in your fixed update that runs out a hundred Hertz so 100 times a second by default in the unity configuration so that's where we're going to be manipulating any physics in here we'll first of all will force the the speed to always be the speed that we want of the ball so rigid body velocity let's set that one to rigid body velocity and then we grab the North we set the normalized value so basically that's taking the the whatever speed the ball is moving whether it's really slow or really fast then normalized will actually take that value and shrink it down so the the magnitude of the direction is one-one units so we get that one and when it's one unit here normalized we multiplied by speed in this way we can always force the speed to be the speed that we wanted of this ball which is 20 units per second so this impact now okay we still don't get it and that could be again because of the impact absorbed this so let's actually store we created this little variable here that we can store every frame we store in this variable will store this value here control-c control-v to copy it on and here we'll change it from the this one to the velocity that we stored before the impact happened and let's try this one again and now we have a good bounce here and it'll just disappear off screen now because it doesn't have anything to bounce up on top here it bounces straight off because the normal of this surface is upwards and the ball is coming in straight from above the going down so that's why it bounces straight back up okay the next thing we'll do is we'll put some walls in here so the ball doesn't fly off into infinity and we'll create a new empty here and call it walls let's reset this and doesn't really matter where it is but I like to keep it in the center and here let's create just a simple box a 3d object cube let's call this one wall and then we can see it throws it into the center of the screen here we can hold the control key and move this to the left here so it snaps all the way out it snaps by you a single unit at a time and since we've configured the the size of this camera and the orthographic view to 20 then it should always align up with these edges here and then we can change the height of this wall so let's go it's at minus 35 and oh probably about 40 high we should actually move the camera the camera is on white zero at one here so let's move it down to here basically it's the same as moving it up and down so we have our left wall here and it's already got a box Collider here which is good the ball should bounce off this one by all by itself now and we can just hit this one and press control D and then we flip this x-coordinate and then we have our right wall don't have to do anything more with that we press control D on this one as well and call it ceiling and this one will have to change because we have to snap X back to zero and then it needs to be 40 wide or no it's actually gonna be wider now because it's a wider it's minus 35 to 35 so that's 35 times 2 is 70 and then we hold the control key and we can move this one up to the top here and it goes out of the screen all the bits so let's move it down by half a units nineteen point five so now if I press play these walls here should contain the ball and it'll keep bouncing up and down we might run into a bit of a problem now because the collisions even though it looks quite good now it comes down and it bounces off this pad pretty nicely but eventually when it starts bouncing it could get a little bit out of shape and start flying into the scene and we want to keep this ball fixed here so it can't come in or out of the screen basically if I were to tilt this paddle a little bit here let's flip this one like this we're not gonna have it like that in the game but if the ball comes down now you can see that it flies off into the screen and it'll eventually just disappear and get out of view and let's actually flip that one back let's rotate the back again because we can do this by it on the ball here we can select it and then here on their constraints of the rigidbody we can freeze the Z position and that will make sure that it can only move left and right and up and down so even if it's angled like this now when the ball comes down you'll still just bounce back up because we've forced the movement to only exist in the XY pane here plane okay so we can tilt the paddle back to its original rotation and now we can trust that the ball is always in the center of the screen here and it can't escape in front or behind the walls here next thing we'll do is we'll make you able to control this paddle here so for that I'm gonna create a new script that I'm gonna call player since we're a player that will be controlling this one and I'll drag this script onto this paddle here that's what we'll be controlling okay and for the paddle and let's add a rigidbody here for this one as well we'll use physics to control this one and let's switch off gravity and for this one we want to freeze the Zed position as well I'm gonna make sure you can't go in and out of the screen to disappear and we also want to fix or freeze the rotations in all directions because this one is only going to be moving left and right and we don't want anything to to rotate it out of shape so that's why we're setting these as well and then I switch interpolation here to interpolate and that'll make it smoother movements as well and now let's double click on the player script here and here like we did with the old one here we did we created a variable called or the rigidbody we need to access so let's put call this one rigid body and in the start let's grab that reference rigid body equals get components rigid body and here we want to use the fixed update remember because we're dealing with physics so fixed update and here we set the rigid body there's a method here called move position and whenever you move a rigid body into position you should be using this one if you've if you're used to using translate transform drop translate that could be used but this rigid body move position takes into account the physics engine and it will if you have interpolation enabled then this force movement will actually take that into account as well so that's why this one is recommended to be used and here we create a new vector3 and here we go to we need to access the camera camera main dots screen to world's point and the reason why now is we're gonna grab the screen or the mouse position and that one we need to that exist in screen space so whatever X&Y coordinate the mouse is that and we need to convert this into a world point since the unity operates in the world space with the 3d world space we need to use the camera that we're using to view this and convert the screen to world point that's why we're using this and we're only going to be using this for the exposition and for this one we need to create a new vector3 as well in here and we're gonna grab the input mouse position and we only want the exposition of this one which is there left and right movement and y position doesn't matter so we'll force it at zero because we're not going to be able to move this paddle up and down and we move the camera 50 units back so in order for the X&Y positions to be correct then we need to specify the same distance that we have from the camera all the way to the ball it's or to the pad itself paddle so that's why it's important to keep this the same distance away that you are from the object that you're about to control and for this we just want the X components basically this long text here just takes the mouse input position translated from screen space and translates into the world space at the point of where this paddle is and then for the height will put minus 17 we had for the Y position which is down at the bottom of the screen and for the Z position we'll stick it at zero okay and let's try this if I press plane out okay I missed it but as you can see the pad is actually moving now so I can move it left to right first of all let's hide this mouse cursor because it's in our way so so in in this script for the rigid body or in the player we can just do this chorus or adults visible equals false so when this runs we should hide our mouse cursor now we can just focus on the control in this partners then we also got some funky movement as the ball hits there seems to be flicking a little bit so let's see what could be causing that let's try to freeze the positions first of all okay it still funk got some really funky behavior here so that wasn't it maybe it's because we put the interpolation on it's trying to correct yeah that seems to be it so in this particular case since we're miles controlling it we what we don't want to have that one we don't want to interpolate it we want to let this grip control it but here we see another issue that it moves on impact here and to fix that we can enable their kinematic here so is kinematic we'll set to the pad that basically makes it to not be affected by any impact or anything like that in the movement so here we go it's now solid as a rock solid there's no forces that can bunch this object at all we just force the movement through the mouse script here okay so I've got our ball moving we got our little pad moving here now the next thing we need to do is have something that it should hit so it wouldn't be much of a fun game if it just it's not very challenging see if I can win I probably will so let's create that brick for it to hit let's right-click here create 3d objects and let's create a cube and let's call this one brick and in here well let's put it in the center of the screen first of all and let's make it too wide not too wide two units wide and 1 unit high so let's make it a traditional let's do this add 0.5 to get that traditional brick look this is what I'm used to back in the day with Arkanoid when I played Arkanoid this is the problem about the same proportion I think probably totally wrong but it looks a bit similar anyway okay so we've got a brick here now let's move it up so remember we can I press W on the keyboard here I can get this little nifty shortcuts tool here so I hold a ctrl to snap a movement we're gonna use that a lot of when we create the level because it's really useful when it snaps and we have it here it's got a box Collider so now we should already be impacting here so if I can control this it'll just bounce up and down so the first thing we're gonna do is let's create a couple of blocks up here actually so if I do here's another tip now as well if we're gonna make this brick reusable it's quite useful to have it as a prefab so as a prefabricated object because then you can make changes to it in the prefab instead of in the scene so you can save yourself a lot of time by reusing that prefab so what I'll do here is I'll just create a new folder right-click and do folder and call it creep ups and let's just drag this brick now into the prefab folder here and it'll turn blue and that's to indicate that it is a prefab so if I make a copy of this one now so I press ctrl D to copy it and move it inside then I've got another braid but if I change it down here then the properties will affect all of these ones that are in here so this is really useful when you want to change properties to a brick maybe the color or the amount of points that you're going to get when you remove it then you want to change it in this prefab instead of here because then that will apply to all the objects we can also see that the bricks are a bit too big because they form a single looking object and here's an example where we can modify this if I were to modify this one as you can see it only affects the left one and this is exactly why we wanted it as a prefab because if I go to this prief instead and double-click on it I can change this width now to 1.95 and the height to 0.95 when i go back here now you can see that it's created a little gap here and that's the beauty of these prefabs so use them a lot that unity is built on this concept of prefabs it's really useful really powerful so you should familiar so familiarize familiarize yourself in it so and we've got a couple of bricks here let's make three and maybe we want the space a little bit more so that's easier to fix just double-click this one put 1.9 and 0.9 here instead and now we've got a little bit more space between them but still nothing happens when I press play I just keep bouncing them on and off so now we need to create another little script here and let's right click here and create a new one c-sharp script called the brick and let's drag this one onto this prefab now that's why we want to be controlling this so we've got those little brick prefab script here or the brick script let's double click on it to edit it and here we're going to need to do a few things so first of all let's define some variables again up here let's do let's make this one public so we can specify in it from the outside hits for example let's default it to one so if I save this now you can see that a new variable appears here on our brick from double-click you can see that we can access this from main this is called the inspector and we can configure that this brick should be destroyed after you after you hit at one time and as soon as you put public here and then it'll expose it into this inspector so we can put hits equals of one here and then we want to say how many points do you get if you hit this one and let's set that one default oh you got a hundred points to destroy one block here and again like the other one it appears here so here you can just change this out here now and effect maybe you get two hundred points for this brick or you get a hundred or 150 or a thousand maybe a million are you a fan of big scores like where you go into the millions or do you like tiny scores like one two three four five I like big scores but I'll stay at 100 for now that's a stock easy and then build a little bit onto this one so we'll create the new one here I remember this on collision enter that's what we'll be using so that detects whenever this brick now gets any form of collision it's gonna be the ball in our case but if anything hits it now this method here on collision injury is gonna get called so here first of all we want to do is hits we want to decrease by one because as it gets hit you reduce the hits too and we already hit zero at the first time since it defaults to one here and then what we want to do is we want to here I'm gonna put some logic here to score some points we haven't got anything to do that with yet so let's just put all the reminder here and then we'll do here if hits is less or equals to zero then we want to do something we want to destroy this game object and game object is a predefined variable that you can access to access this particular game object that this script is added to and this is gonna be the brick so when we hit now and the counter reduces to zero you can try it well I suck at this game and it's the simplest one in the world so here we go one hit it's gone and that's because it got called it got hit the on collision enter was hit and then we can demonstrate here if we just increase now that brick hits two two then it should allow to bounce twice here so first hit nothing happened second hit now BAM it goes away so but let's keep it at one for now and that's pretty much all we need at this point let's put some flashing effects and things like that when you get hit later on but for now let's just keep it like this now we can create a little maybe we don't want it to be white anymore so in the materials folder here we can right-click and do create material let's call it brick one now let's just drag this one onto the brick so look like we're not done be quick on it but we'll change here their color may be too blue I let's change this shininess oh I didn't get a sign so let's double click on the brick let's create some more bricks in our scene here so I can select all of these three now I hold the shift key to multi select like this and then press control D to duplicate them hold the control key and you can slide them sideways like this and I select again shift select control D move them sideways and then we can select this whole thing and hold the control key and move it all the way to the edge control D again move them there control D again and move them to there and then we got a few extra here we can take those away one - too many at least one too many there we go so we've got our first row of bricks now the reason why the ball keeps bouncing up side up and down like this oh yeah no because it removed a brick to the side we actually got some movement into the ball and this is starting to look like the traditional breakout clone game now maybe you played it on the Atari if you're as old as me you could have played it back on the Atari 2600 I think it was called and maybe on the Amiga no product arc and I was on the Amiga I think and on the Atari ST 520 you correct me in the comments if I'm wrong I might be in these names all wrong but I'm old so that's ok ok we can also make let's select all of these ctrl D and just move them up here let's make some layers here maybe you want to skip a few rows here as well like this that's it and now we're already laughing a little bit at we have a game to play we don't get any points yet but that's ok then maybe it looks a bit too too static now after all we're in a 3d engine making a 2-d looking game but let's utilize the power of the 3d even if we've got this orthographic view on the camera we can still have some fun with it and we can go back into this brick now and for this let's let's move the rotate these a little bit time so in the update here it's just to transform rotate vector three actually let's make this public as well so we can configure it from the outside do public vector three and let's call this one rotator and then we do rotate rotator and then here it's important since we're doing this in the actual update frame instead these are not rigidbody objects they're just boxes with or cubes with colliders so in this one we're gonna use this rotator and we're going to multiply it by time.deltatime and this is really important because you want to regardless of what frame rate the game is running out we want to rotate these at the same speed so even if you've got a super fast computer and it ran there's loads of frames every second then doesn't matter what what frame rate you've got to this will enforce that the rotation keeps the same speed all the time and now on the brick care we can actually configure the rotation and let's rotate it around this red x-axis we want them rotating like this so maybe I'll put 45 here if I can type and when I press play now all the objects should start rotating okay oh I meant to put it on the x-axis sorry what is that so let's put 45 there and that's hit play and now all the all the bricks are rotating and this is some fun stuff that you can have you see that the bricks have got some lighting effects to them already with the shadow and things so it could make some interesting looking effects and when they're all moving in sequence maybe that's a little bit boring so we can go back into the script here on the brick yeah let's just start by offsetting all of these so in the start remember that runs when the game starts or when the object is created it only runs this once so we can do the same here we'll do transform that rotates and then just do the rotator and then let's just base it on the position of this in the X Y position so we get some alterations between them because if I do transform position X plus transform position Y then we'll start having some variation here who should be anyway now they all start at some different angles and you get some funky effects with this and let's it seems like it's going a bit too much so maybe reduce its multiply it down so it has less of an effect and now when we press play we get a bit of a like a more of a Rolie rotational effect too I'm not gonna stop saying mmm all the time because it takes me forever to edit those out let's do it one more thing as well when maybe if we put this brick material let's bring it the metallic up here a bit and this let's press play you can actually modify this while you're in play mode here and when we brought their reflection up here and let's go back you can see that you can get some metallic looking effect here on the bricks which could be quite cool to get this little shiny effect another thing we want to do now is when we press play and let's increase the hits here so on the brick here let's make you a be able to hit them twice it looks a bit boring when you hit it and nothing really it just doesn't seem to affect the impact at all until it disappears and we can actually do something here let's create a new material and call it hits or brick hit and if you played that good all they're side-scrolling games from again the amigo or the Atari time then sometimes when you hit objects they turn pure-white first split second and then they revert it back just to indicate that they were hit so let's replicate that let's just create the material here and it will bring it's already white but we want to enable this emission here and put white because we want the material to be pure white that's how it was back in the day so let's bring this material up to bright white let's go back into our script here and let's make this accessible public material and we'll call this one hits material and now we can actually earn the brick here I can drag this if I scroll down you can see that it's exposed to this hit material so let's bring this brick hit onto this and nothing's gonna happen yet because we haven't told it to do anything with this one but if I go back here now into the brick script then first of all we need to the original material so we know what to go back to after you've been hit so we did a private variable here called org material and then in the starch here let's grab a reference to that so it'll store whatever material it is into this folder here and to access the current material we actually need another one here as well and that's something called a renderer that's what's responsible to render this object on the screen so we need to get that one first so renderer equals get component that's what you can use to grab the renderer and basically it's this one that we want to grab it's got here a mesh renderer here so that's a derived object from from the renderer us so now we can actually use so the original material equals renderer dr. shared material because materials should be shared as much as possible which increases the speed and that's the default material that's the sign whenever you drag like I did you drag the brick text or the brick material onto this object you can access it through the render and the shared material here and now when you get hit we need to do something as well we've only set up the the requirements now to actually make the hits here so when the material gets hit let's invoke first of all let's change the materials so renderer we have share material equals hits material let's do this so when you hit it now I should go white but it stays white now and because we're not resetting it back to the original value and that's what we need to do now and that's why we stored this original material in here so now let's do let's create another method here called restore material and basically that just that's the same a renderer share material equals org material so that was the original material but this one's not going to call by itself you need to tell it to be called and we don't want to change it straight away because then it'll flash so fast that you couldn't see it and there's a unity Scott and quite a useful little helping thing here called invoked and you have to use this with a string parameter risk or a material that's the name of this function that we want to call let's call it after 0.05 seconds of after so it's gonna register the hit it's gonna set the white material and then it's gonna invoke this I want to restore the material in 0.05 seconds so then I'll run this lower one and you'll revert back to the original material so now we should have a little flush quick flush effects and you could have some formulas and rescale objects and things like that as well but let's just keep this a little flashing effect for now if you actually bring the ball up here to the center we can see this a little bit more in effect holder control key you move it up here press play now you can see the hit effect is in effect there and get that oh that old-school breakout organoid hit effect love it oh that was a nice Bowl okay now what okay I think time has come now to start we need to score some points it's no fun playing a game if you don't get any points and for this one now we're gonna create something called a game manager and that's gonna be a new concept probably for some but let's create let's do some cap housekeeping here first actually and create a new let's create a new material call it level 1 let's keep it at the roots here we set these parameters and let's move all these bricks now I'll select with a shift key and then drag them into level ones created and that's just so we can keep it a little bit more tidy here so we've got our level 1 here and now I'm going to create a new object here and call it the game of manager and this is this might be our final script that we need actually so I'll right click here and do create c-sharp scripts and call it game manager and this is a quite a use of use quite common name for it so it changes don't be afraid of this little icon changing to a cogwheel here it's because it's so commonly used that unity decided I think to change the icon and let's drag that one on to the game-manager itself here and this is gonna be what's controlling what score you have or how many lives you've got how many balls you've got left that's the same life as life's it and if you go in and out of the menus for example so our game manager is gonna be controlling all of this and if you set the new high score for example so we need to start introducing a few new concepts here if I double click on this game manager script it's going to be quite empty in the beginning and the first thing we need to do I'm going to convert the ball and the pad now to prefabs like we did with a brick so let's define those here the game manager should be able to instantiate it's called when you create them from the from the game itself we want a game object here ball prefab and then we want the game object layer 3 5 so now these should be accessible here and what we did here with a paddle let's rename this one to play here maybe and let's drag this player prefab in to or make it a prefab and same with the ball there and then we can actually delete these from the scene no because we want the game manager to it to instantiate those when the game starts and I'll come back to that in a minute maybe the first thing we should do as well is to put up will create a few menu screens because that's most most games when you start them they'll have like a main menu where you press a play button and then you have some other maybe for example when you complete a level you want a separate screen or when the game is over game over game over [Music] you want a game over screen so we can create some UI panels here for that let's right click and do create egy panel and it goes a little bit white because it's got a background here and it automatically creates this canvas here that the UI uses and it throws this event system in as well those these are required by unity to run the e UI so you have to keep those and let's create the menu first so let's rename this first panel and call it menu and for this one we can actually just disable the white color there and let's right-click here and create two things the first thing we're going to do is create a UI button and let's just do button plate and then can expand this one and change the texts and safely let's make the font a lot bigger as well 18 maybe and we want the actually here is an important thing as well when you scale this now it'll keep the play button the same size maybe you want that or maybe you don't sometimes I find it easy to know exactly how big the things are on screen so on the canvas here quite often in my Ludum dare games and things like that I go to the constant pixel size I change to scale with screen size and that'll make it so that the button is always in the relation to them to the game size itself is always the same size so let's do that for now so that's all we're going to use for now there's a little play button here and we also want another panel so I'll right click here and do create UI panel this one we're going to call panel late and here we want to start putting some texts in so first of all I'll disable this image again we don't really need that one and up here we want to put some some text up here while you play so I right click here DUI and select text and this one we want to just put score here and then whatever score is gonna go in here and here's a you're gonna have to get used to this a little bit but up here with the rect transform you can control where this on the screen and it might take you a bit of use getting used to all these different coordinates and things but the most what I'll do for this one is I'll change the anchoring point here to top left because I wanted to go up here and it doesn't change straightaway because it automatically resets these values but I want to move this one to zero for example I'm zero now you see those still disappeared out of screen so we don't we want to bring this one back in somehow and we need to change the pivot point here to X zero and which is at the bottom oh sorry oh the far left of the object and then I won't change why here - one which is at the top of the object because now when I snap these back zero and zero that's what why it'll take some getting used to this probably but the anchoring points are at the top left the pivot point is to the left and the top of the object and when I've set it like this and it's alignment here is left alignment and at the top then I can keep position zero zero here and get it to be always locked into this little corner here will reduce the size a little bit to make it fit make it bold and let's bring it in ten units like this and we're gonna change this scoretext here in minutes so but it's just a bit of a placeholder so I can see and let's make it wider as well so it can fit a bigger score so it doesn't wrap at the end so let's create that let's name this one texts or and let's duplicate this one and do texts we want to keep track of how many balls we've got left hopefully to at least and balls balls and then we move this one in maybe to 310 so we've got enough space we have plenty of space for a good score e'er okay and then finally let's do duplicate that one and then do texts what more do we have the score at balls and let's put the level name up here as well or the level number and this one I'm going to change the alignment let's switch this one to top right and then go right aligned and then we change the pivot point to the right of the object and then if I reset this to minus 10 it'll bring it into the same lovely my kids are coming down hold on a minute oh I'm recording a tutorial it's a game tutorial at you and do you know what a breakout clonus no it's not the way you break up with your girlfriend but it's where you break you have a ball that bounces and you your bounds are back and you break bricks with it it's easy making tutorials when your kids are home highly recommended okay let's bring you back mm-hmm what was I even making let's see let's press play to see where I was so these were rotating oh yeah that's right we were creating these canvases okay let's bring it back so we've got our play button and we've got our play panel here and we're changing this text here levels we're gonna say levels orden just level level then we're going to change this one so we've got everything aligned good and proper I've got score balls and level and they're defined here so now we want to be able to change these from our script here so in order to do that I need to go back into the script which is our game manager and here let's start making some references will do access to the text score text and as you can see now it goes then allow me to put text it's because it's not defined so we need to include this one using control period here and they'll suggest to use Unity engine UI and that will bring that into out of arrow mode so we'll bring we need the score text we need the balls texts and the level texts now we can go into the game manager and drag these components of score texts balls texts and level text there so we can manipulate those let's go back in here and then we need to create references to our panels that we're going to create as well so we do and here we'll do a reference to the game object panel menu and we'll do one for a play panel let's prepare as well we'll make one for level completed so whenever you complete a level you want on a message go and level completed and then we want another one called public game objects panel game over let's create those panels straightaway as well so we've got our panel menu here drag that reference in panel play let's create another one here create UI panel let's call this one level completely disable this one just put a nice big text here saying level completed let's make this one a lot bigger so it fits a lot of texts let's Center it and do it really big 32 and whites level let's make it bold I'm even bigger maybe 50 their text completed and then let's duplicate this one and do panel game over game so we've got our panels now let's strike the references in it looks a bit funky because we need to only one of these are going to be enabled at a time so for example if these three are disabled then it's just the big game over text tape so that's pretty much it for the panels that we're going to need we can actually disable these now because we're gonna bring them into focus the game manager is going to control which ones visible at whichever time so now we need to create a simple state machine so we can tell what state the game is in so let's do a public and enumerable or enum a public enum here called States and let's define some different states that the game can be in let's make it a menu state and in its state to initialize the game when you start to play a play state we once we want to state where it's level completed another state by we're loading the next level or load level and a game over state plus should be sufficient and then we define a private variable here as well just called space and now we need to the game manager should be able to transition between these different states so let's create something called switch state here so it's a public that we can call from outside the script and it's going to return nothing that's why we've got void there and then let's call this one switch state and then state new state and whenever we shout at this public method here now we will be able to go from example from the menu into the init tour to play or level completed and things like that and to do this one we're gonna actually call a begin state and an end state and update loop will have here let's move this one below here then let's do a private method here called Beijing State and another one down here called end State this gives us room now so whenever we transition from mistake we want the switch state here we can say her we want to end the current state that we're in and then we want to begin the new State and this one we need to bring in as well new speeds so here again we define state new state so whenever we call switch state now here it'll end the current state they'll shout at this method here and then it will start the new state here with begin state but then we need to actually tell them what to do here so we'll do a switch statement here of the new state that we're gonna do and here we can do control period and then select add missing cases here and that'll save you from typing all of these manually so this is gonna be a state machine here we can put different things so whenever the game is transitioning into menu state for example we can light up different panels here we can close different panels update things in it for example we can reset the score and things like that so we're gonna make good use out of this let's copy this as well because we want to be able to put that in the end state as well but here we want to actually and the current state that were in so we accept this one to state here and in the same way we can whenever this script now is calling n state we can for example ending the state game over we need to hide the game over text again so this give us quite an easy access to do some different core things for the game and then I'm gonna put this one here in update as well then basically whatever code I put here stay tuned then during the update loop that runs all the time we can put some logic in here during the different game states as well if we want okay so the first thing we're gonna do is when we enter the here in the begin state for the menu then we can actually show the menu screen so we've got a definition to that here called panel menu and then we do set active true and then in the same way when we exit the menu here when the end state calls the menu then we set this one to false and then to demonstrate this the first state that we're gonna go into in the game here is called let's do switch state state menu so when the game starts now when the game manager is initialized then it's gonna go into this menu state and hopefully show the menu screen here so when I press play you can see that it lit up this panel menu here this is exactly what we wanted and if we were to press play and the configure this one now we wanted to hide the panel and start the show the play panel instead so we can do this as well let's say when you press play here we're gonna switch it into the init States so we do panel plate set active true and the reason why I've got an init status because I want to do a few things before we enter the play loop as well I want to have a little in between state to any let initialize everything we reset the score and things like that and then also we don't really want to hide the play panel when we exit the place or the init state we need to keep this play panel active all the time so I'm not gonna put the hide command here we want to hide this one maybe a good point would be when you leave the game over States that's where we want to I'd the men sorry the that's where we want to hide this panel called play so whenever game over is set we press back we get back to the menu that's when we hide this play panel then and nothing happens now when we press play we're not going to be able to play the game because we haven't told this button what to do and a quick way to do this one is to define a public method here called lay clicked it's gonna be void it doesn't need to return anything then here we do a switch state to state dot in it for initializing the game and since we've put this one as a public method now if we look at this button here that play button then we can define here on click we can define an event so I click click the little plus key here and then we do here I drag this game manager game object onto here and then this game manager C play clicked is exposed now so if I go here now press play then it transition into this so you can see that the score let's do this one again so now it's the play menu or the menu panel and press play and it'll go in and show this play panel instead and then we can play the game here and whenever game over is finished we'll transition back to the menu ok and then the same way here when the level is completed we want to set the level completed here to true panel level sleep and when game over it appears we want to show the game over screen or yep we want to show the game over screen that's right and whenever we leave the level completed here we want to hide this screen here so we set this game object to false or subjective false in the same way when we leave the game over screen here we want to set the game over panel to false as well I think that pretty much covers that we're only gonna use four panels in this game to go from menu to play to level completed a few times until you reach the game over and then I'll go back to the menu so that pretty much covers the menu transitions okay next thing we do is we're going to define some variables let's say that you start with three balls or three lives and this court and a score as well obviously so let's create some public properties here that we can use for that so here I'm gonna do there's a shortcut here you can do full in Swedish it's helpful it means that you are full you don't want any more food but this is a shortcut so if you type of prop full and then press the tab twice it defines one of these properties so these are gonna be hungry let's call this one sport or underscore score and let's press f2 here to rename this one to score and I'll do the same thing for level and balls as well or prop full thumb tab let's press f2 here and rename this one to level and press f2 here the level and then one more time full int o sub tab f2 terrain it name this one two balls nope I wanted to rename it to balls and this one to the public wanted balls as well so now we've defined this and the reason I do these properties and studies because whenever you set the the value here from outside from the different script then we want to update texts as well that we've stored a reference to here so for example when you score a score here then not only do we want to set this internal variable here or the field score we want to increase that one but we also want to update this score text that we have it so if I access the text property in that one and then I set score we want to still keep the label for now let's do a little shortcut there and then plus whatever the score is at this time so instead of updating the text every time every update loop this will update the score text only when it needs to be updated only when this property is set here they will update the on-screen UI elements so that saves a bit of performance not that we're gonna have any performance issues with this game so simple but pretty good pattern to use so now let's go into the brick here and remember that we had when we dis crease the hits here then we want to increase increase the points and let's only score points if we actually break the objects not only to hit it now we need to reach this public property of the game object to the game manager keeps a count of the score now so we need to access the game manager somehow and there's a bit of a shortcut you can do for that one let's make this accessible from from elsewhere in our game so we'll make a bit of a shortcut to a singleton and you're gonna hear the first thing you're gonna hear is don't use Singleton's but I say go for it if you're gonna make a simple game like this don't make it too difficult on yourself just make it super simple to access it you can find some fancier patterns later on so I'm going to define a public static game manager reference here and let's call this one instance and it's gonna be a property so we can just get it and let's do a private set so it can only be set from inside this game or for inside of this class itself and in start here we're gonna keep it super simple so we'll just do instance equals this and since this one is a static variable now then we can from bricks here we can just simply access this one by going game manager dot instance score posts equals points which is the variable that we kept up here so when this brick is destroyed now the points counter of this game manager will increase by the number of points that we've defined for this brick and if I press play now nothing's gonna happen because we don't have a ball yet so we need to make sure we instantiate those as well and that's something we want to do in this in here so in the game manager whenever we initialize the game we can do this is really handy now because begins state of in it here not only do we show the play panel we also want to set the score to zero and since we're using the the property here then not only will it set the score valuable to zero but remember here it goes into this one and it also updates our UI element automatically when we set this level we want to set to zero and balls let's set it to three so you start with three balls and now we need to instantiate and instantiate is the term that you use or the command to bring a prefab into your scene so we can type instantiate and start it and we're going to instantly instantiate the paddle or the player prefab and then we're gonna so we don't want to switch on the ball just yet let's also switch the state now to state what we need to load a level now as well so switch state state load level and we don't have anything here just yet so we need to define that as well and to do this we need to start creating a prefab of this level as well so let's scroll this up a bit and prefab let's just drag this one in here this will be level one and that's now defined as a prefab so we can delete this from the scene because it is exists here now as a prefab so then we also need to define our levels here and we can do that by defining just a simple array here so back into the game manager script we'll do public game object and make that on into an array that's what these indicate the little brackets levels and then when I click on this one now you can see that there's a new expandable array here and I can drag level one in here and for the sake of it let's create the level two as well so I press control D here to duplicate it to level two and then maybe we just duplicate this one control D and make red bricks here to it in to visualize that we're on a different level and maybe we also duplicate this brick let's rename this one to brick one this one can be called brick - and then we swap the material here - brick - and let's increase the points for these one to two hundred for example and on level one we can hit them let's keep the hits - one just so you have to hit the bricks just once on level one and you have to hit the bricks twice on level two okay so we've done a lot of things now without actually seen anything on the screen so I hope you're patient enough to stick with me and I hope you follow along if you made games before this is gonna be a piece of cake and if you haven't made any games and you're getting into unity maybe it's tricky to follow but you can always rewind and go back and forth hopefully you can find some parts of this what that will be useful for whenever you make a game so I think a lot of tutorials don't go through the game states and that's really a important thing to have in your game so you can transition between the different states that's what makes it into a traditionally playable game that you can restart so you don't have to restart the whole executable over and over again so okay we now need to instantiate so when I press play now let's see what happens I press play and we get an error here and that's because the player prefab is not defined so I click here and these two objects are not defined so let's just drag the ball here into the ball prefab and then let's drag the player prefab let's press play again and we hit the play button and there's our instantiated paddle the reason why it ends up in the right place it's because it doesn't really matter where we instantiate it because the this script if you remember the player script here forces the position all the time because normally you'd need to when you instantiate an object you need to tell it where to go but since this script is updating where it needs to be based on the mouse input we don't really need to worry about that one and then for the ball we need to instantiate the ball as well and the ball we're actually gonna do keep a track of so let's put a private variable here as well let's do a reference to a game object that we call wall what current ball we can call it so the ball that we're currently playing with and then we scroll down here to the update loop and in this play updates let's put some logic in here and let's see so if if the current ball equals nil no that means that there is no current ball it's not defined yet and or it likes to be done so if there is no ball yet then we need to see if the player has any balls left have you got any balls hope so if you don't there might be perfectly valid reasons for why I don't but that's okay too so if balls which is the public property that we've got is greater than zero that means that we've got balls then we set a reference or actually we I can do this so instantiate and we instantiate the ball instantiate ball prefab and then we actually want to set this to the current ball will set this reference here because now when we instantiate the ball if there is no ball during the play state so we haven't defined a current ball that means when you come into the game it's no ball there then we check if you have balls then you need to instantiate the ball and we get a reference back into to the current ball so when it hits as long as the golf ball is in play now and it comes this loops every frame remember this play state and as long as you have a ball here and defined then it won't instantiate the new one and then we can do as well if if you don't have any balls then we need to switch then it's game over if you've got no balls it's game over so we switch the state to game over and now when we go back in here and we press play we still have no balls it sucks you've got no balls how many times am I going to try to make that boring joke I don't know I keep doing it okay so we have to figure out now why isn't it and it's because we're we've told the script here and begin state to go to the in it state which it does and then we've said for it to go to the load level state which it does but then it doesn't know what to do at all so in the low-level state we need to do something remember that we defined an array of levels 1 & 2 I hope we put the second level in there as well no let's do that no so I'll drag the reference to level 2 here let's go back here then we can do similar here so when we come to low level we need to have a reference to current level here so and instead of having a reference to a game object as well current level because we're instantiating the levels as objects so here in load level now we go if level is greater or equal to level stop length so that's the length of the array then we need to switch States to game over that means that you reach the last level and it doesn't have any another level to load so we're going to switch the state to game over but if it isn't that means there is another level to load so we can do current level instantiate the level so we input instantiate so Kate we instantiate and from the levels collection the array of levels we do hard bracket and then we put the property level here which is the current level that we're at so this will instantiate the level and put it into a reference into current level so we know which level we're on let's try this out so go here and we'll press play plate and we've got the level instantiate it now as well but still no ball and we need to do one more thing here and that is to transition so when we've loaded the level here then we need to go into the next state okay and to go to the next state when we load a new one here we switch level or switch state to state lay again sometimes you want to stay like for example the level completed message you want to stay on for maybe 2 seconds and then transition so it'll be useful to have a delayed their capability here anyway so we can do this by in switch state here we introduce a new variable here a float called delay and by default that will be 0 and then here we're gonna break this out into a new one and this one is gonna be called we're gonna call this as a KO routine so it's gonna be an I enumerator and then call it switch delay and then we bring the same variables here at the state's new state and that floats delay and in this one now we can do something called yield return new wait for seconds delay and this will now come into this core routine and wait for the delay before it continues and then we do the end state and then we do the actually we'll just move this logic here control X control V oh yeah one thing I've forgot to set I see now is in this state I'd forgotten to set it before we need to set this the state to the new state so that was important that's probably why it wasn't working properly before so we've got we've set the the current state here now to the new state and then we begin the new states and in here now we just need to change and say start co-routine start co-routine switch delay new states coma delay and let's also introduce so we can know if it's currently switching level or not so let's create a bool here which is going to be a true or false variable it's switching state and then that one we set here straight away when we enter the core team is switch for state troop and then here we have is switching States force and let's try this out not late and there is our ball note and it starts up in the level we probably don't want to do that so let's change we can change it on the prefab here that's why it's a zero pardon if you can hear my kids in the background there bit noisy plate there we go okay now it's time that we should consider changing the pad here because we can't really affect with a flat pad like this the ball is always gonna have their in angles gonna be the out angle so let's change that so we have a little concave is it curved convex convex like a a bit of a bent paddle instead so we can control if it should go somewhere else so this is the first time we're gonna replace this primitive box here with something that we create in blender so I'm starting blender 2.8 here's free software if you haven't used it before you're probably heard of it well it's really good free to download just go to blender or can download it and here I'm gonna this is the default scene that you get so I'm going to delete these to the camera and the light we don't need that and then I press 1 on the keypad to get the front view this default box and then I press tab to get into this vertex editing mode i press scale and then press x + 2 and this will make it wider i've got loads of videos on my channel about modeling in blender so if you want to learn that just have a look through the channel history and hopefully that can give you an insight to how this works a bit better but for now i'll also press s to scale it and then zed to scale it's on the z axis and then do 0.5 and then I'm gonna hit the ctrl R and then scroll up a few times so I get these bendy like the possibility depending on scroll it all the way up to there maybe and then I'm gonna look from the side here author shift and then I'm gonna end it enable proportional editing with sphere press G to grab it deserve to force it on the z-axis and I scroll this one up and this will allow me now to create a curved paddle instead so we can control the way the ball should bounce let's make it like this maybe press a to select this shift space and G to bring this down and then let's make some press ctrl R and just to make this a little bit funky looking as well so I'll uncheck the proportional editing scale control or scale it down again like this control our scroll it up with the mouse wheel scale this down make some interesting shapes here maybe we can do some features here as well not sure what this could be able to extrude this down a bit s to scale it's okay something like that and then I'm gonna make texture this as well so it's not just a plain white color so I'll go into this shading tab and then I'll go to this site called lost bet calm slush palette - lists and here you can download some free palettes that people are uploaded and let's just get a colorful one maybe this one so I'll hit the download button and download a PNG file here and then we save this one save image as grayed out palettes let's go to the folder and drag this one into this scene here and then let's bring the color into the base color here then I also need to go to you the editing tab here and switch this one to texture up here in the corner and then hits 8 hey you again here scroll this down a lot so we can pick some colors here let's make the pad maybe this blue color here and then I'll shift and alt to select these press G maybe I'll get a yellow color in there ctrl + actually let's change this one into some pink and then maybe I'll change this whatever these are as well plus into green let's make those yellow as well keep it a bit consistent at least okay that'll do for our puds so let's name it as well double click here and select paddle i'll put it and then let's also create a new brick since we're in here as well so I'll press I'll hide this one for now click on this little eye icon to shift a insert to cube here press tab to go into edit mode scale and then we want to reduce it to half size scale side to 0.5 and then the depth we want to scale Y is 0.25 and then we want to scale it down a little bit more so we get some gaps between the remember from the first one and then control B to bevel these edges a bit so let's make it simple as that and let's call this one brick there we go we've got our two objects now let's select them both and go to file export FBX and here it's important I choose to select the objects that are on export and here's the important one you want to do apply scalings to FBX units scale and we want to protect this experimental apply transform so the orientation is correct when we bring it into unity simple breakout clone assets let's create a folder here called meshes and then we also keep the name here is already typed mesh this FBX meshes stop FBX and export and now it will bring it this new folder here so we can double click on the brick prefab here that we had and instead of using this brick here which is the here at the cube we can bring in another one called let's see the meshes we expand here first go back into brick one and then we pull this brick into there and then we need to change the this to scale one one one again because we've already got the correct scale in the prefab itself so that's the let's do the same here for this brick as well so I'll pull this mesh into there into the mesh filter and then reset that size because we've already got this scaling in there but we still have to change the puddle so let's go into this player prefab and do the same here instead of this cube one we bring this paddle mesh in to the mesh filter here and then we need to reset this scale as well and here we also want to create a new texture so let's create a new texture here called puddle and then we need to import the same material that we had created before so I wherever you downloaded that palette file into I'm gonna drag this straight into my materials folder here and then we select this puddle and bring this texture into there and then finally we also need to assign this puddle material here to the pad and then we get the colors that we had in our editor some look what it looks like when we plate there's our mr. plate and now you can see that I can control hmm okay something funny happened there okay I know why it is the reason why that happened is because on the brick here now we have a box Collider that's not correct let's just remove the duct component and add it again and that should fit it to the size of the brick and let's do the same for brick number two here double-click on the prefab right click remove components add and then add the Box Collider again so now we should have a proper Collider here there we go and now I can use the pad here too and I missed this and that's okay as well and we've done the same mistake on the paddle paddle itself so on the player here you can see that I have a box Collider so we'll take that one away we can't really use a box Collider now because we've got a shape to this panel so I'm gonna add another Collider and it's called a mesh Collider and this one I can pick the mesh that we want to have which is going to be the paddle it's already defaulted to that one and I'm gonna click convex here and that will basically simplify the mesh and have this round shape to it it doesn't follow the bottom here but that's okay we don't use this one for the collision so now I can press play and we should be able to control yeah so the ball bounce is based on the normal perpendicular hit point we can control if we want the ball to go upwards we'll just hit it on the left side of the paddle here and if you want it to go to the side you just hit it on the side so that's a simple way to control the way the bounce should work on this bowl and we've got you can see as well now up in our left corner we've got the score counter so that seems to work so every time we hit a brick remember that the script this the brick script calls the game manager and sets the score property and that automatically updates this information up here okay so the next thing we need to do let's see when I miss a ball now nothing happens it'll just disappear out of view and we need to fix that so let's make a check here on the ball I'll double click on the ball script here so we need to figure out now when it's out of view and to do that I'll end another one here called it's a reference to something called the renderer that we used if you remember when we did that flashing brick effect it's the same type of thing here so renderer renderer and then we get that component here remember equals component renderer so I'm not going to change the material for this ball here but in the fixed update here I can do this so if not renderer is visible and that's another handy thing that unity provides the render to the renderer knows if it's visible or not and if the renderer isn't visible then we need to destroy this game object so we destroy the game object but before we destroy it we also need to tell the game manager and remember how we access that we can there's a static variable there so I did game manager dot instance and then I do balls - - so you just basically when the ball disappears it'll deduct one ball from your amount of balls that you have that'll suck by the way you don't want to you want to keep at least two remember and then you destroy the game object so and also if you remember now in the game manager not the game manager the game manager the the balls count here you'll detect that the ball is nil suddenly because it's been destroyed so it should instantly instantiate a new ball if everything is working as planned so we press play we have this one it works and let's miss this one out there and we straight away we get a new ball that's great but we probably want to have a little delay there so let's introduce that in a second okay we don't seem to be updating the text here that we wanted to so let's have a look why that is so when the ball is lost we go to game manager instance and we reduce balls by one and then we look at this property that we created here and that's because we haven't actually made a change here as well so when we set balls then we need to do ball texts text property equals balls bolts in the number of bolts that we have now and then let's prepare already and do this for the level as well so when we change the level and the level text here we've set to level and then on this court level now we're starting on level zero and we've got three balls and then when I lose life it automatically drops down to two balls and it's instantiates a new one so let's create a little delay first of all maybe we want to send the ball upwards straight so you don't get surprised by a ball that's coming down so we can go into this ball script and then just change this start one to go up speed and let's take this opportunity as well to put a little delay here so we'll move this line actually control X to remove that on create the new method here called launch and then we do invoke launch and then we can create with the invoke then we can actually set the delay of half a second or 0.5 here and now there should be a delay here so it instantiates it and then it waits half a second before it launches if I lose that ball now it'll instantiate a new one and then you'll wait half a second and then send it up so you get a little chance to capture it on the way down okay that's it and now we need to check that we can actually transition to the next level when this one is completed we haven't actually put in any check now so if we make a really simple level here let's call it level zero control D to copy this one rename this one to level zero double click on it to edit it and let's just keep one break in here so I'll delete all of these bricks and then just keep this one at the zero zero zero so that one would be hit straight away by the ball and then we go into here into our game manager and then we insert level zero here before okay let's rearrange it a little bit so zero and level two and now that we actually deleted it's straight away so let's move the brick up a little bit so we can actually see again hit so level one or level zero it's a bit difficult to see where this if you want to edit the level and still see the frame here there's a trick you can do you can bring this whole level into the hierarchy and I'll give you the frame so you can see it and then you can hold the control key to move this one to wherever you want that'll be easier to position it this way and when I've got it here for example up let's put it up at the top here and then I can drag this level here and overwrite level zero here let's go in and have a look at that had an effect brick is now at position 17 is that here so if I press play let's bring we can delete it from the C now because we've overwritten this prefab I press play and now the brick will be up on the top and as you can see now we deleted that brick and the game we need to transition to the next level now so we need to put in a check now to count if there are any level blocks left and if it isn't we need to go to the next level okay to transition to the next level then let's bring our game manager up and scroll down to the let's go game manager and then let's go down to the update loop here of the game manager and in the play state here and we need to check if current level isn't no in so in effect if we have a level loaded because we set that one when we load the level we set that reference to current level so if it's not nil and current level transform child count equals zero so that transform child count the reference to the game object it's got something called a transform and I should briefly say what that is basically it's this component that every game object has it's got a transform that states a position rotation and a scale and it also knows of any sub objects or sub transforms that it has under it so if I access the children of this transform then I can count how many there are for example or I can do with them to do this now we can actually count the number of children that this if we bring this level in here we can see that the bricks exist tears children so and we can count those and when that counter reaches to zero we can delete or we can determine that the level is completed so if we go back into here now we say that the current levels transform child count equals zero and then also make sure that we're not switching any state at the moment so it's switching states or it's not switching a state then we need to switch state to state level completed and let's see what happens wait and then there we hit the block and level is completed so it did transition in the state but we probably don't want to keep this ball rolling and we don't want to stay in this level completed forever so let's fix that as well and we can control that by going back into our state script here and when we begin the level completed stage here and the only thing we're doing at the moment is doing this panel level completed we're setting that to active and that's the reason why we saw that text saying level completed here we can do some more things let's do before we show that panel we can destroy the current ball and it's not a problem because we're not reducing the counter of the boss so even though we're destroying the ball you don't lose a life because we're not deducting any lives from the game manager and then we also destroy the current level because even though it's got no blocks left we want to remove the level the empty level that we've got left and then that'll effectively put this into an all state as well and do we need to do something else we show this panel then we do an increment as well so the level public property that we've got we increase that one by one and then we switch to the lower level States which states to state load level so quick recap Oh capitalist so quick recap here when you enter the level completed begin state here it'll destroy the current bowl well you don't lose a life because we're not deducting the ball count you just we destroy the current level because it's an empty just a game of the check that we don't need anymore and it needs to be set back to nil we increase the level by one and then we switch to the load level States actually we need to switch to list state after we do the panel completely so we do that the last thing in the States so we don't we make sure that this panel is set to active first and then we switch to load level and we can we prepare there as well actually to do a delay so we'll put a two-second delay remember that we changed this so we introduced the ability to delay the state switch and that's because we want to be able to show the level completed meshes message for a little while so two seconds for example is it maybe a good time so if we press play now we'll see that the press play the ball hits level complete that the ball disappears and then we come into the next level and you can see up in the top right corner here now that the level counter has gone from zero to one so if we were to complete this level now then we'll go to level two we can also check here without having to play all the levels then we can go in here and change this as well so if we go back into the game manager and then we just set this down to not thirteen one level and we press play and we can ensure now that if we hit there it says level completed but then it says game over because we've got no more levels to play and here it's gonna stick here forever now so I'm gonna stop here now it's gonna stay here forever and we need to get out of this game over loop as well that we've ended up in and to do that we can go back into the game manager usually it's the game manager that keeps track of all of these type of the things the events switching and here we can do a simple thing we want the player to be able to control how when to get out of the game over loop so in the update loop here here that we are in game over we can do if inputs any key down where's the any key where's the any key to start press any key where is the any key I see tasks Kotaro and pickup there doesn't seem to be any any key well if any key is pressed down that also includes mouse clicks by the way it's prepared for that so then we want to switch state to state menu let's go back to the menu let's check if this works we press play late hit it level complete it and then we go game over not the greatest ending and now we'll stay here until I either press a key on the keyboard or click a mouse button and it'll bring me back into the menu over here we go got a problem now because I don't see a mouse cursor anymore and we need to bring that one back so let's if you remember we set a little thing in the player prefab here or in the player script we'd set the cursor visible so let's take away that that shouldn't really be a responsibility of the player or the pad to do that so let's press control X to cut that one out and let's go of control s to save it and then I go into the game manager and here we want to have when you enter the init state here when you start playing the game in it here then we press control V up here so we do we hide the cursor here instead when we enter the init state which is when you press the play button you go here and then we want to show when you get into the menu so begin state of the menu we actually want to show that so let's set the visibility of the cursor to true here that should fix this problem that we had so we press play again test it plate hit it game over only a hundred points lousy game and then I click and the mouse cursor appears again here so let's finish off as well by just keeping track of the high score as well and put a little label here maybe as well and to do the high score we go back into our game manager script and when we enter the begin state here of the game over here we do if for its greater than and here's something that's really hard as well it's called player pref play your preferences or play your pref and that's unity will store this information on Windows for example it's in the registry and on other platforms hold on we're gonna tell my kids to be a bit more quiet okay not the easiest thing to record tutorials while the kids are up but I have to do it because otherwise I have no time to do where was I okay so there's a really handy thing that unity provides and that's called player prefs and unity can store information in Windows it stores it in the registry and other platforms it finds the appropriate place to store persistent information so here we can set or actually we do if it's so if player profs get int which is called get an integer which is gonna be a whole number let's just call that on high score this is a key that they uses to store the information in the registry so if the score is greater than whatever is stored here then we need to update it so then we set the player prefs here second high score and then we set it comma to whatever the score was that we just got and you don't really have to initialize these this if it's no information there it'll be a zero basically so the first time you play the game it'll figure out that your score is higher and then you'll figure it out that it's higher than zero and then you'll set this reference if I could spell score thank you thanks whoever pointed that out but then we should also display this somewhere so let's put that on the game over screen and maybe on the menu screen as well so let's do here public or let's just keep it at the main menu I guess text high score text so that's a reference there and then on the main menu now let's put it in here in again let's see we go to the canvas and let's show the menu screen here let's right-click on the panel here and create a new UI item texts and call it text high-score all right sport score let's make it wider so if it's short a bit higher as well maybe 50 and then Center it change it to white bring it down a bit Y position may be there and bold let's put a title in here as well so ctrl D to duplicate this one just call it title let's bring that one up so we will bring it to 60 and then puts simple great outs flown and let's bring the size up to 60 but we need to make it higher that's why it's disappearing and wider and smaller 30 okay and finally we also need to do this that in the game manager here we need to drag the reference to high score here that we created so high score text there but it's still not updating this text and to fix that we go back into our game manager here and then in the menu States when we begin the menu state let's update the high score there so let's put high score texts dot text equals high score and then plus player prefs and let's grab it straight from the persistent storage here get int so getting the integer here called high score and let's test this out so we need to actually let's reinstate the next level here let's bring it up to lock 3 again and put level 1 and 2 back let's press play and as you can see now it shows high score is zero because we have got no value in there just yet we play it hit one brick level completed and now we've got the other levels back so we'll hit or bring the score up to 300 and then let's lose our lives here now and the final ball and we get game over all good when I click now you can see that it's updated the high score to whatever score that I just got here when we press play again here we've got a problem as well we're not resetting the level when we get out of the game over state when we go into the inert state here as well then we need to make sure that it destroys any previous level so we can do current level if current level is not nil no then we destroy it just croix current level and that should hopefully fix this problem so we press play play again hit one ball level completed here let's lose our lives again and the final oh no not the final ball we have to wait for this one I'm gonna set the new high score three thousand nine hundred four thousand five thousand four hundred let's just miss this ball out and let's lose our final life high score game over new high score and this stays which is fine during the menu stage but then when we press play again now it'll destroy the previous level and everything's back to normal and then let's just destroy we let's get rid of that test level that we put there level zero we can delete that one and then go back into the game manager right click here and delete the array element twice you have to do that one to make it disappear and then we've got our levels in there again so now we've got a functioning high score here if you want to reset that high score you can you you can just delete there's something like this that you can do and for example if you were to you could just here in the starch you could put delete hey play your prefs lair prefs and then here you can either just delete the specific key high-score here and then you execute the game once and they'll delete that or you can delete all the keys if you want as well but then if you remove this one again then if you only run it once it'll delete that key and reset it back to zero okay so pretty much everything is functioning now the very last thing I'm gonna do to this game now is to put a little scrolling star field as a background just to make it a slightly prettier to look up maybe it's not the prettiest team in the world but it's here we're here to learn from it so here I'll create a new empty game object called star field reset the position to zero zero zero and here I'm gonna add a component called particle system and it'll start spraying some particles here and we can change this a little bit so let's change the shape here first of all to a box shape and then change the width of its scale here to 40 you know 50 60 70 let's do 80 so it goes a little bit wider and then I need to rotate this one as well so let's rotate it around the x-axis down 90 degrees and then I'll move it up to the top here and we don't really want these being pink first of all we're gonna move it in so it's not on the same space as you play we don't want it to interfere like with the level objects so let's put it behind here and let's change the render here first of all we make them live longer so the life time here let's put that to 20 and let's change them to stars now so I changed from this material here let's just use the default particle here and then we can make them smaller up here so star size we do random between two constants 0.1 0.3 maybe and then we change the speed of them so we do also a start speed is random between two constants maybe two one and three and let's start by putting the lowest number and see that they don't really reach all the way and we need to increase their lifetime to make sure that they can live through the duration here of the game and then now then we know that the slowest star can reach all the way to here then we can increase it to maybe three there as well so you get the different speeds of the different stars here and then we can see that the particle here is capping out at about 500 so we can actually increase the emission here to 20 and let's increase the maximum particles to 5000 as well just in case so we don't loose any halfway down it's about we can actually go up to maybe 40 particles or yeah something like that and that's a really simple primitive star field that we can have and let's try it while we play no okay and it starts like this maybe we want to have it already warmed up so we can take this pre-warm and that'll basically spawn the whole star field at the same time and now we press play we've got the first level scorer is working their balls is working and level zero you could add one if you want to have it start at level one to that texts if you wanted we've got their balls bouncing and the pad working with our little bevel so you can whoop either lose a life like this or you can have it bounce left and right we've got the little impact effect here as well actually we've lost that one can you see oh yeah and we lost the impact effect and the reason for that is because we're the bricks are getting destroyed on the first hit so you don't really have time to see it on this one but on the next level then you'll be able to see that when you have multiple hits we can actually update this level a little bit so I'll double click on level one here let's swap some of these bricks out in the center for example so let's take this is sent to break away can press this to get the center view here let's go into orthographic here by clicking on the back there as well let's take those two Center blocks away are we still okay here let's I'll look let's replace those just with a the other brick type here zero zero zero control control deep to duplicate that one you can use this as a level of editor and just use the control key when you position these and make them position perfectly along the grid just when you start make sure you're on a whole number so then we can test this one as well to make sure that you get that double hit flashing effect yeah so those red bricks are gonna be a little bit more difficult to destroy and then now you can just go crazy you just double click or press ctrl D to duplicate the level again go to level three and remember that you if you want to see this when you edit them you can drag it into the scene here so you can see the actual frame maybe it'll be easier to edit if you click here you get orthographic view and you click on this Z icon here then you get the perfect front view as well and now you can just block select like this okay and to edit this prefab now this is a big prefab that's instantiated so I can't really block select any block items here we can fix that temporarily by right-clicking here and do an unpacked prefab now I can actually select the individual ones instead we can move this light out of the way as well it's a directional light so it doesn't really matter where in the scene it is let's move that out of the way this stuff that you see here is from the UI that's in your scene like this so you can just try to ignore it if you come and we're editing this level three now so we could just block select a whole bunch of blocks here and here maybe and then we can easily duplicate this one to brick type three let's make this one a bit gold make make it even more shiny and we can do here on brick we need to duplicate this prefab as well and then change the material to the new brick material brick three and then we can increase the hits here to three hits and give them four hundred points for those and now we can use this BRIC type here as well and bring it into the scene remember that it needs to start on a whole number so the snapping will work correctly control key and then now you can just multi select with the box like this start making some funky levels so basically this like a built-in level editor now that you can just use unity to create as many levels as you want and here we can start doing some things as well we don't have to necessarily change the rotator thing on the prefab we can go into these objects here let's select these and change the rotator here so on the rotator here let's do a row on the x-axis but then let's spin them around they're the distance or the depth axis so on this side we'll do 45 degrees per second and on this side we'll do - 45 take this one away as well and now when we go into this level actually let's update this level important thing here I noticed that when I dragged the bricks in they ended up outside of this level prefab as you can see how here if I scroll up they're not under this level if I collapse that one so collapse the level multi select the ones that ended off the outside of the level and just drag them into this level no get them there properly and then let's update level three now by clicking on level three here in the hierarchy and dragging it down and overwriting the level three prefab and then it'll say replace anyway I'll warn ya because it looks like it's something else but let's replace that one oh we had another brick that we missed let's throw that one in okay I've unpacked the prefab again drag it in drag level three into there and rip place to test this level as well instead of having to play through the levels all the time you could just go into the game manager and put level three here first and then now when you press play it'll go straight into level three okay and the reason why it's one there is because we can delete it from the scene now we've stored it as a prefab here so be careful though if you delete accidentally and you didn't save it as a prefab you might be out of luck for a while press play play okay we're missing those bricks now so let's see why that is could it be that there yep there's ed coordinate was way off so let's mark that and brings em to zero and then get out of it press play and now they should be visible and as you can see we changed their rotation on these press pause maximize we changed their rotation type of those and you could put loads of things in there you could have that the blocks rotates actually why is it not hiding the cursor there we go it's cuz I resized it during a pause moment so here you have the gold bricks now we'll take three hits the blue ones we'll take one hits and the red bricks we'll take two hits and you can start putting things on your brick components maybe implement so they drop some power-ups and things like that I won't cover that in this tutorial it's probably way long enough already but just hope this gave you a bit of a start it pretty much ties together the whole game loop so not only does it make a simple little gameplay but it also ties it together hopefully with how you can transition between menu panels and how you can have a keep track of the state of the game so remember we're going from a menu we initialize the game we have a game loop we can switch to a new level and we can go to a game over screen and then back to the menu and it ties everything together it resets the counter for example it updates the UI elements it keeps a little simple high score and things like that and you can just build on from here so I'm gonna end this tutorial here probably a long one but someone had a comment a while back that they preferred some of the longer tutorials so I understand fully if it's too tedious to watch through a full full one like this but for those of you that do do enjoy these and then find use for it great I'll be happy to probably create some more tutorials like this thanks a lot for watching guys hope you enjoyed this video I hope you found some useful tips about how to make your own game in unity and that some of the parts may be just the simple mechanics of having a ball bouncing some simple physics or even the game state machine or the how the UI I hope that couldn't help you out a little bit on your road to making your own games so I have a lot of fun thumbs up if you like the video subscribe if you want to see some more and take care have a great week and I'll see you soon again bye for now normally this time of year it's freezing cold here we usually have about this much snow and it's like at least well below zero maybe minus 10 5 10 20 even at times but now thanks to the climate changing a bit it's quite nice today yeah like you [Music]
Info
Channel: Imphenzia
Views: 307,194
Rating: undefined out of 5
Keywords: unity3d tutorial, make a game, make a complete game, breakout, arkanoid, making a game in unity, how to make a game in unity, learn unity, state machine, game states, ui panels, highscore, prefabs, fixedupdate, persistent highscore, UI buttons, invoke delay function, OnCollisionEnter, score counter, loading a level, gamemanager, game manager, mouse movement, ScreenToWorldPoint, what is a prefab, amiga style hit effect, white flash, updating score, complete game in unity, unity3d
Id: gCqOnchV4V0
Channel Id: undefined
Length: 113min 28sec (6808 seconds)
Published: Mon Jan 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.