Weapon Switching - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is literally a 10 min video tutorial on enabling/disabling gameobjects.....I mean, really?

👍︎︎ 7 👤︎︎ u/Rhames 📅︎︎ Apr 25 2017 🗫︎ replies

Not only is this completely messy it relies on quirks in Unity's UI for getting array positions and is way too cumbersome.

Simply have a weapon manager script with an array that you can define how many weapons you wanna switch between, and then wrap the array at the edges.

👍︎︎ 1 👤︎︎ u/[deleted] 📅︎︎ Apr 26 2017 🗫︎ replies
Captions
in this video we'll have a look at how you can create weapon switching in unity this system will allow you to switch between any number of weapons that you want using both the scroll wheel and the number keys and it can easily be combined with the weapon script that we created in my previous video in order to make a really nice weapon system so check that out if you haven't already alright so let's jump into it by the way the weapons I'm using here are from this sci-fi weapons pack if you want to get it for yourself you can go to dev assets comm click on the sci-fi weapons as with everything else on the side its pay what you want this bag actually features a total of 32 different weapons so you'll enjoy them and if you're making a more realistic shooter we actually also have the modern weapons pack which features some pretty popular realistic weapons so as you can see I have this example team set up under the player we have the main camera and under the main camera we have our three different weapons you can see in the scene view here that I'm just using the standard rigidbody first-person controller you can always get this by right-clicking going import package then choosing characters then going under standard assets characters first person character and choosing the rigidbody FPS controller prefab now under our main camera we currently have the heavy weapon active but as you can see we also have a rifle and a pistol each one of these guns have a gun script on them which is responsible for shooting this is the one that we created in the previous video note how the different weapons have different gun settings now in order to switch between these when in-game we need a script and we could just go ahead and put this on our main camera itself however I think it makes sense to have an object dedicated for this purpose let's go ahead and right click on the main camera hit create MT let's rename this one to weapon holder and I'll just have it sit in the center of our camera then let's take our three weapons and drag them under the weapon holder we and then select the weapon holder hit add component and we want to create a new script called something like weapon switching it's a new script like c-sharp and creating that let's double click it to open it up in visual studio now of course there's a bunch of way you can go about creating weapon switching you could create an array of all your weapons and then spawn them in and out as you need them or you can even create some kind of scriptable object that is going to store all the information about your weapons but I think a much easier and still very customizable way is by simply enabling and disabling all the child objects of our weapon holder this way we don't have to update any kind of list we don't have to go and create custom data objects and figure all that stuff out and we can still swap weapons in and out as we want so let's begin by delete the two using tags up here and we'll actually keep both these start and they'd method for the script we only need one variable and let's go ahead and create that now this variable is going to determine what weapon we currently have selected you could use the name of the weapon but I think a lot easier is using some kind of index so we'll create a public integer and we'll call it something like selected weapon and we can just default this to zero now in unity whenever we talk about indexes of child objects we start from the top so a rifle here would have an index of zero and so this will be selected by default the heavy weapon will have one and the pistol left - luckily we can very easily rearrange all the objects here so if we want our pistol to be selected first we simply drag that to the top but I'm just going to keep the rifle there so right when we start the game we want our rifle to be selected let's go ahead and call some kind of method that will do this force let's call it select weapon so now we're calling the method we also need to create it let's do that right underneath our update method let's write void select weapon and basically what we want to do here is loop through all of the weapons if the weapons index does not match the selected weapon we want to disable it and if it does match we want to enable it so in order to loop through our weapons we use it for each statement and we say that for each transform and we'll go ahead and call this transform the weapon under transform this basically means that we will take all the transforms that are childs to our current transform which is the weapon holder and we'll do through each one referring to the current one we're inspecting as weapon let's open and close some curly brackets and of course now we need to reference by index and a for each statement doesn't have one let's just go ahead and create one ourselves up here we'll write int I equals 0 then each time we go through a weapon we write I plus plus so the first time we live through is going to be 0 we then add 1 on to it the second time it's going to be equal to 1 and then add 1 on to it and so 2 3 4 and you get the point what this allows us to do is now go in here and check if I it's equal to the selected weapon well then we want to enable the weapon and so we go weapon dot game object subset active we want to set it to true if not meaning that the index is do not match we say weapon that game object subset active false so what we're doing here is that the start of the game we want to select the currently selected weapon which defaults to 0 so that's the first one and we do this by first creating a variable called iron setting in equal to zero will then loop over all of the weapons and in the beginning here is going to be zero so if our selected weapon is zero and it is we're going to go ahead and make that active and we're then going to increase I and then for a second weapon we're going to check if one is equal to zero which it is not and so we'll go ahead and disable that weapon and then we add one again and this time I is going to be equal to two and selected weapon is still going to be zero so it's again false and that is two going to be disabled this way only one of our weapons will be enabled at a time so if we save this we should actually already see that when we run the game it's going to select our rifle call but we still can't switch weapons during the game to do that we need to get some input from our player and we do that in the update method we can both change weapons to using the scroll wheel or the number keys let's begin with the scroll wheel so let's check if our player Scrolls up to do that we right if input get access and the access that we want to check for here is the one called Mouse scroll wheel and you need to write this the exact same way that I did capital m capital S and capital W if not it's not going to avoid this here is a float that represents the current movement of the scroll wheel if it's greater than zero we've scrolled up and if it's less than zero we've scrolled down so here we can check if this value is greater than zero and if it is we can go ahead and select the next weapon in the chain so we want to increase the directed weapon by one to do that we go SAS plus however right now we can do this infinitely and since we only have three weapons we are quickly going to get to a point where we simply disable all the weapons so instead let's have this number wrap around so that when we get to index two meaning that we've selected our third weapon and then scroll up it's going to go back to the first weapon meaning index zero to do that we want to check if selected weapon is greater than or equal to the current amount of weapons we have which we get by using transform the child count minus one so if we have three children we're going to check if selected weapon is greater than or equal to two and if it is we don't want to increase our selected weapon instead we want to set our selected weapon equal to zero and if not well then we can go ahead and increment it every one this to happen in the opposite direction as well so we'll say here that if it is less than zero we want to see if selected weapon is less than or equal to zero and if it is we want to set selected weapon equal to transform child count minus one and if it is not we want to go ahead and subtract from it this way we can go in both directions this can be a bit confusing to wrap your head around but try and go through it line by line to understand it and don't worry these things become much easier to understand over time so now if we save this and go into unity a weapons won't change but we should be able to see our selected weapon variable change it's a play and now when I scroll up you can see the selected weapon increase until it gets to two in which case it's going to go back to zero the same way if I scroll down you can see the decreasing and when I get to zero it's going to jump back to two awesome now all we need to do is also call us select weapon function but we only want to do this if we've actually selected a new weapon we could go in here and call select weapon here and here but since we're going to be lots of different stuff that requires us to select a new weapon let's not do that instead let's go up here and create a temporary variable it's going to be of type int and it's going to be called previous selected weapon and it set it equal to our selected weapon then at the very bottom after we've done all of our calculations we can check if the previous selected weapon is not equal to the selected weapon in which case we want to go ahead and call our Select weapon method so that we will enable and disable the corresponding objects this way when we hit play and again scroll using our mouse wheel we can see that we can now switch between the weapons in both directions so now we actually already have a nice weapon switching script oven running and you can easily add on to this by creating weapon switching animation and maybe some UI to display what weapon we are currently on and which one is next in the chain I think most importantly we can go ahead and map the different weapons onto the number keys to do that we just use a bunch of if statements we're going to hear me right if input get key down and the key that we want to check for is key code alpha one meaning that we've pressed the one key on the keyboard and if we have we can go ahead and set selected weapon equal to zero let's go ahead and copy this if statement and we'll just go ahead and change the 1 to a 2 and the 0 to a 1 however we of course also need to check if we have two weapons if not we don't want to set selected weapon to one so in here we need to add another requirement and we use the double and signs here and what we want to ensure is that transform Dutch child count is greater than or equal to two and now we can copy this entire if statement once more and let's just do once more this way we can use the first four numbers will change this one to a three this one to a three and this one to this one two or four this one to a four and this one to a three if we now save this and hit enter unity and I begin by pressing two you can see it switches to the second weapon three to the third one to the rifle and four nothing happens so now we can use a combination of the scroll wheel and the number keys in order to change weapons you can very easily go ahead and add new weapons to the hierarchy here and everything should automatically update you can also go ahead and reorganize elements on the fly and our script should just update to take care of it let's pretty much it for this video I hope you enjoyed it if you liked it make sure to subscribe so you don't miss a future video thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in March and a special thanks to Derek Eames Kirk face lemare a fine James Calhoun and Jason the Tito if you want to support the channel and become a picture in yourself you will do so a patreon account slash brackets thanks a lot guys
Info
Channel: Brackeys
Views: 328,178
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, model, models, beginner, easy, how, to, howto, learn, course, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, weapon, switching, weapon switching, gun, change, changing, switch, pistol, rifle, shoot, scroll, wheel, number, keys
Id: Dn_BUIVdAPg
Channel Id: undefined
Length: 9min 59sec (599 seconds)
Published: Sun Apr 23 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.