How to make a Dodge The Blocks game (Livestream) - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thanks for tuning in at brakus hello everyone and welcome to this video this is a recording of the first ever brackets live stream that we did yesterday and it was a blast I had so much fun you guys were awesome and we basically created this a dodgy falling back style game so the recording for that is coming up in just a second I just wanted to say that this is an extra video on top of the other videos that will come out this week of course so don't worry about that and also if you want to download the game and play around with it yourself I have a link to the source code in the description down below I uploaded it to github so it that's fully available so without further ado enjoy the stream and I will see you there okay so I'm really glad everything uh everything is working and if you haven't seen this on on youtube what we are going to be making today is one of those I dodge the block kind of games so I already have the game set up here so we can see what we will be working towards and this is mostly for the beginner to intermediate um we are going to be going on fairly slowly forward so I will be able to explain most of what we're doing so but it's going to be a lot of fun I can just show the game here while we're waiting for it for people to join the stream so this is what we're going to be making you can see that I have a player here with some amazing art I did it myself it's true uh and if we hit play here we're able to move around the scene and some blocks will randomly spawn in and one really cool feature that I like is this kind of slow-motion a death can so when we when we get hit by something you can see that it slows down time until it loads a new frame and we have some physics going on there so um that's kind of what we are going to be working towards in this example I also have a feature implemented where we um kind of make the blocks fall faster and faster as time increases so if we have time I will show how to do that as well cool and also of course ask all the questions you want in the comments I am probably bad at paying attention to them while talking but I will try and answer some of them along the way or at least we'll do some Q&A towards the end um so yeah um so I think a lot of people are in here already so I think that we should just go ahead and get started and yes to the first question I did use time scale to make the slow motion but whenever you adjust the time scale you have to adjust time fix Delta time as well because we are doing some physics movement if you don't do that it won't be smooth but we'll get into that in a second so another sip of water here and um another question yes um I will try and upload this to youtube so if twitch stores it probably and I'm able to put it off their servers I will definitely upload it um but we'll see about that I don't know about the Technic technicalities yet I haven't set up to be able to record this while streaming so I will have to pull it from twitch so if there's any issues I might not do that so okay so let's get started so what I want to do here is just go ahead and create a new project and I'm going to call this one the our blog dodge game and it's going to be a 2d project and I'm going to store it in my projects folder I have an entire projects folder set up to this and a lot of bad stuff in there and I'm going to hit actually that's just disable unity analytics and I'm going to hit create project by the way if you hear some stumbling around in the background that that's just my cats they are being kind of crazy today so they might interrupt the stream at some point so you can see that I'm here unity and what we want to do is begin by creating the most essential parts of the game which is of course the player and his movement so let's or hers it could be her so let's go ahead and create some kind of player and whenever it comes to doing a 2-d things in unity let's always the default sprite so if we right-click in the are higher Kingo create 2d objects right we will see here that it doesn't appear but if we go over here to stride and select one of the default ones here such as the UI sprite we will see a tiny square with rounded edges this doesn't look too pleasing so what I am going to do is to quickly jump into Photoshop and really you can just Google for this or you could use any kind of software all I want to do is just create something like a 16 by 16 white image that's it just a white square so I'm just going to do this now by going to new and then calling this Square and it's going to be yeah let's just do 16 by 16 but really doesn't matter because it's just one big pixel and we can zoom in on this and you can change the background but we are just going to leave it white here and I'm just going to save this in directly as a Photoshop file but you could also export it as a JPEG if you wanted to so let's go to projects black dodge game and let's go to assets and let's just say that in here actually no we won't be creating folders today this project is small enough that we can do without so I'm just taking the comments here everything looking good um a lot of people from the forum that's that's nice to see and if you see me commenting I'll or actually nevermind okay so um what we're going to be doing now is we're going to be dragging the square into the UI sprite to fill that in and you can see here that works just fine right away and let's now rescale our player a bit so let's change the scale here to something like six and then we'll keep the one on the Y and one on the Z we can also take our main camera and move it up and let's just move it up by something like four I believe is going to be fine that looks fine to me and we can change the background here but actually what I want to do is I'm going to bring in another square and use this as part of the background because what I had in the other example was on this main background color that was kind of the playable area and then I had a darker background color in the outside of that playable area to indicate that the player I can't move there and that there won't be spawning any blocks there so let's uh take this new square here and he's currently on top of the player so let's just scale this up and we'll scale it up quite a bit on the Y um something like that looks fine and then we'll do I don't know 20 let's do 75 on the X cool so you have something like this which currently looks horrible so let's go in and find a cool-looking color here and I went with something kind of red is kind of magenta like something like that you don't want it to saturate it or it will be annoying to look at this might even be too much and then we'll take the main camera and we'll change on the background here to that color and we won't use skybox we want to use a solid color and then we can just Diegan it a tiny bit so you get kind of this effect and looking at my other monitor here which has some different color settings this looks very saturated but on this one it looks fine so it's just one of those things where you'll have to play around with it as always I'm just going to continue because it's not very exciting to see me just spending a lot of time finding the right color so at this point we're ready to save our scene let's just save it as the main level will only be having this main level and because technically the levels are procedurally generated I mean they are generated at runtime you um most of the time only need one level so we'll just call it that and on we have this new sprite here let's call this one and this is the player let's call him player and we have the square here which is the background cool so you can see currently our background is on top of our player and to simply fix this we could change the order and layer to something smaller which means that it will be drawn before the player and so the player will be drawn on top or we go and add a whole sorting layer since we only have this one thing in the background I'm just going to input something like -2 here and we'll be fine but please do add earlier if you have multiple things in there then let's select our player hit add component and let's create a new script and don't worry about the error that I'm getting down here it's just something that the unity editor has been doing to me for a while now it's kind of annoying but it doesn't have any effect on the game whatsoever I've I've went through a whole load of DRA with that error in my console so for the name here we are going to select player our original and let's double click that to open it up in visual studio and again I will remember to drink water so I don't sound like an old man at jeune um so I'm just going to look at the comments here while we are opening this up I do not have a game jolt account actually um or anywhere to play my games you can't go to brackish calm I have a few projects there that you can check out I made one for a jam called flappy bird and that's an alarm going off so just fix that real quick or called flabby cow which was of course a flappy bird spin off about a flying cow it's pretty cool also kind of done but you can check that out also in two hours exactly there will be a video airing on the YouTube page um that is about creating the latest Ludum dare en3 are called diggin so you can go there and check that out of course um that is also playable on through links to the brackets page but I actually haven't put it on the breakfast page to be displayed there so go to Lutheran diary calm for that but sure okay so we'll continue here uh in Visual Studio so what we are going to be making here is some movement controls and we're going to keep it fairly simple we're going to be doing all of our movement inside of the fixed update because that's a good place to do our physics stuff because well it runs on a fixed timer and physics likes to do that so if you're going to be doing a lot of movement i really suggest splitting it up into registering the movement in the update and then doing the actual movement in the fixed update but today we're just going to be using on the horizontal input axis which works fine inside of the fixed update itself so let's start by getting our movement and we could do this by going like if input get key down arrow right or arrow left but that won't allow for smoothing of the movement and it will not be usable with the joystick so instead we'll go here and fetch the input axis so we'll have this float called X and we'll set that equal to input dot get axis and if you don't want any smoothing on this you can do raw but I do and and then we put in an access name and we're going to be using the horizontal axis which means left and right movement and what this will do is it will fetch from unity information about when we have pressed move right or move left and also if we're using a joypad it will throw that in there as well and it will smooth it a bit out too so we'll get an X value between minus 1 and 1 then kind of interpolates ellipse or it smooths from one setting to another cool and one thing that we always want to do when doing movement is multiply this with time dot fixed at a time because this is inside of a fixed update which is the amount of time passed since our last fixed update was called and therefore it will make it independent on of whatever are fixed at the time it's running ad and then we will have a speed variable let's go up here and create a speed variable it's going to be a float and we call the speed and to fold it to something like I have no idea let's do 15 and that's our multiply on that down there cool so now we can use this X variable to do actual movement and we are going to be moving using a rigid body so let's go into unity and add in a rigid body so let's go add component rigid body and then 2d because we're using two dimensions here and we want to turn on is kinematic and this will allow us to only use the rigid body in terms of colliding with other objects but moving the rigid body is completely controlled by us through a script that's basically what that does so it doesn't matter that our gravity scale is set to one it won't fall to the ground you don't need to do any constraints or anything like that it's only moved by us in this script go and I'm just going to take a look over here I hope to continue this c-sharp tutorial once I have something relevant to say I really find that we I covered most of the things that I wanted to do when starting the tutorial and there's a lot of demand for other stuff at the moment but I don't consider it completely dead either so we'll see where that goes umm cool so let's actually use this so let's first off get a reference to our rigidbody component because we want to move using it and so we need to reference it in some way and therefore we make a private rigidbody our be called or rigidbody 2d code RB and we cache this which means that we find the red body on our object and put it into this variable in the start method so we only do this once and then here we set our be equal to get component of type rigidbody 2d and it's that easy that finds the component it goes down and searches all of the component finds the rigidbody 2d on a game object at Cole Sid RB so now when we want to do something with the rigidbody we simply use the RB our name dot and then we can use move position and this move two moves on the object to a certain position checking for collision along the way and the useful thing here is that the position that we want to move to is our current position plus some x value so we want to move to our current position either a bit to the right or a bit to the left so our current position is called our B dot position and then we can add on to that and we can't just do X because this really doesn't make sense our current position is in two dimensions it's in x and y to the left and right and up and down and then we're adding on a one dimensional value that doesn't make any sense instead we use vector two dot right and this is the same as saying that we want to UM go 0 up and one over to the right so 1x + 0 Y and then we can multiply this with our X variable and that basically means that instead of always going one to the right we now can go minus 1 which means to the left or somewhere in between so that will correctly move our rigid body and we can actually go and plus play test this now if I'm not mistaken I'm just going to clear the console there because I freaking hate those error messages and you can see that I can now now move from side to side and it smooth those out smooths out the movement a bit which is super cool I can do that using a and D or the arrow keys or whatever good one problem that we do still have is if we try and do this we can fly off the map so let's just make sure to clamp on this movement a tiny bit and we do that by going up here and declaring some kind of float and we'll call this the UM map width and we'll set that equal to something like I have no idea let's set it equal to 2010 let's do 10 bits to 5 let's try 5 so the map with here is basically a way for us to do to choose how how wide our map will be and the way that we then clamp this value here which means limit the value between so it doesn't go above the width is to use a math dot clamp which takes in a value that it should limit then a minimum value and a maximum value it's very simple so let's split this code up a bit let's take all of this which is the new position that we want to move to and let's store that in a temporary variable vector two variable called new position so we set that equal to all of that then we can do new position dot X so we only want to edit the x value here and we can set that equal to and then we can do math clamp and the value that we want to limit is new position data X and we want it to be between a minus map with and map with okay so if that makes sense and yes technically our map with is then only half of the actual width but because we're doing both the negative and the positive but don't be technical with me here we are programming and so um let's put our new position into the position that we want to move to and now that should indeed be fixed so let's hit play here and we should be able to move to from negative five on the X to positive 5 on the x axis so that's perfect I'm going to have yet another sip of water and I am going to continue so what we could maybe add now is some falling blocks that is of course essential to what we're doing so let's go ahead and start by adding some blocks let's create some blocks here so let's drag in the square again on the basis of all nice things is the square and um I don't know that didn't make any sense but we are going to go with it and we're going to set an x value of 10 and a y value of something like 4 and um then we can call this a block and we can even add a block component to it but we'll wait with that we'll do that later and we'll add a rigidbody 2d and we don't want this to be kinematic because we want it to fall on its own and we can add a Collider more specifically we can add a box Collider 2d so we can see that that just fits this Collider very nicely actually fits it so nicely that it's almost hard to see the green line here but it is there cool so we can then move this up and when I now hit play it's very simple it falls down but it doesn't collide with our player what's wrong if we look at our player I'll play doesn't have a Collider so we'll add a box Collider 2d to that as well and now it should collide with our player but the game doesn't end or anything we'll add registering that in a moment let's first begin by actually spawning out these blocks and this is the most difficult part of the tutorial and it is a bit difficult because we are going to be using an array and an array is just a list of items that we wear we associate each item with an index so we say that we have three items the first item we associate with the number zero the second one with the number one and the third one with the number two and then we can say that this list and then we use some bracket keys on one then references the second object so in order to actually get some use out of this information let's go ahead and create an empty object and this will be responsible for spawning our blocks we'll call this one the block spawner we can then move this up and off the screen so that all of our blocks can just have the same Y position we can move that to the top of the hierarchy just for ordering sake and add a new component called block spawner create an ADD let's double click this to open it up in visual studio and reload all and everything's fine cool so we can delete that name space there it's really not necessary and then we can add a public and then transform and remember transform how a transform is how we reference an object that we want to do some kind of position or rotation checks with so we could actually actually let's do a transform here so basically the way that I want to do it is I want to create a few spawn points something like five spawn points up here and then I will have the computer choose one at random so we'll have a list of all of the spawn points it will choose a random spawn point and that is where no block will spawn so it will just choose a single one and it will spawn a block on all of the others and it will do that on a fixed timer so let's actually create this array this list of blocks and we make this into an array right now it's just a single thing by using these square brackets now it's a list it's that easy so now we can call this our spawn points and close that off and what this allows us to do is if we head back into unity remember to hit save we can see that we now have a spawn points list and we can increase the size of this list to let's say 5 and we can now drag in objects I don't want to drag in the main camera however I want to create some empty objects that we can use for this so let's go up here hit create empty let's reset the transform on that that's called this one a spawn point and let's have the first one be at 0 0 0 and that is relative to a block spawner because it's a child let's duplicate that and move this over by 2 on the X duplicate again move over by 4 on the X and these values I know them because I've done this once before I'll play around with them you can even take the block up there onto use as a reference if you want and we're just going to duplicate the first one here on once again now do a negative 2 on the X and duplicate that one and do a negative 4 and the way I'm duplicating here is by going ctrl D or command D if you're on a Mac so now we can select all of our spawn points and we should actually what we're going to do and I should have done this before you can't go ahead and select an icon for this which I am just going to do so that will just very clearly show that we have some spawn points up here for some reason you can edit multiple icons at once you could quickly turn this into a prefab if you wanted to but there you go so now we can really visually see the spawn points in the scene but none in the game cool so now we need to drag all of these into the list and we could go like this that takes a lot of time if you have many objects I'm just add you only have five since that we could remove the hour change the size here to zero lock the inspector then select all of the spawn points by using shift and then drag all of it over here and it will insert all of them right in it's a super handy trick and then we can unlock the inspector once again so now that we have this in our spawn point array we can go in and actually use this to do something so right now we'll just be doing this in the start method and what we want to do here is we want to select a random spawn point how do we do that we use a random number so unity provides the ability to give us a random number between a minimum and a maximum value and because all of our spawn points are associated with indices with numbers we can just use that number to get the spawn point that that number is represented by know that that to get the spawn point that is connected with that number okay so now integer we are creating a new temporary variable here which is an integer just a flat number and we're going to call this the our random index and we set that equal to random dot range and this is unities way of getting a random number we're going to get that between zero and then spawn points dot length we could just input five here and that would work just fine but then we would have to change it every time we add it at always not remove the spawn point since that we just get the amount of spawn points in the list by using dot length um cone then what we do is we loop through all of the spawn points so we say for int I equals zero I is less than spawn points dot length then we want to do something so this is a way of saying that we want to do something X amount of times in our case we want to do something spawnpoints dot length amount of times that means five times so for each of our spawn points we will do something and then we have a number associated with each iteration of the loop so the first time I will be equal to zero the second time it will be equal to 1 all the way up to 4 so we can use this eye to get the spawn point that we want that's basically what this means so it declares a variable I it says that we want to keep going until we reach the end of our list and it should increment I by one each time so now we can get a random spawn point by going transform and we'll call this one the spawn point or maybe even random point no let's not do that instead what I want to do is I want to check if random index is equal to I so if this time that we are looping over this index that we reach this spawn point should be the one selected up here if it's the one that we chose the random single one that we don't want to spawn a block if that's the case well then we don't want to do anything so do nothing but if it is not and we do that by simply putting an exclamation mark up here if it's not equal to that then we want to do something so let's actually do that and what do we want to do we want to spawn in a block and we do that by using the instantiate function let's go up here and let's create a reference to what we want to spawn in that's a game object we want to spawn in and the game object that we want to spawn in is let's just call it the block prefab so we want to spawn in our block prefab and we do want to do that at a certain position more specifically we want it to be spawned at our spawn points position so we'll do spawn points and the way that you get when iterating through the spawn points here the way that you get a spawn point at a certain index is always using these square brackets and inserting the index so if I wanted to get the first one point I would do 1 0 the second one I would do 1 2 or 3 you can input the index and it will give you the spawn point but in our case we want to get the 8th spawn point so the first time we go through we want to get the first one the second time we want to get the second one and we want to keep doing this unless of course we get the it's it's the random spawn point that we've chosen we reach or we run out of spawn points so we want to use spawn points I dot position we want that position and whenever you don't want to rotate anything you use quaternion identity that just means don't do anything to it and really don't ask me why you have to put in quaternion that identity it is a quaternion is the continuance is a whole field in mathematics and it's super complicated and we're not going to go into it just just learn that by now just yeah memorize it and we want comment on that again um how did I do the ATO intellisense thing for the for loop you mean the auto completion there basically what you do is you just write for and then tab and then tab and it will give you the ability here to change the variable name so such as the X tab on and you can change on the condition here on to say spawn points that length and then you hit enter and it will put you down here so it's it's a really really handy shortcut it you can do that with the switch statements as well whilst while loops a lot of lot of stuff cool so now we're instantiating that and everything's looking good and that was the most difficult part it's over I think so let's see if this is working so let's jump into unity and we have the block prefab slot here now and instead of just dragging our block directly in there let's drag a block down into the inspector we've now turned it into a prefab that means that if we have multiple blocks in here I can put in multiple of them and change something on one of them and hit apply it will be on transferred to the audio object so that is the awesome thing about using prefabs and also we can reference them without having them in the project so let's select our block spawner and drag a block right in there in the block prefab slot so now when we hit play we should see right away it spawns yes it spawns a block for all of the spawn points except one and it should be a random one it is so it changes every time however it currently does this only once and that is in the start method which means right at the beginning of our game so let's have it do this on a fixed timer instead and to do this we change this to do to be done in the update loop and actually what I want to do is I want to take all of this code now I want to move it into a separate function I just cut the code there control X you can also just control C and then delete it and then void I'm going to call this our spawn blocks and we can paste all of the code in there and now instead whenever we want to do all of this code we simply right spawn blocks we simply call it that way it's now a separate method um but we don't want to do this every frame so update is called every time on the computer draws a new frame which means in most games on decent computers a lot of times between 30 and 200 500 times so that would be a lot of blocks so what we're going to do instead is we are going to keep track of time more specifically we are going to have some kind of private variable some kind of private number that stores the time for the next time we want to spawn a block so it will just be a number the amount of seconds elapsed since we started the game and then until we want to store or to spawn so let's say our private float here and it's going to be a let's call this the time to spawn and we're going to set this equal to 2 that means that there will are at that 2 seconds after we start the game the first block will spawn then we will be utilizing time time this is a unity variable and basically it is just the amount of seconds that has passed by since we started the game so right when we start up the game is of course going to be zero five seconds after it's going to be five and the cool thing about this is that we can use it to determine whether or not it's time to spawn and the next time we want to spawn so you can see here that if we go if time dot time is greater than or equal to time to spawn well then we want to spawn something the time that we want to spawn is after two seconds there's gone three seconds PI which means that time that time is greater than or equal to time to spawn and then we want to spawn something new so let's do that here let's move spawn blocks in here and then we want to change time to spawn to be again some amount of seconds after and this amount of seconds here is normally what we refer to as a rate one divided by the number of seconds is the rate or we could just have a interval so let's do a public float here and let's call this the time between spawns or between waves let's let's call it waves here and it's going to be defaulted to let's say one second here and what we can then do is we can say that time to spawn down here so right after we've spawned a wave or some blocks we can say time to spawn equals time to time plus time between waves so now time to spawn if three seconds have gone by and this was two seconds originally this will now be three seconds plus time between waves which means that it will be four seconds and then the update loop will keep running until four seconds or more have passed and then it will spawn the blocks and again increment by one so we keep doing that in an infinite loop alright so let's try this out in game I'm just going to make sure that everything is working here it looks like it and let's hit play here and we should see after two seconds it spawns the first wave this second wave the third wave the fourth wave and so it keeps on going and you can very easily adjust time between waves to make wave spawn faster and faster and I definitely recommend you and to do this but we won't be doing it here right now because frankly this dream has already lasted for long so on question time I'm just going to look over here just for a moment and then we will continue someone asked why I had to use the quaternion please don't ask that question it has something to do with how you rotate and keep track of I don't want to explain this now that we've reached the patreon goal I'm going to do a game math essentials course might have something in there well we'll see um other than that not much here what happens if you use forces and stuff like that in update instead of fixed update well most of the time things will seem like they're working but you can get some really weird behavior when you do physics in update collisions won't work properly you will have jittery motion it's it's just a mess so move it to fixed update especially if you're if you're modifying this kind of stuff in update and then for example want to have your main camera follow the player so if we do a player movement in update want to have the main camera follow the player that will be a very weird motion because they're doing it in the same loop you need to be separated out unity has some great chapters on this if you want to read up on it in their manual cool so let's continue here by actually um making something happen when we hit the blocks because at the moment here things are a bit dull we can just keep doing this infinitely so what we'll do is we'll go in and modify our player script to do this so let's double click on a play here and let's add a new function here and this is going to be an on collision enter 2d and you need to write this out exactly as I've done here normally you're able to change your method names to whatever you want but this particular function is what we call a callback function and that means that unity decides to call this force just like start and fixed update and update this is called not when the game starts and not when a frame is drawn but whenever we hit something using our 2d Collider and then we can do things down here such as destroying the player or ending the game so in our case we're going to be ending the game but let's just check that this is working first so let's say we got hit just debug log that just throw it out in the console so we should see now that when we collide with something that we get hit and that is indeed true and we it does that multiple times but it won't matter because well you're going to be ending the game once and we could go ahead and end the game right here which means reloading the game and adding that cool matrix effect and all that but what I want to do instead is I want to create a separate script to do this and it's great to separate out your code because I don't think ending the game really has anything to do with player movement so let's create another empty object here reset the transform and let's call this one the game management a game manager is one who ends games so now let's hit add component game manager and let's hit create an add on that and double-click the game manager and in here we'll just make a public function just a public void called end game and then this will be called from our player script and then we can do all of our ending game sort of code in here so all we can do now is maybe just debug that lock the same thing here ending game and then we can just call this from the player but how do we do that how do we reference this game manager that sits on this object with this component that has this method well the way to do it one way to do it is plan your way to do is one once more correct ones really not correct and the the way that we could do it is that we could use a game object or actually our find object of type and then you give it a type so we find an object of type game manager and then you can simply use dot and game so there we go we now have access to that function so we're basically just searching the entire scene after an object call and with a game manager component this could be called anything it doesn't have anything to do with the name just it just needs to have a game manager component so that also means if you have multiple game managers this code will become weird so don't do that and it's not very safe and it's not very performant but it works so we'll continue with that so uh in our game manager we should now see if we had play here that it will throw out this debug deadlock statement and it does so that's perfect and now we can separate this with some other code let's go in here and use unity engine dot scene management and then we'll just do scene manager and you need to do this whenever you want to load new scenes scene manager dot load scene and the scene that we want to load is our current scene so we could just go in here and put main level or zero because it also uses a build index and ours is first in the list but we could also use scene manager dot get active scene so this will just get the scene that is currently active and then use that dot build index or dot a name we'll just use build index here because integers are smaller than strings cool um so that will load reload the scene and it will do that immediately so this one look cool but it will work so if we play here and we died we reset it's already a game really it it is a game it's not very rewarding but it's a game so um one thing that I want to fix before doing the matrix stuff I'm really teasing that towards the end is I want to make sure that our blocks when falling down get destroyed at some point so you can see here okay so I actually have to play the game a bit here to show you so I'm just going to kind of move okay I suck at my own game apparently the pressure it's too much okay so you can see now a lot of blocks have have fallen down and if we look on our main level all of the blocks are now in the scene if you are good at the game and play a lot we're going to have way too many objects in here so what we'll do is we'll just select the black and add a quick script to this that will make sure to delete it once it reaches let's say a y-value of negative 2 somewhere down here let's create this black script and that multiple ways of doing this you could also just put a destroy timer or through the block spawner here on whatever and and we're going to do this using the update method and what we want to do is simply check if our current position so transform the position dot y if our Y component is less than or equal to actually let's just do less than and something like negative 2 well then we want to destroy ourselves so we want to call destroy on this this object so we reference that by game object with a small G there so now when we hit play and it's just a very quick fix you will see if you look over here that the objects are destroyed again so it's removing them just as quickly as it's spawning them back in so that's perfect that just helps keep the hierarchy clean and now for the matrix stuff okay so before we get started with that just wanted to see if there's any questions um let's see here okay yeah and one one guy commented when he sound working with the game manager he is riffing sing it through a property it's a much better idea have some kind of singleton pattern but I think this the scope of this tutorial is a bit too simple to get into a static game manager scene and all that so let's just continue right away so what we want to do here is we want to head into our game manager where we end our game and the problem here is we want to kind of delay this a little bit because when we just load a new scene we don't have any time to see the matrix effect that we're going to be adding so what we'll do instead is we'll use what is called a KO routine and whenever we are using corrupt teams a unity the syntax is super weird and doesn't make any logical sense unless you understand how I numerate this work and what an enumerator is but I would just show you how to do this and then just follow along and pick up on the points that you get so you can see up here when we are using Kuro teams we need to have this using system data collections that is important then what we do is we create instead of typing in void here we create another function of type I enumerator so we say that the function has an enumerator return type and this is a weird word and we'll just continue on and we are going to call this a restart level and you can see all the other syntax is just is a completely normal function and then what we have the ability to do here is we have the ability to type in yield return new waitforseconds and then the amount of seconds that we want to wait so in our case let's wait just a second let's just wait one second so this R will wait and then it will call the code down here so after one sec sec and this is before one sec okay so that basically means that we can take our scene managers code here the code the switching scenes code and we can put it down here so now we'll be called after one second and then we need to call the restart level and normally we would go in and do this but with kuroh teams of course the syntax different so we have to do start corroding and then do restart level and remember the parentheses here we need start and end for the reset level and a start and end for the cruelty cope let's say that head into unity and now we shouldn't see any matrix things or anything like that but we should see that it waits a second before we starting there we go so now to the actual changing of time what is time I don't know but we're going to change it so you can see here that we have time dot time scale time that time scale is a number that is usually usually set one which just represent a factor or something that unity multiplies every code that they do with basically so it's just our current time if it sets to one its regular time if it's zero it time isn't moving minus one will go backwards I don't think they actually support that two it will go twice as quickly you get the point here so our time scale can be modified very easily by simply setting it to a new variable or to a new value in our case I want to have a tweakable value up here a public float and we're going to call this the slowdown factor or something like that so slowness almost sounds cooler and we'll have that be something like ten so we will move where time we'll move ten times slower then we can set time scale equal to one and then divided by that ten so that means effectively 0.1 but we don't want to put in ten here we want to put in slowness however when we adjust time that time scale we also have to adjust fixed update because when we are doing things inside of are fixed update we use this value called fixed Delta time and this isn't changed together with a time scale so if we want that to be smooth we have to change it so let's let's use time dot fixed Delta time and we want to set that equal to our fixed time that fixed Delta time divide it by slowness so whatever it currently is divided by that amount cool then we want to wait for a second then we want to load a new scene however we want to set our time back because I time is transferred to the new scene and so we need to set it right back so we could set a timescale back to one there and time that fixed Delta time we want to set this to odd time fixed Delta time multiplied with slowness so we multiplied we divide and then we multiply and it evens out and it's back to the original state however when we wait for one second this is also affected by time that timescale so if we slow down by ten wait for one second here we will actually be waiting for ten seconds that's a lot of time to wind for an N screen so instead what we're going to do is we are going to do 1 divided by slowness here as well so that will make sure we always wait exactly one second real time awesome so I believe that code should work now great if you understood what ker teens allows you to do I really don't understand you or expect you to understand everything about them let's hit play here and look my cat is joining in on the screen say hi say hi ok I'll put here down here um so if we get hit by a block here you can see that time slows down and we have this cool matrix effect let us look showed you before and just makes it very clear to the user that um the game has ended the last and very final thing that I wanted to show in this dream was just how to make the blocks speed up so that the game becomes increasingly harder over time and a very easy way to do this is to go into a block script and modify it a tiny bit because we have already a variable available to us called gravity scale if we set the gravity scale of the blocks to two they will fall with twice the amount of gravel gravity which means twice as quickly so let's just in here in the script change our gravity scale depending on the amount of time that has passed since we loaded the level so let's do and we only need to need to do this what right when they spawned in I don't think they need to increase over their lifetime they just need to begin to spawn with a greater gravity so our void start here that's fine then we can get a reference to a rigidbody we know how to do that get component rigidbody 2d and then we do dot and then we do gravity scale there we go and of course this is set by one by the set to one by default so we want to plus equals that that means we want to add something on so if I put in one here we will get two and so on so we want to add on a value to that and the value that we want to add is time dot um time since level load we don't want to use time that time because that is the time since we started out the game so if we died and it then reloads the scene we want this difficulty to reset or else it would just continue going crazy but we want the amount of time since we loaded the level and then of course this would very quickly become difficult because this means that each second this will get increased gravity skill by one so after one second it will be twice as hard and that's not desirable I think we should divide this by something like 20 and we can maybe go in here and I don't define a variable here that we will call gravity scale ah I don't know factor something grabs gravity skill I don't know let's I'm just gonna I'm just gonna put inside T I don't I don't have a good name for this going to hard-code it and you can put in a variable if you want you know how to do that so yeah time that times the sense level loaded divided by 20 that's fine so now let's hit play and we should see and I'm going to show you this so I'm just going to pause this here these blocks now have a gravity scale of 1.15 and if I continue the game let me switch over real quick here these blocks have a gravity scale of 1.2 and the ones after have a scrout gravity scale of 1.25 so they will increase in speed and I'm just going to show this by maybe playing the game a little bit please don't tease me if I lose I lost on the first ah okay so this is going better I'm a bit lucky there okay so you can see actually visually that the game is becoming harder and I think we are at a pretty nice difficulty state here so that's fine that's basically all I wanted to show you if you want to continue working with the game i really suggest adding some kind of score just some timers and i've shown you how to do so much with time try and add a UI element just in the corner of the game or whatever somewhere on the screen that displays the amount of time that has passed so that the user can be proud of how long he was able to survive or you could have a wave calendar so it eats time it spawns a block we sponsor wave of blocks it's going to increase by one and you can see how many block waves you have survived that would be ideal for this sort of game I think we're out of time for now this dream has lasted so crazy long I will do a bit of Christian and answers here so you can go ahead and leave them in the comments here or in the Twitter feed and I will see what I can do um and yes I am just coding what my cat tells me they are my biggest inspiration from where can we get those files your unity project I haven't made them available yet anywhere but I will I'm just going to switch to this mode here I will definitely upload them somewhere and let you know I notice it's super done but I will definitely be able make sure to upload them I will probably publish them to on the brackish website somewhere but I will make sure to publish a Facebook post or and a Twitter post and maybe mention it in a video we'll see so follow me there if if you want to be notified about that I should do it rather quickly when the stream ends I will figure something out so yeah but it's not available yet I'm not going to maybe I'll upload it to github it's actually not a bad idea I mean might be a bit much some beginners seem to be confused with git github I understand why it has a lot of functionality that is unusual um but yeah I could definitely look into that when will you introduce currency in your tower defense game that is soon as soon as we get the missiles done I think it is about time for our currency I just wanted to get a secondary turret in there because it looks super weird with the UI only displaying like to our displaying two copies of the same turret let me see here the microphone I'm using is an sm7b from sure so it's a shure sm7b and it's plugging into a moto audio interface cool what kind of education do you have Danish terms are fine the Danish term is given a shil that means somewhat relates to high school I haven't taken a longer education in programming anything like that I am pretty much a self-taught on much of this but I've been programming for a very long time now but yeah I don't have any official degree or anything like that that's actually a question I get a lot so thank you for asking that here um am I thinking of doing an 8-bit 2d RPG that was very specific I am not thinking about doing an RPG at the moment I think most of the time IG IPG projects tend to get crazy very quickly and demand a lot of content and I think that would steal emphasis from a lot of the other projects we have going things that I'm working on is a lot more on the 2d a 2d tower defense a lot more to detail defense a lot more on the tower defense more in multiplayer FPS videos now the game math essentials I'm also don't tell anyone about this working on some blender tutorials maybe very much working progress don't quote me on that and yeah a lot of a lot of cool stuff to do so no one and IPG is not really on on the schedule at the moment I am 19 years old that is my age but that's just a number and how exactly does patreon work I will support you it will automatically get my money every month yeah pretty much well patreon is just pretty much like any other crowdfunding site except you're able to make a monthly recurring donation and you're able to cancel it at any time donations are charged at the beginning of the month and it might take up to 72 hours but that is how it works you are you can choose a completely optional amount you can choose a priced here generally you just have complete control and that is something I haven't mentioned if you're you like this sort of stuff if you want to see more of it if you want to see more videos on all that thank you so much for the support I've already received here but if you want to see more you can go to the patron right there patreon.com slash Brack ease and leave a donation um it really helps me out a lot I mean this would not be possible without that extra money each month so you guys are awesome thank you so much for that um but yeah that that's basically how it works um yeah age is just an integer yeah that's true um I was not at gainsco game scope in all-pole I'm sorry I am Danish yes that's true I am a Dane I have done some client work in the past I don't do much of it anymore but I do have to do a bit because it the money even with patron is not necessarily enough so I do do a tiny bit of client do do a tiny bit of client work I am I going to do a 3d platformer or MMO I'm never ever going to do an MMO tutorial I'm sorry if that was what you were waiting for you can go in and unsubscribe that is a bad idea it's just too much to show in a tutorial video if you're going to do an MMO you need a team I'm not a team and I cannot explain all of the steps that goes into an MMO you also need cash as well it's it's I've seen some guys on YouTube who create and who's working on an awesome MMO project I believe they're just two or three people they've been working on it for like five years you can go and search for unity MMO project or something like that I'm sure it will appear can't for the life of me remember the name those guys are amazing but that is really closest I've ever seen anyone get to an MMO infinity with a small team so it's it's very difficult will you ever make Unreal Engine tutorials that is very possible I actually wanted to do it a long time ago but then my education got in the way and I had all sorts of excuses and that might be a thing I'm by no means an unreal expert but it is a cool engine and I don't necessarily want to do everything in unity I probably will never start using unity but of course that might be nice as an addition of course to the regular regular videos any good suggestions for a way to make an installer that depends on what you are installing and for what OS if you want to see some recommendations when it comes to packaging up your games and stuff like that go watch the second-to-last make a game video called mega game standalone or something like that exporting to standalone that shows a bit about that also um yeah I would just go there I'd talk about it there do I have any plans to remake my older videos um when you were using unity for with unity 5 not at the moment I think we have a lot of other very exciting community things going on that I would rather focus on but if the demand is there I I might do it I'm not not close to that sort of idea but it's not really a priority at the moment um let's see advice a book for c-sharp let me just find here for you the book that I read upon starting and my mouse has quit on me now so I will have to restart that somehow just a moment I will have to restart my mouth see this is kind of the bit that I cut out when doing a video that's the exciting part of the livestream you never know what can happen my squid everything goes crazy explosions yeah so um I really enjoyed the book called beginning c-sharp programming by Jason Lim I think it's just shortened to the point it really is just a logical introduction I couldn't I can just go ahead and put this in the chat here in case I pronounced something wrong there you go also it's in the chat and yeah it's it's it's pretty nice but only the first part the second part gets crazy don't go into that kind of stuff it's really not beginning minded I were planning on making your own complete game for sale not as a turtle but some longer game you could submit on Steam for example that would be super exciting to do at some point in the future it's not something that I am working on at the moment but I will definitely want to create something at some point but real all my energy right now is just going into helping you guys as much as possible with with all that stuff to patron and with videos and all that I really wouldn't have time to create create a game on my own um and if we assume um in the FPS tutorial will I make a tutorial I'm picking up guns and switching guns maybe weapon switching that would be exciting um maybe weapon customization it's something I get asked a lot maybe some class picking class selection screen that would be exciting as well but I won't go ahead and promises promise anything just yet but just know that I'm definitely looking into that stuff I think we need after having taking a look at the user counts and data serialization and all that I think we need to think of ways that we can expand that series and that would definitely be on the list somewhere um cool yeah my my cat killed the mouse that's that's funny um let's see here when Wilma to play FPS 25 episode come out um when I make it I will get around to that very soon um as I said a video airs in just an hour on my Ludum dare array entry I'm really excited about that and that means because today it is a Wednesday that I will probably or probe yeah I will probably have a multiplayer phase video out on this Sunday so I try to upload with Wednesdays and Sundays okay and that is on a priority right now so um yep are you going to going to do more livestream I hope that means that you enjoyed this I sure think it was super fun I am going to do more live streams if you support the patreon it allows me to do and to do more but I will try and do it as often as I can at the moment the limit for the patron tear that we just reached is one a month so you have one guaranteed light livestream each month also you're free to leave some suggestions as to what our kind of content you want to see in these I just thought I would go ahead and just make something so we have something to discuss here but we could do something just completely Q&A or ended or something more community-oriented this was a lot of just me doing a live tutorial basically so we will see that I will consider the donations on Twitch and this was just my first time so I wouldn't want to bombard you or me with information so but that is definitely something that I could look into but for now the focus is on patreon should people want to it I think we're getting towards the end here guys final questions and then we'll do some wrap up my voice and slowly quitting on me and frankly if I if I have to package this up and I know that the work day is already very long let's see here final questions um yep I should do that is it possible to change languages inside the game in unity I'm not sure what you mean if you're talking programming language or like languages that we speak but it is definitely possible yeah there are plugins that help you change speaking languages inside of the game so you can get the game out to Moorea to more users in other countries definitely it's it's a simply a just exchanging out a string and yes you can also have multiple programming languages in one one project but it is not a good idea but frankly um did I do the monthly subscription for unity Pro no I purchased it through study camp which gave me some student discount once I went back when I was still as student and back when they had this sort of when they didn't have this sort of payment model but I did purchase a unity Pro but I think now that they've switched over towards this client stuff or and cloud stuff it's really not necessary um I I wouldn't do that on this I I had the money of course don't buy something you don't have the money for but I I would only do that if I was a full-time developer um I should make a QA about myself that is a good idea I'm actually working on a set up video that will show some of the gear here because that a lot of people have been asking so that is something that you can look forward to um i key a glass yes that is correct am I going to upload this to YouTube as I said earlier I will try to this will probably be stored on twitch servers if I find it worthy I will definitely help you load it to YouTube and if everything works out so I'll try and do that how did I discovery c-sharp through unity actually I started um that's not entirely true I did some programming before I discovered unity um some and a bit of it involved c-sharp but really it was just the need for a more solid and like syntax heavy programming language then JavaScript and so I started with JavaScript and unity and quickly found that that was limiting and led to issues um I mainly onion um you cannot get the dark gray background for the engine with unity personal that was my number one reason for buying as well now alarms are going off all the way all over the place so I am going to stop the livestream here guys thank you for being so awesome and so positive about everything I really really had a blast um let me know if you want to see more of this patron is there youtube is there everything below you know how it goes so yeah
Info
Channel: Brackeys
Views: 183,565
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, beginner, easy, how, to, learn, learning, course, series, tips, tricks, tutorials, workflow, fix, tip, technology, game, development, develop, games, programming, coding, basic, basics, C#, dodge, the, blocks, livestream, vod, stream, twitch, falling, catch, simple, q/a, questions, answers
Id: tyAutnOlsfA
Channel Id: undefined
Length: 70min 53sec (4253 seconds)
Published: Thu Sep 01 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.