How to make a Tower Defense Game (E14 LASER BEAMER) - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video is brought to you by our own Metta thank you so much for donating if you want to support breakage yourself you can go to patreon.com/scishow jeez thanks for tuning in at brac ease hello I want to welcome to this video in creating a tower defense game in unity today we'll be adding a laser beam er to our game so it's always a lot of fun to play around lasers and today we'll just be importing the our model for the chart will also be setting on the line renderer that will give that laser we feel whenever we shoot stuff and then in an upcoming video because it was a bit too much to squeeze into just one video will also be slowing down enemies whenever they're hit so that will allow for more interesting gameplay mechanics and give the user more abilities to create interesting are kind of defensive setups so that is what we're going to be doing today and without further ado let's just jump right into it so as you can see I'm pure in unity and I really want to begin with just importing the models so let's minimize unity here and head on over and open the tower defense assets pack and you can of course download this for free and that's the link for that in the description so let's just let's jump right into our models folder under our laser Beamer and you can see that's both a blend file here in case you have blender installed and if you do not we'll just use the FBX as we have done the other time so let's just use that one and drag it into unity but before we do that let's create a folder here on our imports and let's call that laser beam erm just to stay nicely organized we can drag the FBX in there instead and you will of course see that it creates a laser beam object which we will put a scale factor of 0.5 on and hit apply and then we have a materials folder with all the different materials that will just be modifying a tiny bit here in a second so now let's take our laser beam er and drag it into the scene and let's reset the transform if it isn't already and hit F to focus in on that just shift it back to perspective view there let's have a look in the scene view here how it looks in game and I think it looks just fine however there are some things here that we need to tweak so the first thing is that we want this to be in one word ah that's just preference of mine whenever dealing with stuff inside of unity and then I want to rename these things here so this one here is the base that is what we call the other ones and this is going to be the head awesome then we can change the materials for to make this look a bit better so let's find our bolts here I actually want them to be black you can of course choose whatever you want here and I'm going to bump up the metallic Ness on those and bump down the smoothness a bit on there we go actually make them a bit darker a bit more metallic I guess that will actually Dargan them and then we can take the base here because I think and that is the stand here let's rename that to base as well so we don't get confused and I think this is a bit too blue so let's actually make it more over in the gray area here a bit more metallic take down the smoothness just a tad and maybe even lighten it up a bit here so something like that is a lot better because I wanted to kind of get in a be in line with the other arm turret stance so that's fine I do want it to be a tiny bit more blue then we can take our main turret which is this part up here and actually really like this the feel of this I just want it to be more metallic which means that it will be darkened then therefore we have to brighten it up in our colour and then I think we can maybe take our smoothness and just doubt that down just a tad and I really like the feel of this and then we can find our third accent here which is this a yellow colour and we can definitely go with the yellow but I think we just need to dial it down just a tiny bit um for this to look a bit better something like that and it's just small tweaks here and there finally we have the pipe I think we can make that pop even more so let's just give that even more of a purple colour a tiny bit of metallic Ness and then definitely bump down the smoothness here and I think that looks pretty cool definitely looks laser II maybe even toxic so that's fine and on what we can do now is just I actually apply our turret script so let's apply our turret here and we'll give this a range of say 20 and we won't mess with the fire rate that's not going to matter because we're going to be creating our own little system for dealing with armed lasers because they are kind of an ongoing thing and we have the enemy attack we'll just leave that and then we need to add a pot to rotate so let's just right-click create a new empty object and reset that and call this one our part to rotate and let's drag this under our head and then reset the transform to that drag it out again and that will just Center it kind of where it needs to be which is here and now of course we need to have our blue z arrow point in the direction of our head so we've done this multiple times and we're just making sure that when we rotate this it will actually align with the z axis being forward so that we don't have to do any messing around with any rotation since I have the script we just do that in here which is much more visual so make sure it says pivot pivot and local up here and then we can maybe zero DX on the X and um zero the coordinate on the X and then us put a 90 that will have to be a negative 90 on the Y and let's also put a 1 1 1 scale I think that's going to be better and then we can take our head here and drag that under the pot to rate rotate and we can just say continue here and now we should be see that our pot to rotate points in the correct direction and we can just easily move this part around awesome so now we can reference that in here as well so let's drag our pot to rotate into the pot to rotate field and finally we need a bullet prefab except we're not going to be using bullets so we'll ignore that as well however we do want a fire point um so we'll have some place where our laser will origin so and we are just going to create that as we've always done on the d-pad to rotate rotate so let's create an empty there again reset it and we'll name this our fire point and we will make sure again to position this correctly so let's view it from this side I'd switch to isometric view and we can just drag that up there and I want it to be pretty close now because we're not actually instantiating anything here we're just kind of putting a point for the line renderer so we can get really close and um it's actually centered from the front here I believe already that's awesome yeah great cool so now we can drag this in select the line the laser beam er and drag the fire point on to the correct slot and there we go so I believe that was all we needed to set up on on this object so far so let's actually go out and turn this into prefab now so let's go into prefabs and drag that in there the entire laser Beamer and we'll just have it sit here for a moment so that we can easily change things if we want to so the next thing is kind of adding this to our build menu so that we can actually put it in the game as the game is running so and to do this again we find our overlay canvas under sharp and then we see that we have a standard chart and a missile launcher item and I will really wants me to spend money these days so I'll just dismiss that the only thing that's running out is my patience the hell from there we go so I'll duplicate our missile launcher item here and I will call this one armed laser Beamer and we'll actually do laser beam a item and we need to change the icon and we'll do that in just a second but let's also change the cost text down here and I'll change that to say 350 dollars to make it very expensive which means we'll have to make it powerful as well and we need to go into the shop and change this function here and I believe we can just go ahead and do that right now so let's double click on the shop to be taking over here and let's double click that to open it up in Visual Studio and all we need to do here is first add a new turret blueprint so we'll go public chart blueprint and this is going to be our laser beam err and we'll add a new function for this as well so we'll call this one deselect lace Bheema and it's going to say laser beam are selected there we go and we are going to select the turret to build and we're going to pass in our laser beam are of course awesome and one thing that I actually noticed is that under our build manager if we go to a game master and find a build manager here you can see that we still have a standard turret and a missile launcher prefab referenced but we're not using those in any way so let's just double click on that and go in here and remove those two fields there's really no reason for those to sit there because we are passing them along on inside of the turret blueprint so we can just remove those and you can see that we should still be error free down here awesome so now that we've added those we can find our shop and we can find our laser beam err and we can set this up so let's set the cost to 350 and let's also find our prefab and drag that in there cool so I believe that was kind of all we needed to setup in terms of the shop here except of course we need to add an icon so let's have a look at importing on one of those then again I inside of the arm inside of the tower defense assets pack I have a sprites folder and I've included a laser beam err icon dot P&G that's also a Photoshop file if you use Photoshop again I'm just going to import the PNG here and let's find our um icons folder and let's drag that right in there I believe the other ones we imported were PNG as well let me just check for that because I like to use the same actually the first one was a Photoshop we'll just do a PNG here it really doesn't matter on my system so you can see here that the texture type is going to set to texture we need to change that to 2d and UI and the sorting here or the Alpha Channel should be sorted out and I believe that's all we need to change except we can maybe on change this to 512 if we have our CD and then we can make this to color um there we go just to make it display really beaut and then we can change that inside of the our missile launcher icon here change that to the laser beam err so you can see that setup inside of your unity scene and I actually think that looks fairly decent good okay things are moving along I actually wanna maybe make it a tiny bit Baker um actually want to make it maybe 120 down here I think that's better and that looks really really cool so now we should be able to just hit play and select the laser beam err and it should say boobs that's something here we haven't assigned oh that's on a laser beam maybe we haven't assigned a bullet prefab for now just what testing will go in here and just assign a normal bullet one we'll just do a bullet there we go just that we won't get any errors so we can select here our missile or it says missile launcher selected that's because we have to find our laser beam err item now we go into the shop and we have to change the on click event so we've actually added the function but we haven't changed this event so it still points to the Select missile launcher function so we just need to go under shop and change that to select laser Beamer and we should be all right so let's try and hit play now clicking there laser beam are selected and clicking somewhere in the scene and you can see it appears and we can only purchase one because we actually don't have that much money when starting up cool so now I think it's time to add in a line renderer to do some actually lasering laser ring laser thing so let's do that so let's create a we could either actually put it just in here or we could put it as a separate object I think we're just going to put it on the road but you can do anything you want so let's just put it here and um we're not going to be casting shadows we're not going to be receiving shadows motion vectors will just leave on will have one material which will specify in a moment let's just set up some temporary positions here that will allow us to view what this looks like so let's put a y-value of three and a why're your three down here and then as the value of maybe five just so we can see kind of what is going on actually we could put it out here but I think this is going to be fine good so this is what our line currently looks like not too awesome so let's go to um excuse me I think I'll have to take a sip of water here that's better all right so let's go into our parameters and let's uh put our start with two point three and our end with two point three as well cool that is definitely better and then we can go inside of our know our positions are fine we can go and find an actual material so let's create a new one inside the materials folder let's create a new material and let's call this one laser let's then go back to a laser beam er and drag the laser in there and we can go down here change the shader to something like unlit color maybe or you could go particles are alpha blended actually I think I'm going to go alpha blended and um you can see that we can mess around with the color in here so that's what we're going to be doing so let's take our start color here and change that to some kind of orangey thing let's take our end color actually I want to switch that around um this one needs to be a bit actually let's do the first one red and the other one yeah let's do that so this one will be kind of bright almost yellow and this one is going to be kind of red to give that cool-looking effect there and we can maybe make the first one here a bit transparent as well and this is where I want to go in to our material and bump this up a bit and also bump this up a bit something like that looks a bit better and there we go I actually think that looks kind of nice I'm looking inside of the game we view here just to get an idea of how that looks and you can definitely go ahead and add more effects around this one thing that I do recommend is that you add the tip of the delay to add some kind of party system that you toggle on and off in order to give a cool impression that the lace is actually hitting the object and there's sparks bouncing off and stuff like that it's that if that's something you really want to see me do leave that in the comment and I'll include it in the next video for slowing down the enemies and stuff like that then we'll just go particle crazy which you know I love it's not really not that I don't want to do it so please leave a comment okay so we'll continue the video here so we've set up these parameters and are we actually ready to go ahead and make this interactive through the script so let's apply this and it don't worry about this being looking weird like this we can just delete our laser beam err should be totally fine and then we can um find the scripts called chart there we go so let's double-click our third script and we'll move things around a bit here and to organize them for what we want to do so currently we have these apps your boots I want to call this general and I pretty much only want our range to being here because the next thing that I want to do is have one cold on use bullets and this is going to be the default and in here we have stuff for bullets we have the fire rate the fire countdown we also have the bullet prefab awesome so we'll just put all of those in there and then we'll create a new header down here which is going to be armed use of course laser and we'll remove the default there and to include the laser what we'll do is we'll have a boolean that on the arm that we can check inside of the editor if we want the chart to use a laser instead so we'll just do a public poll used later and we'll set that equal to false by default awesome and then in here we can also have other stuff like a reference to a line render so let's do a public line renderer and we'll just do line renderer by the way in case you missed on the recent brackets live stream it's on the YouTube channel as a reupload so you can check that out we created on the curve fever the snake-like game and in there we do a lot of stuff with the line renderer and we do that in unity 5.5 which is currently in public beta but will probably soon be released for real um and there's so much new stuff done to the line renderer it looks so much better so if you think this is looking kind of not that great I'll go check that out and play around with the features there there's so much stuff to make it look a lot better and especially with particles as well so that's a lot of fun and you can check that out just wanted to mention that so we have the public line renderer here and let's actually put this to use and just to give you an idea of what we just did let me just find our prefabs and then find our laser beam in here and you can see that this is now much more nicely grouped and we'll just for the laser beam err here we'll have to check the you use laser and we'll drag a line renderer right in there so if it's sitting on another object you can just drag in that object and it will select the line renderer for you so that should take care of all of that and we can just remove the bullet prefab here not that it matters I it's just not the one that we're using cool by the way I'll just throw in one more point before continuing if you want to set this up even more nicely so that you have a variable called used bullets or maybe even an enumerator to select either bullets or laser and then it will show fields depending on what you've selected then i really suggest creating an editor script and unity is so easily extendable with those it's just too much for this video that simply switches between displaying those that's super easy to do and you can just look up unity custom editor good so that was all so now we can continue so let's find the place here where we update stuff so in here we check if our target is equal to null and if it isn't meaning that we have a target we lock on to it and then we handle some shooting so let's take all of this logon code and move into separate function we should have done this a long time ago so let's call this one lock on target and we'll move that down here so board lock on target and we'll just paste in that entire code blog in there and um then we can put an if statement here saying if used laser then we want to laser stuff whoops not used laser we want to create a new function called lace that's really annoying let's just create the function here instead of getting around visual studios autocomplete so we'll have a void laser down here and now we should be able to title type it up here so laser and um if not so if we're using bullets we can go ahead and do all this fire countdown shooting stuff awesome and you can of course put that into a separate function as well but since we already have a shoot function I don't really think that's necessary um cool so um and I can see that I've pressed insert there just needed to get rid of that so inside of our laser what we can now do is say line renderer dot and then we can set the positions of the different points that we want our line renderer to go through so again if you haven't seen the livestream and I cover this in there there's the positions array here meaning on each of these elements here in the array is a point that our line renderer will go through so we can actually create more if you want kind of a bending laser if you want one that sticks ax or whatever you can definitely do that but for now we'll just create a symbol line and we'll do that by simply setting the first element and the second element in the array so we'll just do line renderer that's set position and we do that using an index so in our case the first element is index that's zero and giving it a position and that's going to be our own position but we want not to use that we want to use our fire point fire point up transform dot actually it's it's a transform so we can just do fire point up position and we want to set the second one so line renderer dot set position one to our targets position so target dot position there we go so that actually does it for us and we should see this working already so if we go in here if we hit play and place down our turret we can see that that works however you will want to also notice that when our enemy here gets out of range the line renderer does not disappear so what we have to do in order to get that working is just using the fact that up here we're checking whether or not we have a target so let's just make this a bit bigger here so let's say if target is equal to null we definitely want to return but we want to do some stuff before that and that is just checking if line renderer actually let's first check if our used laser is true so you only want to do this kind of stuff if we're actually using a laser and in that case we can say if line renderer actually we don't even need to do that if statement we could do that if statement ah we'll do it if line renderer dud arm enabled then we want to disable it so we can just do line renderer dot enabled equals false there we go and if you can of course put that inside the audio statement that's what oh but I think this is easier to read and then on down here we can simply say that if line renderer dot enabled and then put an exclamation mark so if it's not enabled we'll do line renderer dot enabled equals true there we go so this way we easily switch it on and off and we only do it once even though we are doing all of this stuff inside the update loop so if we now hit play we should see that when we spawn this in we no longer have a line up here and that on there's no line until we actually get a target and as soon as he's out of range our line renderer just again disappear so that is working just the way we wanted to and um yeah again for the next video we'll be slowing down the opponents if you want me to add some damage or with time let me know if you want me to add more particles or more effects let me know I would love to do that and yeah so that's pretty much all I wanted to show for this video I hope you enjoyed it and I will see you in the next one thank you so much to all of the awesome people who donated in September and especially thanks to our own Metta Robert Roach James Calhoun Kim Rose cuccos Bao and Antoine and Dickson P thank you so much for donating you guys are awesome if you want to support practice yourself you could go to patreon.com/crashcourse
Info
Channel: Brackeys
Views: 91,648
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, model, texture, models, textures, material, materials, beginner, easy, how, to, learn, course, series, tutorials, workflow, game, development, develop, games, programming, coding, basic, basics, C#, laser, beam, turret, tower, defense, defence, asset, assets
Id: 5vw7RYxvqeE
Channel Id: undefined
Length: 25min 8sec (1508 seconds)
Published: Wed Oct 12 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.