Unity 2D Space Invaders Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you'll learn how to have these enemies moving and we can fire with the spacebar and if you get hit it restarts and it pauses on you and you can see if three lives down at the bottom and you're going and trying to get enough enemies to destroy before they reach the bottom of the screen and if you lose all three lives you'll lose the game so I hope you guys enjoy okay so if we drop down focusing on our main camera and we actually change our arm layout a little bit you can drag this all the way to the left drag this all the way to the left and then we can actually move this so it's below I want my my scene window to be a little bit larger and go all the way down at the bottom for this asteroid shooter game makes it just a little bit easier and nicer to play with and we got enough room here we could shrink this down a bit and I've actually saved this as a specific kind of layout you can call whatever you want but I just called it big center window you know then you you have this for any any game that's more of a block type game and has a little bit more height to it than what we've had before it just makes it easier to see some stuff so if you then go into your assets as of now in this saved big Center I also have it a one column layout I'm gonna go ahead and change it to two column layout for this so that I can see the folders and the assets that I put in and they're not just little tiny text right there so if I got a folder and I create a sprites folder I've done this in previous games and how I do it is very simple I get a white block and that is one of my sprites so I simply go and get a white block PNG file and you can do that by using the snipping tool if you're on Windows or you can screen capture on a Mac and if you you know just create a little block here of any really any size we can manipulate that block and if you save it and I saved mine that's white block and then you can use that to easily drag something in manipulate the size and it's just easier to make that as a 2d image then going in and then you have to create a sprite and drag the sprite in or you can create a 3d object that's pretty flat on and and it's just a little harder to manipulate I feel like this is the easiest way to create something so I'm gonna go ahead and make make it about the size of the world you know we'll edit this as we go a little bit I'll make it all the way go to the top of the world so okay there's a little bit of room on the sides here but so if I go to my color and I go black I know it's I'm gonna make it space so and I can go to my white block and I notice it's a little off-center I can make my x value zero and I know that I'll move it over and I could make my Y value zero or you can go in here and reset but that would reset the scale so control Z what if I just change my position that'll make it easy there so the next thing we're going to do is make a particle effect and so after we rename this so let's rename this just our background and we can make a effects particle system and this particle system I'm going to rename as Star effects star effect and so as of now this just goes straight up in the air and that's just kind of the default and so if you move your Y value to 10 it'll go up kind of where we want it to go we're gonna have our Z position go ahead and be zero as well and we're going to make a couple changes here so our rotation for our X is gonna be 90 okay now that flips it so it is facing down okay so if in your particle system you will go to the the effects and change some of these I'm gonna go through all the things that if I if I kind of hit these right in a row it might be a little easier for you guys so I changed my duration from 5 to 10 I checked pre-warm I changed my life time from 5 to 50 so that really got the the length of time these will survive these particles will live right at the right size I have my speed to be 0.01 so it barely moves they barely move and if you don't change the gravity they won't even really get much past this okay and then I my start size to be incredibly small so 0.05 to 0.01 and that is random between two constants okay and that's our start size rotate all the same down to gravity modifier I changed this from 0 to 0.0 0.4 the stars to slowly fall and based off the size and the mass some will fall just a fraction faster than others and have a little bit of movement and then we go into our shape and the shape I changed my angle from being at 25 to being 0 and that made it so that instead of having a cone effect it kind of fell down and has a like chandelier effect where it just Falls straight flat down and then that's all all I did with my particle system the position I put at 10 so it would be above so here's five and here's 10 for my y-value and then I rotated the X 90 degrees so that it would flip from facing up to facing down and then I scaled it up just a little bit so 7 about 7.5 on each each of these so that it's evenly scaled and so now when I run this I could even make my stars smaller than that and actually will so I'll go point 1 to 0.005 and then if I run that here we really you can really see us falling through space and so that's just a really great background a great look to it so I'm really happy with that so if we keep going the next thing we're gonna want to do is create our object and so instead of creating a 2d object and sprite and dragging that in and all this all this other stuff I'm gonna show you guys now that you can actually go into the asset store and there's a ton of different options and if you just click this assets button or you can get to it through a browser as well but if you go to assets or you can only click open and browser usually that's a little bit faster this this seems to be a little bit slower but more convenient so if I do a search and I do spaceship I've already looked it up and I make it to D free you get pretty good variety of things that you can you can download and create so I've downloaded this 3d Jeebs and so you click on it and you would download and then you would click import and so I'm just going to show you that import process so once you click import it will have these different not only these different images prefabs that you can see but also some of the animation that you can do with them so this would be really cool addition so that this kind of separates to the computer graphics person that will can design some of the images and sprites in a game and the game designer and so now that those are imported in you can see the spaceship we can rename space sprites okay and so we can drag this into our sprites folder where we have our or white block and we can separate it out so that it's specific to the space sprites okay and so you can click lick click in there and they actually already have prefabs and the reason they do is because these are not just sprites anymore they can you people can add different things to these prefabs in this folder and they can do anything from having code in there to just simply having an animator and that's what we have here so if we drag in our spaceship you can choose whichever one you want I notice I can't see my spaceship that's because if I need to click on my background and go to additional settings and make this negative one so it always shows up in the background and we'll have our ship be a little bit smaller about take up let's see I think I want it to about take up one full square here there we go yeah that's about the size I want doesn't have to be perfect I'm gonna rotate it and I'm gonna rotate it at 90 degrees on the Z value and so I can see right here this will be the Z value actually I want to switch it flip it all the way around so 180 and now we have our ship and if we run it it's already has that animator in it and so let's see what this animation does I believe it has these lights flickering a little bit and has some some yeah has some stuff blowing out the back some flames for our Rockets and stuff so feel free to experiment with a few different assets packages that's just one I found that looks like an animator I could see if I put a bunch of these in here it'll look a lot like asteroids and so that's kind of what I'm going for don't get hung up by spending way too much time trying to figure out the absolute perfect graphics wait for your your your big projects that you really need the graphics to be absolutely perfect to take up too much time focusing on that when you really need to focus on understanding the gaming logic and the programming side in order to even get to that final game step and so don't waste your time now on graphics if you're you're trying to focus on learning how to build the game right now okay so if we we actually run this you can see what these spaceships do and these will be our enemy spaceships now you can you could go I'll go all the way to making the pixel graphics of old times of the asteroids and and use that as well if you want so just up to you so now that we have our spaceship let's go ahead and delete these for now we're gonna rename this and we're gonna rename it our player and this player is gonna be different than what this prefab is so we're gonna need to go in to our assets folder and create a prefabs folder and so here we can store the prefab specific to this game yes that was a prefab okay and so if we drag this into the prefabs folder we can make it just an original prefab because this is the one that we're going to use and be connected to so we want this to be the same not very of the prefab so now that we have that we can go ahead and go back into our assets create a scripts folder oh I made a script instead of scripts folder so you can go in and create a folder scripts now if you double-click this and open up visual studio and even rename it here this is something that can be be quite confusing because you can go in and go into visual studio so we have Visual Studio up right here you can see new behavior and this is actually called new behavior script even if I rename this what I meant to call it before I jumped off of it I meant to call it my few rename it and what I meant to call it was my player script it goes in and you open this up so even after you rename it player it might still have this new behavior script in there you just want to make sure that this is called player okay and then once you're in there we've done this we've done this before of creating a way in order for us to move now in order to move our player we're gonna be able to move all over the screen so just WASD controls so if imp input dot get key and we will say key code dot right arrow so when you do WASD or you can do the arrow keys then we're gonna do transform dot translate and we're going to a new vector3 if you've done this before this is the x y&z value and we're gonna change our x value and we're gonna make our x value move five and ours is going to be if you want it to be a smooth mover you can do it based off of frame so you do five times time dot delta time and then we can save it make sure that works correctly before we do the other four directions if you want to use WASD whoops you want to make sure that you do it negative here or the right direction you do negative five times time Delta time and then you want to copy paste paste paste and if we do left up down left will be positive five up you put this X value in the Y value so you put zero comma negative five times time Delta time and then this will be a positive in the Y value of the same value so positive five times time Delta time and then you can run it and you can move every direction and this is pretty pretty good speed that we got going okay so actually I'm gonna put this these those if statements inside their own method called movement and then if you copy and paste and then we pull it movement in there that way this can be organized and we can only call we right now we're always constantly calling this method but sometimes we might not want to move and and we're gonna have the game paused and I won't want to be able to move and so it later on we can do if the game is paused we we can't move so now we'll make another method called fire projectile and so in order to do this we will need to do similar if input dot get key down instead of just get key get key down just checks if you pressed it down and you can keycode dot space okay and we've actually done this in our platformer game or previous video of how to add something in the world randomly and on a timer now this we're going to add an object projectile into the world when we push space down and so in order to do that we will need to create a few different game objects and so we're gonna create a public game object that will be our projectile and then public game objects that will be our projectile clone now here we're gonna store the value of a a prefab projectile and then basically we're going to copy make a copy of that and have that be added into the game so this can be our stored value of our prefab and the clone is how we are able to make multiple copies of that and so in order to make those copies we're gonna need to add in a projectile into the world but we'll do projectile clone equals and we'll instantiate a a projectile prefab so it'll know to grab the projectile prefab once we put that in women put that in yet and then it says transform parent so we could do new vector3 and we could simply do 0 0 0 as game object okay it also wants rotation and so we do transform rotation okay so then once we save that we haven't added anything as the projectile so it doesn't know what projectile clone will be we also haven't had it in our fire projectile command excuse me method inside of our update method so now I'm going to go back in and I'm gonna add in a white block and I'm going to change the size of this white block to be look more like a little laser beam okay and so I'm gonna change its color to that light teal color and then we'll be able to take a look at that and that's good enough for now we can always change the size a little bit later so that makes it really easy to see so we're gonna rename this projectile and we can drag this into the prefabs folder as projectile and then we can actually delete it right there and then now our player needs to have inside of the player code this script but actually not just in this specific player I want this in the general prefab of player that we've created so I'm going to go back to my scripts make sure that this is added in and this is actually in the prefab so that it'll auto update in here and actually we can remove the first so you could have removed the original player script and just make sure it's it's in the prefab okay so if I go to my prefabs and in player I have my player script I can drag in what is the projectile well we need to drag in the projectile prefab so we grab it and you drag in into the projectile now the projectile clone will use the code to copy projectile and create a new one in that exact spot so right now I'm adding in and you can see over here we're adding in a bunch as we go now they don't move and they're all stuck if I go in the scene view I could probably move a few off of here I move Mahler on the screen okay so what we need to do now is make some movement for our projectile and we need to locate it not at 0-0 so now we need to make sure that it's located that the projectile gets located at the same location similar location to our player okay and so our player needs to have his own game object storage player so we can reference player and say we want to be located you know by the player okay so in our player prefab so if we save before we do any changes and we drag in and now we drag in our player into our game object so it's it's basically checking for itself anything that any time we call player will be referencing that so if we do player player dot transform dot position X reference our player transform their Y dot and then but add 0.8 F and then the Z value stays the same and we'll do have the rotation the player dot transform rotation so the rotation is the same oh woops you need to make sure that it's dot position dot Y okay now we run it and we have our projectiles added in right above it let's make it a little bit closer to it let's do six okay now we want to have our new script for our projectiles to move and so we can go into our scripts create c-sharp script projectile okay and so let's open up the projectile script and so our projectile is going to move at a constant speed based off of time so we can just do the transform dot translate new vector3 nun on the x value the Y value will be do 3 times time dot Delta time because we want it to be each frame we want it to move and then semicolon here if you want this to be negative three but if you go to your prefab projectile and open up this projectile inspector prefab window and you click once on script and you just click once and drag your project projectile c-sharp script inside of the projectile prefab and the inspector now when you run it you will have projectiles that fire now they never get destroyed you can see they'll go on forever so we need to have a way to destroy them and do a few other things so we only gonna worry about that quite yet though okay so we have projectiles and I don't think they go quite fast enough so I'm gonna change this change it a negative five for now and we'll see if we need to change it later but next thing I want to do is add in a the enemies and so we go to back to our sprites folder so I've even downloaded sprites that looked like this and I've edited the background and I've drug in the Space Invader sprite different colors and things like that you're welcome to do that I might take a little bit more time a little bit easier just to download premade sprites especially when they also have animation it just makes it a little bit more fun okay so I'm gonna I'm going to duplicate these and add and have them just be each unit so I'm actually going to scale these down to 2x2 and I'm going to duplicate and if you hold ctrl down you'll see that you can if you see the x value watch it here you can move it and select it and it'll move every quarter of a unit so that way it can snap to those positions and I can quickly and easily just make eight little enemy ships here and then I can control space and I can do control and I did control D to duplicate each one and then if I hold ctrl down and move it I can move them down and have them be perfectly lined up and makes it nice and easy now if you go ahead and you create and if it's under something for now that's alright we can move it out so you want to move this game object out and not have a beat you don't have B on its own okay and we'll rename this game object our enemies and that way we can click on all of these and actually I like to do control a that'll grab everything and then unhighlight holding ctrl down and clicking and now unhighlight unhighlight the main camera in the sample scene then i can let go of everything and drag and drag them under the enemies now if I drop if I get rid of the drop down it will it'll get rid of that for me and really I wanted the this prefab to be located in here so I'm gonna drag this prefab in I know that's a little different than how we did it with a player and I probably should have done it this the other way but that'll that should be fine for now and so I'm just gonna call it enemy and if I need a re reload the prefab and re save it in here that's fine we can do that later but for now this will work these enemies will move and bounce back and forth and they'll they'll come down the screen and so we can go ahead and move our player all the way back to start they don't get hit by the projectiles quite yet but they will and so what we're going to need to do is we're going to need to create a box Collider 2d make sure it's 2d and a rigidbody so type of rigid body 2d except we are not since it's a 2d game we will not have gravity involved and because we're going up it's not a platformer or anything and so the same thing we're going to do for our enemies prefab and so our enemies prefab it are going to need a rigidbody zero and they're going to need a box Collider or they could actually have a polygon Collider but because our projectiles are gonna be fired straight it shouldn't really matter whether you do a polygon or a box and we can we should be able to see if this works correctly that when we fire and we actually hit somebody we should move them and you can see that's pretty cool huh I can just bounce around in there and so we don't want that necessarily to happen but at least we're detecting the projectiles right now and so what we're going to do is when our projectile collides with an enemy on collision enter to D so if collision dot game object dot tag equals equals enemy so we're going to need to create a tag for enemy objects and we're gonna need to give them all in their prefab a tag of enemy and we could say if that happens we want to destroy what we collided into which was our enemy so we're gonna destroy that game object and we will also destroy ourselves so you can do self and there's different ways that you can have things set up so that you can actually access ourselves but we're gonna go ahead and just create a public game object self and we can have that actually I'm going to call it projectile I'm gonna call it projectile down here just to make sure that even if anybody that came into our code would really know exactly what this is so we need to assign the projectile code now has a public projectile object and so it's going to be itself and so when we collide with a game object with the tag of enemy so we need to have our enemy prefab have a tag so we go into our tag and we're gonna add a tag and the list is empty so we're gonna add a enemy tag that we save and you click back and click back into tag and assign the prefab as enemy now that the prefab is assigned as an enemy and if we move and hit a player we remove them from the world so you can see you can see how that's working for us okay um excellent so moving forward so so in the real space invaders and you can decide if you want to do it this way or not you can only create one projectile into the world at once so that you have to fire the projectile it has to hit something before you can create another one so if you want to do that you need to do an ant and projectile must not must equal null meaning it must equal nothing there must not be a projectile that exists and actually that's going to be projectile clone and that cannot exist so then once we push space down we will make it and it will exist and then as it exists and lives in the world until it gets destroyed we will not be able to create another one because the projectile cloned will not equal null it will equal the projectile on that we're creating okay so then if if we have that then we can only do one at a time okay um so there's a variety of ways you can have your enemies move we have our enemies move and we can have boundaries that we hit you can have it on a timer you can do a few different things you can have just an animate but I want it to look as close as I can to the original space invaders alright so what we're going to need to do is create a script for our enemy and so if we go into our scripts folder we're gonna create c-sharp script and this can just be enemy and our enemy is going to constantly be be counting up and only move over certain periods of time so we're going to create a float that's a timer will start at equal to zero and we're gonna have a float that is a time to move and that's going to equal zero point five F and so our timer is going to add up based off time Delta time and then we're going to say if our timer is greater than time to move we are going to transform dot translate we're going to do a new vector3 0.1 F zero zero okay so it should move about point one to the right we go to our enemy's prefab and we grab the script and we drag it into our prefab here we can drag it to the bottom easiest way is right there component and we have our enemy scripts and our enemy script says we're going to be moving to the left on a certain timer or excuse me move to the right they're left on a certain timer and then we're going to need a reset our timer to equal zero so that it resets okay this will have a little bit more of a stutter effect it'll be a weight and move weight and move similar to the original space invaders and we can make changes to that as we go okay so um we can increase this if we want or increase the speed time to move decrease it so it goes faster as well so what we need to do is is find a way to time this out so that it it will move back to left at the correct time so based off of what we have here we're removing point one to the right so if we hold ctrl down we can and then go over we can actually count how many shifts if we were going point two five units over each time so 1 2 3 4 6 7 8 9 10 11 12 13 14 about 14 so if we did this 14 times and moved it over 0.25 which is what I think I'm gonna switch it to because I would like to move it a little bit more um it would take 14 shifts in order for us to go down to the next line and so we're going to have a counter that counts float num of movements we're going to start it equal to zero and that actually isn't going to be a float since we're counting it's going to be an int and we're gonna have our num of movements to add up one and we're going to say if the non of movements is equal to 14 which is what we said we are going to move down one so we can copy paste and we can actually put this at probably 0.5 F we can see if that's the right amount to move it down and then we can set number of movements equal to zero and we can make this have a float speed which will equal the 0.25 F and we can replace that with speed and then if it equals 14 we can have speed equal negative speed so it starts moving the other way and number of movements will go back to zero and it'll go 14 movements the other way ok so let's see how that works so if you save ok we'll see if it lines up pretty good 3 4 5 6 7 8 9 10 13 14 and it's moving the wrong way okay other way otherwise it was pretty good so we want to make sure that it goes down closer and I just want to make this negative 1 move down let's see how many so I'm at 1 2 3 4 about 5 before you're running into the player which I think is okay see if it bounces back the other way and it does so what we can do here is say and ant our number of movements cannot equal does not equal 14 so that when it does equal 14 and it does move down it will not keep moving sideways as it has with that last diagonal and I'm actually going to set this so when we reset we go back to negative 1 this will make it so that on this last control space when we shift back going back to the left we will go one more over so it really reaches all the way to the edge and we'll just kind of experiment and see how that works so now we move down and it looks pretty good let's assume just less than 14 so once it's on 14 on 13 it'll move but once it's on 14 it'll skip it and we can switch sides so then if you make your timer equal 0 so it resets the timer so it doesn't move right at the very end one last time now we have our enemies moving and we have moving at a speed and now we have these different variables that we can control if we change time to move to something smaller they move a quicker tempo if we had a the number of movements they could they can move wider if we wanted to extend the size of our world or the speed at which the the distance at which they moved that's kind of what the speed is essentially so a lot of things we can do there so all right now let's go ahead and have our enemies fire back at us so in order to do that we can do void fire enemy projectile just so we have the difference between the two and so we are going to say if a random number range zero f comma we'll say 500 F so we're getting a random number between zero random decimal number between 0 and 500 and so if that happens to be less than 1 basically between 0.0 0.9 repeating we will do something very similar that we've already done so we're gonna go ahead and copy and paste copy we're going to make a few changes here but paste and but we're gonna actually use some of the same variables so public game object and we can still call it projectile I think it might be better to call it enemy projectile even though we are in there their code it still would be nice to to be really explicit on exactly what we want to have happen so game object enemy projectile clone by all means you can call them just projectile but and then we need to go back in here and do enemy projectile and enemy projectile clone and then of course the last thing we need to make public game object and this would be ourselves or the enemy and so I'm gonna switch these two and I mean like I said you could probably name that player even and know what what you're talking about but honestly it usually more often than not confuses you later down the road and it's just that didn't take long at all for me and so it's really worth the extra a little bit of time just to avoid confusion later now I'm subtracting the distance and now I don't know the exact distance I think the enemy is a little bit smaller and this is used so we are at the front of the enemy so in our player we added so we were at the front when we were firing a projectile and then here we're going down a little bit less and so I'm gonna copy and paste and really this doesn't matter doesn't exactly matter where it is in the code I think it's it's nice to have it near the bottom so we move first and then we fire projectile instead of firing our projectile and within the same frame moving so I think it's smartest to heaven probably at the bottom here and so control s and now that we're back in here we want to go to our prefabs and within unity we have this enemy script and we got a bunch of stuff we need to put in so the enemy needs to know where we're gonna add the projectile in relation to itself so that would be the enemy the enemy projectile we're going to need to make a enemy projectile prefab so I'm gonna make a duplicate of projectile I'll rename it enemy projectile you guessed it I can since it was the white block sprite originally we can change the look of it to be closer to red and then in our enemy and then in our enemy script we can drag in our enemy projectile and then that is how the clone will be made will copy the enemy projectile to create the end of a projectile clone so we should see oh now we know I actually know that it's working if you slowed it down you can see Oh projectiles barely popping out but they're injuring themselves because if you remember this has our projectile script which says if the projectile is running into a tag of enemy then it will destroy itself so it's really probably a little bit easier to remove this component add a new script and call it enemy projectile script so now we can copy most of the stuff that we've done and our projectile and yeah we're going to need to make some changes to it but not all that much and we can paste over so you make sure we get this right yeah my curly brackets are all lined up okay paste over and we can we can do a few things here so instead of having it be enemy it's gonna be if our if we hit the player will destroy the player and our vector we're going to be moving the opposite direction so that will be how that looks and it will destroy our enemy projectile when we hit the player and we're going to need to add in public game object enemy projectile so that'll be a self and so if we go in our prefabs and I'm a projectile doesn't have the script in there yet so we need to go in and add the enemy projectile script to our enemy projectile prefab the enemy projectile I need to make sure I save control us because that did not show up so now it will show up with that enemy projectile holder and we're gonna drag in or any project out prefab so that the script can read it as itself and so now if we collide with player we will destroy what we collide with which is player and will also destroy our enemy projectile so now we do need a box Collider oh we do have a box Collider on our enemy so they don't destroy the enemy anymore but we do have the box fighter cuz it copied it from the projectile and so here's the thing we know the hoes are never gonna move we never want them to move so we can do something in our rigidbody here and have some constraints and we can freeze all of these so we know we're never going to have those turn or anything like that so we can do that in our enemy and our rigidbody same thing with our player who's going to need a box Collider and a rigidbody 2d box Collider 2d and no gravity we're also going to give them constraints so that doesn't move and really you could do the same thing with your enemy projectile we never want any of these to turn or get shifted basically they're either going to remove themselves and remove something else from the world we need to transform negative apparently maybe we're facing a different way than I expected but regardless now we're facing down and we're moving and nothing's hurting us nothing there nothing's running into us so we need to have our player get injured or removed if we run into the project out the reason that's not working is our player is not tagged as player so if we this has already been pre-made as one of the default tags that they have so we'll have player in there and that should cause our boom to hit us and were destroyed okay so what I'm going to go ahead and do is I'm going to make a new sprite light block and I'm gonna drag it in up here and I'm gonna have it be quite a bit larger than this even okay there we go and I'll have it be black so it blends in in case we wanted to move it or but I'm gonna move it right on the edge so right when we hit the edge our projectile will be destroyed police that's the goal renamed and I've seen sure okay so um when we hit the edge of the screen add to the scene we are going to get removed and so we can name this something like respawn so our projectile can respawn or finish it you know it doesn't matter what we name it there we could add our own tag in but we will have our own projectile so the regular projectile if it hits not enemy tag but we said finish we will destroy our self which is the projectile okay now we go into our edge of scene and we make a box Collider and we make a rigidbody 2d and we freeze our x and y and that actually will stop gravity from doing anything to us and so now even if we push to the edge of the screen we can still create another projectile later on because it gets destroyed you do I challenge you to try to do the same thing for yourself for the enemies at the bottom of the screen but I will do that as well now so I created as you've seen I'll rename it to bottom of scene and it will have some of the same properties and we will actually be able to since our enemy projectiles move the same way they move in opposite directions as our projectiles we can also have that same code added in into our enemy projectile class and they won't trip over each other and there won't be any problems okay but instead of destroying the regular projectile we're gonna destroy the enemy projectile okay so now both both directions something will get destroyed and we could just check on that so if I go into the scene yeah they're getting destroyed right there if I go into the game that's getting destroyed right there excellent okay so I'm just gonna cosmetics change this to black so it matches everything else I don't really want anything to take away from the look of the game and so I'm also going to extend the effects width so if you go down to the star effects the width right now is is just that large in order to increase the size of that you would need to go to the shape and the radius we would just extend just a little bit so like 0.3 and that'll be right outside of it so perfect all right so now our so now our particle effect our stars will spread across the entire screen and really come down on a nice nice look and our color background is black so that we can actually move around and when to fire the projectiles are quite a bit too fast too probably too strong but you can decide on the speed at which you want to do it but I'm gonna increase mine for my enemies and that would be this random range I'm gonna go ahead and do 750 here give it a little bit more less likely that it'll happen okay now what I want to do is make multiple law and so what we're gonna have happen is we're gonna have if our player gets hit we're gonna pause the game and not let anyone move anymore and we're gonna add in a new player up here but our lives will go down our number of life so the first thing with that we need to do is create a empty game object that we are gonna call game manager and this is where we manage some of the the overall gameplay and so this will not have any individual statistics for any one of these objects or and it won't even be added in and and visually be there but it is just the general background information that will always be in our game such as lives the ability to pause the game as well as a few other things so our game manager is going to need a script or create c-sharp script game manager and this is a common script so it'll even change the icon of the c-sharp script but don't be confused it is simply just a regular C sharp script you can double click on it and open up game manager and some things that we can do with the game manager we'll be able to save our lives and since it is always in the game it is nice that we can have a number of lives that is always there for us so we are going to have that and we're gonna have a public static bull play game okay and so we're start with that being false so since this is static if we do game manager dot anywhere in any of these classes we can access this play game okay and so if our player gets hit by a projectile so if we go actually into our enemy of projectile and we hit the enemy projectile hits the player I can do game manager dot play game equals false so we pause and freeze the game so in order to pause the game we're gonna need to add some stuff to our enemy and we're gonna need to say that this game manager dot play must be true in order for the enemy to move or fire okay so in our if statement you can add if game manager dot play game equals equals true or you could just do if game manager dot play game and that will automatically check if it's true we can encapsulate all of this and say that only things in if in the update method will happen if game manager dot play game is true okay so if we save that we can play and now play game is not true so everything is paused because play game starts out as false so if we start it out at true and then when we get hit it should pause and the enemy should stop moving so they move and they fire but then when they get hit we get destroyed but everything pauses okay the enemies won't move the enemies won't fire okay because that's what happens in the space invaders game when you get hit and you get a new life you actually get some time to reset up and refire your position so now the problem is is that we only have one life our player actually only we'll get destroyed so we're actually going to remove that and we're going to relocate it back to the original position okay so gaint collision game object dot transform dot position equals and we're gonna need to give it a new vector and so we want this to be vector three respawn just so if we want to change where the respawn location is we'll get a new vector three and we're going to put it at seven four zero for the Z value and so this will relocate it to this exact spot on the map and we're gonna have trans transform equal respawn and so that should reposition the player not translate it which would just move it from where originally was we're gonna set the new position to the respawn point so when we run it we get hit and then it's the enemy's freeze now usually when you hit a new enemy then the game will start playing again so even if you miss over here it shouldn't start playing but if you hit an enemy it will play again and so we need to put in our projectile when we hit an enemy we want the game manager dot play game to equal true so it plays again so we hit we get relocated but then when we fire and miss nothing happens but if we hit will start moving again then if we get destroyed we won't move for then we'll move again okay so now we want to add in a way to show our lives and to have our lives decrease every time we get hit and only have a limited number of lives in order to lose or and then if you destroy all the enemies will win the game okay so if we go in and add a new UI text this can be renamed lives and we can double click and this will give us our UI screen and this is where our text will show up and so if we move this up into the upper left we can edit what's on lives here start off at however many you wanna give us but three and we'll make this a little bit brighter so we can see and we can actually move this just a little bit over so we can see it a little bit easier okay and you can increase the size a little bit if you'd like yeah I'll leave it like that that looks good okay so now we need to have our lives change and decrease as we go down so in order to have our lives count down we're gonna need to go into our game manager and there's a few different ways you can do this but we can make lives static and we can say that if we are fired upon our game manager dot lives - - that will allow us to subtract our lives every time we get hit and we will be relocated and it will pause the game okay and then in order for our lives to show up we need to go into our game manager we'll need to do public text and lives and it's got to be something different than live so lives score or lives text I'm gonna be lives text and then you can drag in this text into your game manager and if we save we can go into our game manager and then drag lives text right in there okay and so now our game manager will adjust based off what lives is what we need to do now is have our lives text lives text dot text so the text in lives text will equal our lives : space and then we need to add a variable to it fives and so our lives equal will equal three but when we get hit this lives static number will go down so if we control us and say control us and save that will make this go down every time we get hit okay and then we need to also not only have it at the start we copy and paste that and have it in the update so that live text is always being updated constantly and in our lives text we can actually get rid of this and it will always be updating in our code so when we run this again at the start live text will equal three because that's what we started at as but then because we have it in the update it will always update with it and go along with it now the problem is if we hit and get hit we can get to negative one we don't want that okay so we can go in so in our update we can say if our lives equals equals zero we're gonna have an end screen text so and screen dot text equals you lose okay and then if we go in it's a public text end screen and we create a new end screen text so we add in another text right click UI text and have it be a part of the canvas canvas automatically will load up and we want that to be our end screen text and that can be a you lose or a you win we can have that be a little bit larger and have it centered we'll have it be blank to start so nothing will we'll display at the beginning and we want our end screen text to not be great because that's our color and let's just go ahead and make a different color than white like the lives just to change it up okay so now that we've made the end screen text we need to go back into our game manager and drag end screen into its placeholder and if we play we can lose game and you lose will show up as your end screen now we also need to make some adjustments so we can't keep playing the game because right now we can and so so we can go into our player code and we could say if lives is greater than zero we will move and fire be able to fire projectiles and that will allow us now it's not going to be just lives it has to be game manager dot lives I'll allow so we can't move or fire any more projectiles when our score gets to zero and we can lose the game okay and now I can't move I can't fire my enemies pause as well so this is where I'm going to stop the tutorial I challenge you guys to go ahead and move forward and try to create a you win screen based off if you have destroyed all the objects you can either search through unity and maybe look up online how to find out if there's any more enemy prefabs left in the scene you also could have you lose the game if the enemies reach the bottom of the screen but I hope you guys enjoyed the video and as always if you felt like it was very helpful I appreciate a like and subscribe so you don't miss the next video thanks for watching
Info
Channel: Tanner Crow
Views: 13,372
Rating: undefined out of 5
Keywords: unity 2d tutorial, unity 2d Space Invaders, Unity 2D Space Shooter, Unity 2D Tutorial Space Invaders, Fire Projectile Unity, Space war game unity, how to fire projectile in unity, how to make a space shooter in unity, how to make space invaders in unity, Unity 2D fire projectile tutorial, unity game development, unity 2D for beginners, beginner unity tutorial
Id: 2mNXfrh0UYo
Channel Id: undefined
Length: 66min 15sec (3975 seconds)
Published: Tue May 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.