Weapon Generator inspired by Borderlands using Unity.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Very well explained. Thanks!

👍︎︎ 1 👤︎︎ u/Xitech1 📅︎︎ Jun 10 2020 🗫︎ replies

Been looking for a tutorial about this today. This is good stuff and very in-depth.

👍︎︎ 1 👤︎︎ u/Yamil01 📅︎︎ Jun 10 2020 🗫︎ replies
👍︎︎ 1 👤︎︎ u/gamedevluuk 📅︎︎ Jun 19 2020 🗫︎ replies
Captions
[Music] everybody this is game that's Luke and welcome to my very first tutorial series in this series we're gonna look at the borderlands inspired weapon generator the borderlands known for making a gazillion guns so this is gonna be a part 1 of a series probably we're gonna make around 4 parts in total this part 1 let's start by diving right into our design and joy first thing we need to do is identify parts that can be swapped like barrel for example so we want to have modular parts if you take a look at this here I have authorized parts of the gun I think our modular and this way we have 6 different parts and if we change these with our script with our algorithm then we can generate unique looking guns so if we take these modular parts and we give all of these parts a name then you have the following we have a barrel over here a magazine a handle stock scope and a body so that is how the algorithm will work it will just pick a random part from the list that we have so let's say we have a list of six barrels six bodies six scopes 6.6 and six magazines and the system is just gonna randomly pick one of each and then try to snap them together you can generate a lot of unique looking weapons so what's very important for our system is that we're gonna use a system like this and here you can see that you have the body and the body has sockets and these sockets will be empty game objects in unity and in these suckers we will spawn the designated parts so let's say we have the socket over here this is gonna be the barrel socket and in code we're just gonna spawn the barrel at this socket and we're gonna do it for the rest as well because everybody can be unique in size and shape we have to set this up for everybody that we have so if we have six different bodies then we need to make six different prefabs and unity and set these sockets by hands that's all so these are zahra sockets very important and then we have our pivots now we're looking at the other parts the parts that are being snapped on top of the centerpiece which is the body and as you can see here the pivots are in different position so for the barrel they're on the left side because the barrel needs to snap to the body at his left side the same goes for the rib and the magazine they are snaps at the bottom of the body so their pivot needs to be at the top stuck here on the left will be snapped on the left part of the body so its pivot needs to be on the right side finally we have the scope Pope has to be snapped on top of the body so the its pivot needs to be at the bottom so once we have our body in place and we have our socket set up and we have the pivots in the correct positions then we can tell our script to start spawning or well to pick a random part from a list spawn it at the designated socket and then it should look like this but then each part should be snapped on top of the center piece which is the body in a correct way and then you have a unique looking gun if you look at this image it doesn't matter which kind of barrel I'm snapping at this socket because it's gonna be at the exact same position so it's always going to look okay now that we know our system I'm gonna go into Maya which is a 3d modeling package and as you can see here I've made two very simple looking guns it's just for demonstration purposes of course it has to look a lot better when you want to make a game out of this but the technique is in place we have a barrel we have a body we have some sort of scope we have a magazine we have a grip and we have a stock and the same goes for this one as well and if you were looking closely and you can see that I've already set up the pivots as well so the barrel as the pivot on the right side stock has to pivot on the left side so it can snap on the body like this and scope over here can snap like that the magazine can snap like this and the grip can snap like that so that's what I did in Maya I'm not going to show you how to model things in this tutorial but as you can see the shapes are really simple and that's your doable for you if you follow some 3d modeling tutorials I'm gonna export these and bring them to unity and then we're gonna start working on making our weapon generator in unity so we're in unity and as you can see I have my weapon parts model here and it has all these different types of weapon parts so what we need to do now is set up our bodies in the way that they have sockets so I'm gonna drag my models in here well they're still very simple going to break the prefab because we don't need this brief ever we're gonna make our own prefab so I'm gonna unpack the prefab I'm going to for the bodies this is body 1 and body 2 and I'm gonna make prefabs out of these first gonna create a folder prefabs I'm gonna drag body in here and body 2 in here and I'm opening body number 1 and this is just a simple looking block and now I'm gonna make empty game objects and if you use a unit shortcut you can use ctrl shift and made a new game object you can also just use rice right mouse button creates empty now we're simply gonna make the socket so this is going to be our barrel sockets okay paste this is gonna be our stock sockets go p--'s orbits magazine sockets and the grip sockets then we have our sockets and now it's a matter of positioning them well so the barrel socket needs to be at the front of the gun and in the middle in the middle and at the front now stock is end of the gun there we go let's go on top magazine it's gonna be at the bottom and then a little bit at the front and the grip is gonna be at the bottom and a little bit at the back oh this is our first body gonna go to the second and we're gonna do the same now we have our two bodies with sockets ready now we're gonna make a new scripts and we call our script the weapon generator and of course we are very organized so we're gonna put this in a folder called scripts almost there we go so what we're gonna do now is I'm gonna check if I am pressing the spacebar on my keyboards and then I'm gonna call the function calls generates weapon animate weapon and here we want to spawn our body to start with so I'm gonna make a new list of game objects and it's gonna be called body parts and here we're gonna get a random body from the list and instantiate it so getting a random item from a list is something we're gonna do for all our parts so I'm gonna write a separate function for this which is called get random parts it takes in a list because we want to give it a list and then it should get a random game objects from this list and it returns the game object as you can see here so we can get a random number which is random range from zero to the list that we gave that counts but this way it's gonna look in this list it's gonna get a number from zero to the max of this list and because this is an int it doesn't count the last one and then we're just simply returning a random number and I see I made a typo so what we have right now is we're getting a random number so if our list is too big then it's going to be 0 or 1 and it's going to return the list with 0 or 1 so we get a random part from the list so here we want to do gameobjects random body is get random parts and then we want to give our body parts list what we want to do then is we want to instantiate it the instantiated means we want to spawn this body into the unity world so what we want to spawn is our random body which we just got from our get random parts function and let's spawn it just at the 0 0 0 place in in the world save this and let's see if this works so far gonna make a new object here or add weapon generator and throw my new script on here it's gonna have a body parts list and I'm gonna throw my two prefabs in here on the press play and then I'm gonna press space and in the scene we see that it spawns a body clone let's press space a couple of times another body body body - so now it's generated another body and if I press a couple times you see that it's pretty random so that's exactly what we need so the next step is we want to spawn the other parts as well after spawning the body we want to spawn a barrel scope a stock and a magazine and a handle we could copy-paste this now we have six parts so I'm gonna copy pasted six times and we're gonna make more or less because we need to have a list for each parts we want to randomize this is gonna be barrel parts this is gonna be stock parts scope parts magazine parts rift parts now we're gonna replace this so this is gonna be arrow parts stock parts magazine parts skull parts and grip parts ran the barrel and then stock random scope and random grip copy paste heaven what we're getting there almost there all right so now instead of just the body responding a random barrel and we're using this function again to get random parts which we give a list and from this list it returns us a random game object and now we have to fill these lists of course and I'm gonna show a little trick here I'm gonna press the lock over here on the top right and now if I click another game object it still shows the weapon generator inspector here so what I can do now is you know highlight multiple objects here and then drag them in the list at once instead of going doing it one by one I can now just highlight multiple things and then throw it in a list immediately and it's a little sweet trick that save some work actually if these lists are getting big like if we choose six different barrels or a hundred different barrels you don't want to put them all one by one you can just do a trick like this all the lists are set up now let's press space and see if we got something well I see something that looks like a gun already and let's see we have body to barrel to stock to magazine to scope one and handle two and delete all of this and generate again body one barrel one stock two magazine - alright so things looking pretty okay unless you can see if I highlight everything you see that everything is like inside of each other and that's not something we want we want the barrel to be aligned to the body like this and stock to be aligned to the body like that and so on and for this we need to use our sockets which we already made on our body prefabs if you remember and look at this we have our stock is ready we somehow need to tell our script that the barrel needs to be spawned here instead of in the middle if you go back to our script you can see that we're telling the barrel right now to spawn at factor 3.0 which is the center point of our scene and that's something we don't want we want to spawn our barrel at the position of the barrel sockets so the first thing we're gonna do is we're gonna create a new script both weapon body and I'm gonna put this on our on our bodies one two then we're going to open weapon body and here I'm gonna make public transform or each sucker that I'm gonna make a public transform so we're going to barrel socket we're gonna have a scope sockets we're gonna have magazine sockets we're gonna have rip sockets stock sockets all right safe and I don't need to do anything with this I'm going to go back to my prefab so I'm going to open body and now I'm going to fill these empty transforms so the barrel socket I'm gonna interact the barrel socket in cope so good it's gonna be this one stuck sucker it's gonna be that one grip circle it's gonna be this one magazine this one now this grid knows which socket is switch I'll do it for body two as well and again yeah this is some work you have to do by hands but because everybody is unique this is the only stuff we actually have to do by hand and that's I think totally worth it at the end so now we've set up our sockets we're gonna go back set of spawning the barrel here we want to spawn the barrel that's our sockets so before we can do this we need to have a reference to the weapon body and the weapon body has a reference to the sockets so after spawning the body we wants to get the script weapon body from it so first we're gonna save this instantiate it body so now it's being safe the body that's being instantiated is now saved in this variable ins body so after it's it has spawned in the world we can get its weapon body in spotty get components weapon body so now that we have a reference to weapon body we also have reference to all the sockets as you can see here weapon body dot socket returns our sockets that we have now that we have this we could just to see if it works liquid try this okay instead of spawning effect at 3 0 maybe you should spawn at the barrel socket a position saving this going back and rating another gun and here we go as you can see our barrel is now being spawned at the front of the body and as you can see it does not align properly so we have to fix our socket which you will do in a bit but this is exactly what we want we want our barrel not to spawn here but we want to add to spawn where it just spawned so that's works but there is some stuff that we need we also want to set the parents because now if I generate my weapon you see that they're all here in the scene what you actually want to do is that you have your body and beneath the barrel socket you have your barrel this is much cleaner because well the barrel is located at the socket so it might as well be a child of the socket and so the gun is going to be one game object with shouts alright so we also want to set the parents but to do this we also need to save this part as a game object and then we can say okay instantiated barrel inch barrel transform parent is a weapon body but barrel sockets what we're doing with the barrel right now is we getting a random barrel from the list and we're spawning this barrel and we're saving the spawned barrel in this variable instantiated barrel and the instigated barrel dot transform that parent is going to be the weapon body dot barrel sockets so this means that the spawn barrel is going to be parented under barrel sockets let's see if this works it's press space and then we can see that the arrow socket has little arrow which means there it has a child and here we go we can see that our barrel is now chalice beneath the barrel socket so we could do this for all our other parts but then we have a lot of duplicate codes and duplicate code is a bad thing so we're gonna make a little function that does this what we're doing right here then a little bit more generic so that we can call it for each part so we're going to call this spawn weapon parts and again we're gonna give it a list because we want to if the list of parts and we give it a transform socket which is the socket it needs to be spun it and here we we are gonna say ok game objects random parts get a random part using the parts list whatever parts list was giving here we're passing it on to the get random part then we want to spawn this random part and we want to save it as a variable so we're gonna say game jack instantiate part is instantiate random parts which is the one we just got from the list and we want to spawn it at the sockets which we are giving here as a parameter so we want to on it at the socket dot transform that position and we can also use the socket transform dot rotation and then we're also going to parent it so in parts that transform that parent is the socket so now what we have written for the barrel is a little bit more generic for each parts because we're actually doing the same that that you saw here but here we had to use barrel and barrel socket and that kind of stuff but here it's more generic because we're passing on the parts list and the socket it needs to be parented under or positioned at spawn weapon parts with barrel parts and the weapon body barrel soccer but this can now go because it is replaced by this spawn weapon part we give the barrel parts list which is this list which contains all types of different barrels and we tell it where to spawn and this is the barrel socket and we got this from the weapon body which was just instantiate it as well then we go into here it's gonna get a random part from the list then it's gonna spawn that random part and it's gonna spawn it at the socket and it's gonna parent it under the socket so all these things can go now and we can just copy paste this and then change the right list and use the correct socket and now it should work so if I save this I go back to unity press spacebar and there we go we have a gun that is properly aligned let's delete it and spawn another one or let's move to the side so we can see it's actually random now you can see it has a different body a different magazine and a different scope this one another one [Music] looks very similar like this one but it has a different stock and a different magazine as you can see now we have random looking guns they are attached to each other otherwise some gaps as you can see but we can fix this by fixing our sockets and here you can see that we have three different looking guns let's go back to our prefabs and fix or sockets this one looks fine it looks fine probably it's this one yeah there we go there's a little gap here it needs to be aligned with the mesh of course and now it's properly aligned as you can see that is basically the start of our system so we have a list for each of our different types of parts and update we're checking at keydown space keygen generate weapon which is this function here we're spawning a random body by using the get random part function which just returns a random item from the list and the list is body parts is gonna return a random body parts responding it's at the center of the world and then we're getting the weapon body script that's on there and we need this because we need to know the sockets of this body we need to have a reference to these sockets and then we're spawning each part like this so spawn weapon parts were giving it the list of parts it needs to spawn from and we're giving it the correct socket it needs to spawn it so barrel socket scope socket and so on at the spawn weapon parts it's a random part again so if this is going to be the barrel list it's gonna get a random barrel from the list which is gonna be in here and it's gonna be spawned in the world it's gonna use the socket position and rotation to spawn it so at this position and rotation is gonna spawn in the world and it's going to be parented under the socket and the socket was we got this socket from the Welcome body and that's basically everything and we have weapon body and weapon body is just five different sockets that we've set up in the prefab like this so empty game objects there are nothing else and they're positioned at the right place just as I described in our design all right so now with our system done it might not look like much yet but I'm gonna show you that it's actually pretty working pretty well I have used this pack in my examples you might have seen them this is the Cintiq polygon apocalypse pack I'm not sponsored in any way I just really like what they do so what was interesting for me was when I saw this I saw all these weapons and then I saw the next screenshot and it showed that all these weapon parts are modular and this is exactly what we needed so I'm gonna insert the polygon pack inside my project and don't not change any code and show you that our system works very well oh here we go I have three bodies that look very different from each other and of course just like before we have to set up sockets and that's actually all we need to do so when I shift select all these bodies and at the weapon body script to it and then for each body I'm gonna make a prefab out of this or all of them we're gonna make a little prefab and for each prefab we are adding all the sockets again and then setting all the references to our weapon body script and once we've done that our system will work and then we can generate a couple hundreds or thousands of pretty looking guns okay so our body is set up now we're gonna reset the list resetting the list press our lock and now we can just drag all these bodies at the same time in there and then I'm gonna throw in some other stuff oops barrels and those are the grips holders which are the magazines and stocks we box press play press spacebar hey there we go we have a decent random looking gun and read another one looks very different very cool now what I'm gonna do now is add some code that destroys the previous weapon so if previous weapon is not zero then destroy reduce weapon gonna make and rip over your previous weapon previous body is instantiated body because that's the thing that's being instantiated and is the parent of all other parts space hey we have a gun tiny gun with a big scope hey we have a tiny gun with a very big magazine and a small scope hey we have an assault rifle and I can keep going like this for a long time and if we go back to a weapon generator well you can do the math we have three bodies which can have six barrels for stalks six to cope six loaders and six handles so probably that's already like a couple thousand different unique looking guns so unless you can see we didn't change any code our system just worked immediately by just swapping around some models we did have to make the bodies and place the suckers properly because we did that now we have unique looking guns and we can generate for a very long time alright so that was part one of my very first online video tutorial series we've made a board lens inspired weapon generator just part one in part two we're gonna add statistics to each gun part so we're gonna add things like damage accuracy reload speed rate of fire and we're gonna show them all in a nice weapon card in the UI in the user interface I hope you learn something new if you did not and stick around for part two hopefully you're gonna learn something new there as always please like subscribe let me know what you thought about this tutorial series in the comments I'll be there as well thank you and have a nice day
Info
Channel: GameDevLuuk
Views: 6,910
Rating: undefined out of 5
Keywords: game development, unity, indie developer, game tutorial, game development unity tutorials, borderlands weapon generator, procedural generation unity, weapon generator unity, random weapons unity, how to create random procedurally generated weapons for unity, how to create random weapons like borderlands in unity, gamedevluuk, weapon system unity, firing weapon in unity, random gun generation unity, how to equip weapon unity, learn to program random weapon system as beginner unity
Id: uUcPccXTtE4
Channel Id: undefined
Length: 26min 54sec (1614 seconds)
Published: Mon Jun 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.