Make Cool Games With Unity 3D! - (Part 2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the fun way of learning game development with unity3d here are the games we're gonna create in this tutorial I think that learning game development should be fun and easy everybody can do it and I want you to be able to create your own stuff as soon as possible that's the main goal of this tutorial this is part number two so please watch part number one first if you want to be able to follow along also the download link to the entire project will be in the video description for now we'll focus on learning a couple of new unity features and getting some more practice with the features we already know I want to teach you about prefabs for that let's create a new scene and let's just create a cube and I'm gonna drag that into the house make the ceiling red and this orange something like that now in our assets lets me create a new folder and let's call it prefabs and if you drag an object like this in here it will turn into a prefab prefabs are awesome because now every time I want to create a house I don't need to go through that entire process I don't need to duplicate all of those single objects I can just drag that house and pull it right into the scene which is fantastic but what's even better if I want to modify this now I can just click on this little arrow here right next to a house or I can also double click it here and this will open the prefab in prefab mode and as you can see all of the other houses are gone and all all I can see in the scene right now is this single house that's because I'm in prefab mode if you want to go back to the scene then you just click on scenes right here that brings you right back but the cool thing is if you modify this object in prefab mode for example if I give the ceiling a different color for example yellow cool thing is all of your houses are now changed you can even add more objects in the house here let's suit like this and then once again all of our houses are updated so this is just an absolutely amazing feature by the way you can still customize these houses you can still change the color here and seen for example that is still possible and now this house just overrides the color if you select that house and click on overrides right here you see all of the things we changed that are different compared to the to the pre feminine if you want to go back to how the prefab looks like you can just click revert all but in some cases you actually want your preferred to be slightly customized so that is still possible one reason why we also absolutely need these prefabs is because we can instantiate them later on so for example if we want the player to be able to build houses then we can just instantiate these prefabs there was an important thing to learn so let's remove all of these houses thank now the first game we're gonna create as a little exercise is this one where you have to get the ball into the basket we will need some scripts from last time so just to keep working in the old project or copy them over from the old project first of all let's create a little bit of a ground plane here we go and no I'm just gonna create a couple of cubes there's gonna be a removable cube so let's call that removable cube we just learned how to use prefabs so let's turn it into a prefab bang you can see prefabs always have this kind of blue filled cube right here and other objects are grayed out now these removable boxes are supposed to fall down so let's give them a rigidbody bang I made a bit of a mistake here I just realized and editing I should have added that component in prefab mode so it's on the prefab otherwise if you instantiate another one of these cubes it will not have the component okay here we go now let's create a new script I'm gonna call that script destroy on click basically can remove everything from this and just write void on Mouse down destroy this dot game object you don't necessarily need to write this what just looks a little nicer this basically refers to this object which in this case is this script destroy on click and we basically destroy the game object this script is attached to here we go when we click on it now if I want this script to be on all of these removable cubes I'll just click on the little arrow right here open this in prefab mode and at the script right here destroy on click it's automatically saved so I can go back to the scene and now all of these cubes have that script attached to them super useful let's see what happens when we click play it works I can remove them so now I want to create a game where you have to get that ball into a basket so what I need for that is a basket I'll actually add the rigidbody onto an empty game object this is nothing but a transform and a rigidbody and then here in the in the children will be all of the geometry sometimes that's a little more clean for example this allows me to rotate these blocks properly if you wanna see what happens when I direct one of these blocks into one of this blocks this doesn't look nice because you're basically dragging a transform object into a transformed object and that can have some weird effects like that so we don't want that that's why I'm dragging everything into a empty game object with the scale 1 1 1 this way we don't get any weird artifacts like that and it allows me to create a nice looking basket like this that will act in a physically correct way let's see if this actually works ok yes it does only thing I forgot to do is giving this sphere a rigidbody and I obviously don't want that to happen so what I can do if I don't want that basket and that's fear to fall outside of the level we can just freeze the z-position and I think I also want to freeze the X rotations oh this basket can't fall over on this axis yeah this looks a little tricky for level 1 so how about we put that basket on the ground here and let's see if this is possible yes indeed it is that's probably a good idea to turn those into prefabs as well the ball and the basket because that allows us to do level design way more easily in future levels because once again we can just drag these objects in if we decide to change these baskets if they are prefab then we don't need to go into every single level to change them we can change them all at once so definitely make sure to use prefabs as often as possible whenever possible turn objects into prefabs now I want to detect if that ball has fallen into that basket and when that is the case I want to go to the next level that we can finally make a game with multiple levels so how do we do that I think the easiest solution through this problem is just to duplicate this ground plane here move it up a little bit and then just try to detect whether the ball has touched this object or not luckily we already have a script that does almost what we want wanted to do and that is this restart level on collision the only thing we've got a change here is that we've gotta go to a different scene it already has the collision check it also already checks the tech so it excludes all of the wrong object for example if the basket collides with a block we don't want to go to the next level only if it collides with a ball we want to go to the next level so that's what this check is for so let's duplicate that script now just hit control D and I'll rename it to load level on correlation and we also need to rename it here and now what seems to be the case is that this script is not part of the solution anymore so there's no color coding usually this should be green usually this stuff right here should be color-coded that is a problem right click onto your scripts folder Open File Explorer and then let's strike our new script and put it in here because as you can see it doesn't show up here yet so that is a problem let's pull it in there and as soon as we do that we get our color coding back and another fix to this problem is just to close Visual Studio and reopen it but this is a faster fix instead of the active scene we want to customize the scene we want to load so let's just add a new serializable field and at the scene we want to load and then we can just replace this entire thing and just load that scene instead and I think the ball is the one I'm gonna give the script to load level on collision now I can tell this ball which scene we want to load one when I when we win that's just low Game three for example and we want to check the collision with one of these texts that we can can apply let's give this part of the basket a new tech with that tech and let's name the tech goal now I'm gonna give this the go Tech after creating that to crane a new check you just click on the add tech and then edit right here on text make sure that the rest of the basket does not have the go check because we only want to win when the ball actually touches the bottom of this basket and then one script we could also use is restart level with button this is one script we already wrote in the last tutorial so let's just use that again and for the button I usually want to choose R so here we go now this winnable game nothing happens genius that's because I have to specify the tech here I forgot that so if that ball touches any object with the tech goal then we'll go to the scene with the name game 3 no if I mess up nothing happens and I can restart the game with R and if I beat the game I go into a new level that's perfect so let's just create a second level just for funsies let's rename the scene to level 4 game for level 1 now let's duplicate the scene exactly and let's now it's named level 2 and the level I want to go now from level 1 this game for level 2 yes I wanna save that and here from level 2 we can either go to game 3 or we can go back to level 1 let's just go back to level one game for level 1 one thing we always need to make sure is that those scenes actually exist in the build settings so just drag them in there you can reach the build settings here build settings and then just drag those scenes in there I want to level 2 to be a little harder so how about in level 2 this basket is on a tower as well oh yeah oh yeah yeah and now we go into level 2 fantastic come on yes bingo and we go back to level 1 so this works fantastic in my opinion the main thing you need right now is more exercise more practice so let's just keep making a couple of simple games I know you're probably still not able to write your own scripts that's completely normal at this point and we'll probably have to dive a little deeper into that entire coding thing at some point later on for now let's focus on what's fun and I think what's more fun than particles and sound effects and a little combat scene so let's create that next up we want to expand our fighting game so why don't we just duplicate the one we already made last time this PvP fighting game all we got here so far is two controllable balls one with a sdw and what the other one is controllable with the arrow keys so let's duplicate that and call it game number 5 let's call that one control of the ball by the way and turn it in to a prefab as well if I fall down here the level already restarts that's because the ground plane already has the script on it that says restart level on collision that's one of the scripts we made last time and then as a tech it has the player obviously here our controllable ball has the tech player if you have followed the last tutorials correctly replicating this shouldn't be all that tricky so let's create some enemies and these enemies obviously should be prefab so once again I'm gonna drag that into my prefab folder and also give them a rigidbody so we can push them around once again I should have definitely added that in prefab mode but I will realize this mistake later on so sorry about that let's add a couple one of these and then see what's happened what happens ok actually I think they need to be slightly bigger otherwise I can't really push them correctly yeah that feels a bit better if you want it to be easier to push them around you can reduce their here I'm looking for the rigid body which is not there in prefab mode because I didn't add it in prefab mode it's not part of the prefab so that's why it's not here oopsie-doopsie if they need to add the rigid body in here so let's just reduce the mass of them to 0.4 this way it's a little easier to push them around and now if I managed to push any of these into onto the ground I want them to disappear at the moment they just keep rolling that's not what we want so let's create a script that destroys them as soon as they collide with something once again we already have a script that's very similar on the ground restart level on collision and we're gonna create one that is simply destroy on collision if you feel up to the challenge you can create that yourself try it out so let's duplicate restart level on collision and turn it into destroy on collision once again no color coding so let's open this up real quick and drag that script in here let's rename the script properly to destroy on collision and now the questions what do we want to destroy I just removed that and added some brackets here you want to destroy this object or the other object and I want to make the script as versatile as possible that we can possibly use it in other scenarios later on as well that's why I'm just gonna create a serialized filled bool variable so basically a setting that we can select in the unity editor later on destroy self and destroy other and then in here when the tech is correct and when we collide with something we can just check that setting so if destroy self is checked in the unity editor will just say destroy this dot game object and if destroy other is checked then we'll destroy collision dot game object instead so the other object we just collided with and now the cool thing is we can either put that script on to the ground or we can put it on to these balls let's let's just put it on to the ground destroy on collision and we want to check for the check enemy obviously means we've gotta give those guys the tech enemy open the prefab and add a new tech and give them the enemy Tech and now if you go back to the scene all of these enemy balls should have the enemy Tech and the ground plan should check for this tech enemy though if it collides with anything that has the tech enemy it should destroy the other object not itself boop boop yeah seems to work just fine I want these things to be even easier to push so let's reduce their mass even further yeah now they feel nice and light okay very nice so now these objects are destroyed when we push them onto the ground next up I want to make these enemies attack the player by moving towards them so I want the enemies to try to push the player off the platform as well and for that we're just gonna make a new script and I'm gonna call it accelerate towards accelerate towards pop um let's open that once again it's missing from the solution so let's move it into that folder boom and I want to have the realized transform variable that we can use to specify where we want this object to go and I want to do the same again for the speed then we also need a rigidbody variable this is not one we will set in in the editor even though we could that would work as well but we can automate that so let's just do ridge it equals get component rigidbody and this will automatically get us the rigidbody component of this object and we obviously need that to apply a velocity to this object so rigidbody dot velocity plus equals and now the direction from me towards the target is trans towards dot position - transform that position that is the vector from me towards the target position and as I want to control the speed of the myself I'll just normalize the length of that vector and multiply it with F speed don't worry if you don't get all of this not normalized basically just sets the length of a vector to exactly one if we multiply it by any value after that then that's the new length of the vector and obviously we also should not forget about time.deltatime just to make sure we get rid of differences and play time execution for example if the games running twice as fast we don't want to apply the speed twice as often so this balances that out a little bit if we would use fixedupdate then we could get rid of that as we're using just the normal update function here I'm gonna add that time the data time in here so yeah that's a little bit of a long line I apologize in theory we you could break that down a bit like this meet what's target could be this vector for example and then we could just say meet what's target or normalized time speed times time that data time is what we're going to add on top of our current velocity so let's go into one of our enemy objects let's seven to accelerate towards and then for the speed we're gonna pick something like two let's go back to the scene as we actually need to link a scene object years or let's select all of our enemies and then we're gonna take our controllable ball and drag it in here so all of these enemies will have a point to move towards if you forget to put something in here you will get a null reference exception a bug basically and that will appear right here in the console so make sure transports is filled out in all of your enemy objects and now they're all moving towards me that is kinda cool let's make them stronger okay so finally we get a bit of a challenge I want to keep enemies spawning in here now so I want a constant supply of enemies basically the way we do that is by creating a spawner let's create an empty object let's call it spawner if we wanted to be a little more visible than we can give it a little icon let's just place that somewhere up here and let's create a new script spawn first of all I have a serializable filled with game object we want to create this is where we can fill in the prefab later on that we want to create then I have a float variable with time intervals so here we'll be able to set the intervals between spawning objects the time intervals between spawning objects and then finally I want to be able to randomize the spawn position of these objects a little bit for that we're gonna use a vector3 because we want to randomize the position in three dimensions possibly and I'm gonna call that spawn position jitter and then the timer that actually counts down another float variable that one doesn't need to be set from the inspector so it doesn't need the sterilized field property at the beginning we'll simply set the timer to F time intervals and then in the applet even real simply substract time dot delta time this way our variable will count down and then as soon as it's smaller or equal to zero we will set it to its start value again so this will restart our timer now the only other thing we want to do in here is to instantiate something and we want to instantiate go create then we need to specify the position and the rotation of the object we want to spawn and for that we're just gonna say quaternion dot identity quaternion is the variable type for saving rotations and quaternion that identity is simply the default rotation zero zero zero so what's missing here is the the jitter we haven't implemented that yet and also this will throw a beautiful error message I'll show you in a sec so the spawner will get my new spawner script and the object we want to create these enemies a push enemy and for the time intervals we're gonna choose something like every four seconds let's set go and let's see if it works and yes there's one dropping down but as you can see they are not following me why are those newly spawned vaults not following me there's a very simple explanation let's actually remove all of these old enemies so see these newly spawned enemies are not attacking me so something's wrong if something's wrong first place you should look is into that console right here and what do we have right here unassigned reference exception so basically a null reference exception I can show you what the problem is pan here our trends towards is not assigned our player object is not in here and that is because prefabs cannot have references to objects that are in scene so here my push enemy yes that if I put that into the scene then it can have a reference to another object in the scene then I can link the the transform of the object I want to move towards but if I go into the prefab in prefab fuel you can see that I cannot assign anything here or at the very least no scene object so what we can do is in accelerate towards we can check if that one is assigned so if trends towards is equals to equal to na so that means if it's not assigned then we can just Auto assign it and find the objects ourselves one slightly hacky way to do this is with find object of type and then further type we can just use a script that is only available on the controller ball at player-controlled velocity dr transform so an hour accelerate towards script this is a little bit of a hack to actually make sure we don't end up with a null reference exception after spawning trance thwarts this variable is not assigned then we'll just simply assign it by finding an object and scene will find one of these components at player control velocity and as those scripts are only available on our player object we can be sure that this line will actually return our player object so now if I hit play all of the enemies should attack me and indeed they do okay how about we add our jitter in though they the enemies don't spawn at the same position over and over again so here let's go on our spawn script let's create a new vector right here the difference between initializing a variable here and right in the script is that here it will be available in the entire script and if you created here it will only be available in this part of the script until we exit these brackets basically I only need this variable right here not in the entire script that's why I can create it right here I don't need this variable to be saved until the next frame starts for example okay so far nothing has changed I set the spawn position to the position of the object and then in in Senshi eight I use v3 spawn position instead of this term so far everything's exactly the same but now I can start applying my random jitter to that spawn position for example I can say plus equals vector three dot right times V 3 spawn post jitter dot X times random dot value minus 0.5 F and I poured that into brackets so this term right here will return a random value between 0.5 and minus 0.5 because random dot value returns a random value between 0 and 1 so if we subtract 0.5 from that we will get a random value between minus 0.5 and 0.5 and I'll multiply that with the right direction with a simply a vector that says 1 0 0 and the X component of my spawn post jitters so if that is set to 0 for example then that entire term will be 0 as well and if that is 10 then you know jitter is gonna be 10 times to speak so that just controls the size of the jitter in the x axis and we'll just do exactly the same thing for all of the other axis as well so forward is the C axis uh pass the y-axis here we go that's our entire new spawn script so let's add a little bit of a jitter or shall we for example try something like nine and seven I really want these to spawn in all corners of the map here we go yeah that's what I'm talking about yeah here that's what I'm talking about now they're coming from all directions mm-hmm and bring it on bring it on Oh it's actually quite hard next up I want to show you how to create some cool effects for this game so if you manage to push down enemy I wanted to explode let's create an empty explosion object I'm gonna call it enemy death FX and we're gonna add a component to this first of all a particle system here we go and now we should see some particles coming out of this when you scroll down here and the particle system all the way to the bottom renderer here material we want to select one of these default particle materials and then also I don't want these particles to come out as a stream here I want them to burst out so let's go to emission and there are a lot of settings in this particle system you can already see it's absolutely in then right over time I'm gonna put turn that to zero and then here for bursts I'm gonna add something at time zero we'll create 50 particles and the rest can stay like it is right now for the shape instead of cone I'm gonna select spheres all the particles will burst in all directions and I'll set the lifetime of these particles to two and the speed to two as well then I'm gonna click here on color over lifetime check that so it's enabled and then up here when we click on that we can set the alpha value so at the beginning I want the Alpha value to be at 100% and at the end I want the alpha value to be at zero so now as you can see these particles are slowly fading out let's make them a little faster once again and maybe I'm gonna change the color to orange or something here that's our effect here by the way duration that's how quickly the animation loops if I put that to two I'll see it very often when we actually wanted to be a lot higher so let's put it to ten also this particle effect is only is immolated when the object isn't is selected and also when you click on play here what I want to add now is a sound effect for that you just add an audio source to this explosion and for the audio clip we just need to import an audio clip oopsie-doopsie I think there are some prefab let's put that into the prefab folder sounds and in my sounds folder just import this explosion sound now I have a sound in my sound folder and I can drag that into the audio sauce on my enemy to SFX right here into the audio clip spot and then obviously loop needs to be disabled but that's disabled by default so everything's fine problem being is if we would hit play now this object would stay here forever let's place this summer we can see it and then let's just see what happens when we hit play okay so we can hear the sound and we can see some particles flying away so far so good but as you might see if we wait long enough we can see the particle effect again because the object is not deleted Z our enemy death of X is still there doesn't disappear and it keeps spawning particles so let's just write a small script really quick destroy after and if you want you can absolutely try to write it yourself by now what we want to do in this script is create a script that destroys the object after a set amount of time basically just four lines of code serialized field and then we will have a timer variable that can be changed from the editor will it's abstract time not data time every frame so that means this timer will get reduced by one second every second or by one every second and as soon as that timer smaller than you we'll just say destroy this dot game object so now all we've gotta do is add this here destroy after and I want to destroy this after five seconds so this is our death FX we have a particle system on here then we have an audio source and we destroy the object after X seconds let's put that in our prefabs and hit delete we don't need that in the scene anymore now all we got to do is spawn that when our enemy objects are destroyed and I think what I want to do is I want to duplicate this script right here destroy on collision and turn it into spawn on collision spawn on collision okay once again color coding somewhat missing so let's hit folder and file explorer drag that in here got a rename this group up here as well and then I'm gonna rename destroy self and destroy other into spawn self and spawn another you can rename things by right-clicking them and clicking on rename this way all of the references will be replaced as well now obviously we gotta add another field because you have to know what we want to create and we want to create a game object in Senshi 8 go spawn that's the game object we want to spawn that transform that position with quaternion dot identity which once again is just the default rotation at our own position that happens if spawn self is ticked and we want to spawn the object at the other position that we just touched we'll just add another collision in front of that transform collision dot transformed our position that spawns the object where the other object is add this component to our ground it already has this destroy on collision let's give it the spawn on collision script oopsie-doopsie so I was there r1 and then if we touch the enemy then the other object should spawn death animation so let's drag it in here enemy death FX will be spawned by the other object if the ground plane hits an enemy let's see if it works okay the first two fell onto the ground just by themselves pretty useful nice it works ok we're already done creating new scripts for this episode but I want to prove to you that those scripts are more than enough to get creative and to create a bunch of different games for example if we want the difficulties slowly to ramp up in this level how would we do that without writing any new scripts whatsoever how could we ramp up the difficulty over time there is a very simple solution we just take our enemy spawner and turn it into a prefab and then we create a new spawner maybe make that purple or a green that we can tell them apart and this green spawner will also get a spawner script and it will spawn an enemy spawner after 1 minute after 1 minute and 1 second maybe something like that there will not be any jitter so it will be created exactly at this position let's make let's do 31 seconds so we don't have to wait that long this is how we could ramp up the difficulty let's see if it works every 31 seconds this spawner spawner should create a new enemy spawn and this way we can slowly ramp up the difficulty I redesigned the level a little bit just so it doesn't look us exactly the same as the game we created last time and I gave the enemies a bit of a pattern on the outside damn Z I need to survive to the 30-second mark to see if the spawn rate of the enemy increases oh yeah spawn rate increase now you can see that there are always two of these dropping right after each other the spawn rate increase again is I think so oh no I think they got me so what if we want to increase the spawn rate of these enemies only once even for that there's a solution we could just grab this spawner spawner and tell it to destroy itself after 35 seconds this way it will spawn another enemy spawner after 31 seconds and then it will destroy itself after 35 seconds and this way the difficulty won't keep increasing any longer so there are already a bunch of different things you can do with this we could even create a different enemy spawner that maybe creates different and starts creating different enemies different different kind of enemy after after a certain amount of time the possibilities are endless now if I want the player to be able to win this game why don't we just create a goal object appear after a certain amount of time first of all let's create a goal up their sphere and I'm gonna make that green so that's gonna be our goal and then I'm gonna give the gold load level on collision and if it collides with something that has the tech player it will move the next level or something like that and yeah we could either spawn that with a spawner but some a different solution just came to my mind we can also just create another sphere around it that protects the goal basically and can give fifth to destroy after script make that the same color as the wall and this will be destroyed after one minute so if you manage to survive one minute this sphere will disappear and we'll be able to touch the goal and finish the level so there are already a lot of cool things like that you can do with the scripts we already have if the tech s big letters do I need to write it with big letters as well probably them player Jesus Christ will you work fine then let's go to this scene because the scene has been added to the build settings here we go bang the last game we've cried is a very cool one and the cool thing is we don't need any new script so no more coding this is basically my way of proving to you that there are already a ton of things a ton of games you could make with the scripts we have written so far you have a very cool tool box now and I want you to start experimenting start coming up with your own little game ideas but as to fine a little exercise for today let's create this racing game together I'm gonna create a player object give it the player tech move that player to the beginning of the track you want to give it this script at player controlled velocity and I want to control this along the c-axis let's control this with a and D now with the camera I'm actually going to do a funny thing rip and I put it inside this ball oh yeah same goes for the hierarchy I'm gonna put the camera into the player so now just stays inside of the player whenever I move the player player obviously gets a rigid body so it can move physic space but it I will freeze its rotations or ball won't rotate so now when I hit play I'm already able to move to the left and to the right and I'm just gonna say at constant velocity so this will be something in the players no control over and we just steadily accelerate in the x-direction by 0.1 or maybe 0.25 something like that prepare for liftoff let's see if that works yes does just yeah let's give the player the ability to restart with our and then obviously you've got to ask the questions what if there were objects you could delete by clicking on them or what if there are we're enemies following you around you can combine all of that so get creative bring the action on obviously another way to prevent the player from getting way too freaking fast is just to ramp up the damping of the rigidbody you're the drag let's put the drag to 0.5 and see how that affects our movement okay that was 0.5 is already too much it seems about we do 0.25 and then increase our acceleration to 0.35 okay and that actually feels quite cool and then obviously if you want we can create a goal at the end as well just put an object there and if we touch it it brings us to the next level with our load level on collision script nice that's actually pretty fun by the way if you want different skybox for your cameras or not this background but a different one you can just select your camera object with the camera and for example pick solid color and then I can just pick any color I like and oftentimes that looks a lot better than the default skybox and with that I say congratulations you've made your first six games I would say you have officially mastered the basics of unity 3d now all that matters is that you stay at it keep going keep learning there's still so much more you can learn and I'll obviously try my best to support you on your journey because I think game development is worth it there is value to be had we can make the world a better place we can make people smile we can give their lives meaning and on top of all of that making games it's just super fun and addicting so keep at it keep making games [Music]
Info
Channel: Jonas Tyroller
Views: 108,274
Rating: undefined out of 5
Keywords: unity tutorial, unity 3d tutorial, unity tutorial for beginners, unity 3d, how to make a game, how to make a game in unity, make your first game unity, game development, learn unity, unity for beginners, game development for beginners
Id: V29O_Q7W2ZU
Channel Id: undefined
Length: 41min 56sec (2516 seconds)
Published: Sun Jan 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.