Unity 3D Multiplayer FPS Tutorial 04 - Adding Guns

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys now as you know a shooter ain't a shooter unless it's got guns so in this video I'm going to show you how to make guns and how to put them in your game I hope you enjoy it this is awkward I I'm sorry I'm not good at these introductions I'm just gonna I honestly just need to give these things up yeah my name is Walton King from kweissen games and you're watching a first-person shooter multiplayer game tutorial okay starting where we left off from the last video we should be able to look around run and jump there by that alright perfect alright so let's go ahead and get right to it we're going to create a pistol real quick and create very simple gun we can work with for the time being and we'll just make it out of a couple cubes and later in the tutorial series we'll actually model a gun and animate it and everything but for now this should do I'm just gonna make a little rectangle let me disable the the sake of helping me design this knot design there we go I'm disabled to map camera and so I can go to game there we go now I can see from the players view only for now then I'll go back and Riaan able the map camera we're for now I'm gonna disable the map camera so I can get a good idea of what this guns gonna look like from the player of you lower it a bit alright thicker okay then I'm gonna make the base the handle make it tall then and out of it has some dimension there you go sighs some sort of shadow there we go so that's a pretty it's a pretty basic gun for now and I think that'll do the job I think it's a good position for it to so from here what we're gonna do is go into your player and you're going to create an empty object we're gonna name it weapon now we're gonna build a whole hierarchy for this and the way that this hierarchy is designed is incredibly important I'll try to explain it the best they can so the very first step is to create an empty game object called weapon that is at zero zero zero the we're also going to create an empty game object called cameras that is also at zero zero zero make sure that weapon and cameras are at the exact same position and then from there we're gonna just drag normal camera into cameras we're gonna create another empty game object called design and then we're gonna drag eyes into design and then we'll create another game object called miscellaneous or just misc and then I'm gonna drag ground detect in the misc so now our detect our ground detector isn't miss and our design we have our eyes and then in our cameras we have normal camera the next thing we're new is weapon inside weapon we're going to create another empty game object and this is gonna be the name of our pistol which for now I'm just gonna call it pistol and then within the design of our pistol we're going to create an empty game object called anchor and we're gonna create another empty game object called let's call this one States now okay so to just explain it thus far the weapon empty game object is gonna be what holds whatever what holds all of our weapons so like well we have a pencil right now but like let's say for example we want to add a rifle and we want to call that rifle X beam you know then the X beam would be here instead of the pistol so this is where we're going to change out our weapons is under weapon and then under each of our weapons all of our weapons are gonna follow the same design pattern which means that even if this was our X beam or if you wanted to add like ump-45 like any kind of weapon you want to have and you'll name it that and then under it'll have an anchor empty gameobject in a state game object and under our anchor we're going to have our design and then of course for the design you'll just include everything that is like what you can see visible and that's just like what the gun looks like and then also under anchor we're going to create something called resources and so when we get to it resources is what's gonna have like our muzzle flash if we wanted to add any attachments to the gun like a flashlight stuff like that and then for example like where we're gonna spawn the bullet or where we're going to do a ray cast from to this to detect whether or not the gun shot something we're also going to add two resources and the reason for this is because under resources we want there to be and we want a clear path to every object that will be on every gun because in the design we can have a variety of different parts and pieces and different names but the idea is that under resources we'll always have something called muzzle flash on every gun that way from our script we'll be able to just go to anchor resources muzzle flash no matter what it just keeps things organized for you as you're making more weapons in the future all you have to do is duplicate the pistol go in here and then change the design and then move the resources around and then under States we're going to say like for example as you can see okay so under States create one that's called a hip these are also empty and create another empty that's called ABS and so your hip state is where the gun will be when obviously you're not aiming in and your abs state is gonna be where the gun will be when you are aimed in so as of right now you can tell that the hip is just where it should be and that the 80s we're going to just click on your anchor control see click on 80s and then control V to copy it and drag the anchor into 80s and then move and then click on 80s and move the 80s so that the gun is where you would want it to be when you're aimed in so something like okay that's there we go so when we aim in that's where our gun is gonna gonna go and so now once you have that set up you can just delete or hide the anchor I'm gonna delete it and that way you just leave it like this and then I'll we'll get to how we're going to aim and how we're gonna use these States in the future but this way we can create for every weapon a unique hip in aim hip and aiming position we're also later going to add like a running position so that we can move our gun to the running position whenever our player is running but for now that is that is that wraps up the design of our pistol the only thing we could do from here is maybe give it some color so I'm gonna just controlled either player eyes material and then make a pistol material throw it on design throw it on here and here and I'm just gonna name these I really I'm as if you haven't told you I really like organization and stuff I don't know what the top of what God has called so I'm just gonna call it or we can call it the barrel even though there's more to it the housing I don't know okay yeah we ever are we ever pistol looks nice if we play it from here I'm gonna put the game back over here so it's all make sure you re enable your map camera okay and then make sure you save always always safe just in case you need crashes and you don't want to end up doing something rash now from here we can see that we can see the guns there but when we look up and down the gun doesn't follow and yeah and when we run there we go to run the gun kind of goes super far forward which is good to an extent alright let's uh let's fix some of these things okay so for the up-and-down thing we'll tackle the up-and-down looking thing later let's go ahead and make it so that the weapon can be equipped or not equipped so what we're gonna do is in your assets folder right click and create your new folder for prefabs prefabs are just like objects that you've made in in the scene that you want to make over and over and you just kind of want to have like a you want to save them in your project so like for example our pistol we want our pistol safe so within our prefabs folder I'm actually going to create another folder called guns there we go that way we have our pistol here and then since our pistol is at position 0 0 0 I'm gonna go and delete it and then just to get it if we wanted to get our pistol back all we have to do is instantiate it at weapons 0 0 position 0 0 0 position that's good to know for in just a moment so now what we're gonna do is we're gonna go into our scripts folder and we're gonna actually create the script that's going to manage our weapons so we're gonna call it weapon by a compile as soon as it's compiled we're gonna open up weapon reload all okay now what we're gonna do is we're just gonna create really quickly public game object game object array called loadouts and this way we can store multiple game objects within our loadout in that way you can have like a primary weapon a secondary weapon you can have a tertiary weapon quaternary weapon so yeah just create a loadout and then from there we're also going to create a function called equip we're going to take in an integer we're gonna say P underscore IND all right and so now what we're gonna do is whenever we want to equip a weapon we're going to call equip and we're going to send in the number of the weapon within the loadout so if it's our primary weapon we would send equip zero if it's our secondary weapon we would send equip one and then what we're gonna want to do is we're going to want to create a new game objects and game object the underscore new equipment equals instantiate as game object now instantiate takes a couple arguments the first argument is the object that you want to instantiate so we want to instantiate loadout P underscore IMD and we want to instantiate it at our weapon position so now what we need to do is we need to create a public transform called weapon parent and then that's what that's the position we're gonna want to spawn our item at or a gun at is gonna be weapon parent dot local position or weapon parent dot position not local solution and then it's also going to need to take in a rotation which we're also going to take in we're also gonna send in weapon parent dot rotation and then it needs a parent which we're gonna send a weapon there there we go oh my G's weapon parent okay so as we were doing so instantiate takes an object which we're sending in the game object of in our loadout based on the index that we since we quit it also takes in a position which we're sitting in our weapon parents position our weapon parents rotation for rotation and then for the parent we're sending in weapon parent and then just kind of has a double whammy I like to do I like to take the object we just instantiate it tickets transform and then set its local position equal to vector 3.0 and since it's a parent since it's a child of our weapon parent setting local position equal to the vector 3.0 is just setting it just making sure it's at zero zero zero within our weapon parent and then we'll do this we're gonna do the same thing for rotation so just control on you're just gonna do local rotation I took quaternion local Euler angles that's with you local Euler angles which is a vector3 representation of rotation and set that equal to back to 3.0 as well alright now this is a very big this is very basic we're going to come back and we're going to flush it out but just really quickly save it doesn't look like it's saving I know actually there's one more thing you know in update do if input get key down key code dot alpha 1 I believe alpha 1 should be the one key at the top of your keyboard so if you press 1 on your keyboard then we're gonna call equip one are equipped 0 and equipped so basically if you press 1 on your keyboard then we're going to equip the primary weapon so the first weapon in your loadout so let's save this yeah for some reason it's not saving so if yours isn't saving just control I'm gonna cut it out save James yeah see if it actually did say the changes okay okay so now it's safe okay there we go we're gonna throw the weapon script on our player where our motion and look script are so there's our motion script well I'm gonna I'm gonna make everything small so it's easier to manage and drag ken joy there we go it's now our scripts are next to each other I'm gonna open up the weapons script for weapon parent I'm gonna drag weapon and then for loadout I'm gonna drag in our pistol and I'm gonna save a project play all right so now no errors if I press one our gun has been equipped now there is an issue with this is that if we press one twice we should have two guns you see and we don't want that so to fix that we're going to create a private game object called current weapon and then what we're gonna do here is at the beginning of equip we're going to check to see if current weapon if current weapon is not equal to null then we're just going to destroy current weapon so what that does is if if the variable current weapon if there's something stored in current weapon then we're going to go ahead and destroy it and then what we're gonna do is after we've instantiated our new weapon is we're gonna set current weapon equal to our new our new equipment actually for the sake of keeping things parallel I'm gonna rename this recurring equipment any reason why I'm calling it equipment actually you know what let's let's stick with weapon let's stick with weapon I'm sorry I'm not trying to be trying to be all finicky this sometime you know some people you know you might want to add grenades in the future or like some weird kind of item and stuff and it's gonna be you we're gonna use the same script later in the tutorial to do that so that's why I was saying Navy equipment but weapons fine we can call a grenade a weapon I mean technically right yeah so now what this does is if we try to equip something if there's already something equipped it deletes it and then it goes ahead and equips the thing that you're trying to equip so now we shouldn't be able to create more than one weapon so then if you had a secondary and you tried to create that secondary the primary still wouldn't the primary would be deleted before the secondary came up so now when we press one our pistols there and if I press one again I pause it we should see that there's still only one pistol perfect the next thing we're gonna do is inside our weapon script we're actually our loadout is a game object array now this is a bad practice because your weapons are gonna have all different sorts of stats your weapons will have stats from rate of fire to damage to I don't know not kick back so in a variety of things whatever you want to add so instead of having multiple variables here for every single different kind of weapon we want our loadout to contain all of the data for our weapon instead of just the game object alright so to do that what I'm going to show you is we're gonna create a new folder within our scripts called scriptable objects and we're going to create our first scriptable object or these should actually be called scriptable object generators scriptable object gens and then we're gonna create our first script of object generator which is gonna be gone so once that compiles will open up gun alright so to create a scriptable object which is scriptable object is just an object in unity that holds data or holds information which we're going to be using for all of our guns you're going to the first take away the monobehaviour thing and change it to scriptable object and get rid of everything inside of this the next thing you're gonna want to do is up here above the class you're going to want to add and create asset menu on for you're gonna want to do a file name which is the default file name an object is given when it's created we're gonna call it new and then there's also a menu name which is the name it's gonna have in the whenever you right-click and you're gonna create a new object this is the name it's gonna have in there so we're just gonna call it gun and this will make sense once we compile it and we see what it actually does oh my geez happens to me alright and then inside our new gun class is where we're gonna put all of the we're gonna put the skeleton for all of our guns so a public string name this is gonna be the name of our gun and then public game object prefab and this is gonna be the design of our gun the actual prefab that we have so right now what our current loadout situation is is that we are putting the prefabs directly into low down instead what we're gonna do is we're gonna make load out a gun array and then we'll access the prefab through our gun class and then right now we don't have any other variables but like if you want to go ahead and add in like rate of fire and like cost stuff like that you can go and add that in here like you could just call it public float I don't I prefer fire rate you can call it rate of fire doesn't matter but yeah so I'll just leave that here just to show you how it works and then we just save it and then wait for it to compile all right and now it will do is will create one more folder in our assets folder and this one will be called scriptable objects and then within our script of objects we're gonna create a folder called Oh oops we're gonna create a folder called gun our guns and then you're just gonna right-click go to create and then click gun I extent Lee did it I'll just delete this one to show you how it works again right click in in your guns folder and then go to create and then click gun and now we have this is where we're gonna store all of our information for a gun so then as you can see when you click on the pistol you can edit all of its information here so we have a name we're gonna name it pistol we have a fire rate which doesn't do anything right now so we'll just make it nine thousand then we have a prefab then for the prefab all we have to do is go to our prefabs go to our guns and drag our pistol and there we go now we have a scriptable object for our pistol and that has all our data in there but it doesn't we're not using it so in order to use it all we have to do is go to our weapons script and instead of the game object to Rhea Rhea we're gonna use a gun array interesting forget how this works exactly maybe interesting this gun is definitely class give me a momento hold up sorry I'm sorry sure well we one thing we didn't do is create our namespace let's try that first namespace Kawai Starcom kauai son simple hostel just surround everything in that and then we'll do the same thing for our gun calm Hawaii sign doc simple hostel put our gun script in this new space save it see if it gun - you know if I reference there we go the work they worked I guess it's just not registering in monobehaviour okay so yeah just do this it should work and then all you have to do from here so now our loadout is going to be taking in our gun data it is gonna be a gun data array so then we just have to put the gun data in our loadout based on what we want the players loadout to be so now the only thing you have to fix is down here when we instantiate the gun we directly try to instantiate the actual gun data instead of the prefab so all you have to do is load out get the index get the gun from the lot you want to instantiate and then you just have to call prefab save that that should fix it there we go I don't know what's going on about my Mona behavior it's probably just goofing probably set to reset it but yeah so now once you go back to your weapon script you'll notice that your loadout has been cleared all you have to do is go to your scriptable objects and drag your pistol and we'll save it save project and now when we press one our gonna Pierce perfect yay now the only thing the last thing we're gonna do in this video is we're going to get the gun to look up and look down the way we're gonna do that is through our looks script oh let me restart my motive behavior because it's goofin straight goofing on me so hold up and we'll just open up weapon again hopefully this time it's working I don't know how long I feel like I always make these videos way longer than they have to be there we go everything's working just fine so now you're gonna go to your looks script just you that's right okay so what we're gonna do now is to fix the gun look marina create another we're gonna have another transform called weapon there we go we're just gonna yeah so just create another transform called weapon and then in your set Y function over here where we set cams dot local rotation we're also going to set weapon dot local rotation equals T underscore Delta and the reason why we don't need to do all the extra stuff that we did for Kansas because we want our weapon rotation to be equal to our cams rotation so I mean if Theory you don't even have to do this you could just come down here and you could do weapon dot local rotation equals or even you could do weapon dot rotation equals cams dot rotation would probably be the safest way to do save it and before you run it make sure that you set your your weapon and look so under weapon just go to weapon save it just in case you crash something you don't want to destroy yourself I've definitely been there many times there we go we can look up now we can run with the jump and then we press one and our gun is there and we can look up and we can look down ooh we will fix this in the next episode I hope you enjoyed this episode you can now look up and you can look down with your weapon right now the only issue that we're facing is that the weapon has like this weird adjustment where like it kind of rotates on an arc I dunno how to fix this but it looks like we're running out of time for this video so we will definitely fix this in the next episode thank you so much for watching if you want to see more tutorials then subscribe and have a great rest of your day bye [Music]
Info
Channel: Welton King
Views: 40,237
Rating: undefined out of 5
Keywords: Unity 3D, FPS, Dev, Journal, Indie, Game, Blender, Minimalism, Multiplayer, Competitive, First Person Shooter, Simple, Development, Video Games, Tutorial, Setup, Guns, How to, Weapons, Equipment, Pistol
Id: 8z89bdT9TeE
Channel Id: undefined
Length: 28min 33sec (1713 seconds)
Published: Wed May 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.