5e switching weapons This is a topic that many people are looking for. saibabalinks.org is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, saibabalinks.org would like to introduce to you YouTube. Following along are instructions in the video below:
“This video. We ll have a look at how you can create weapon switching in in unity. This system will allow you to switch between any number of weapons that 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. ” ..
Thank you for watching all the articles on the topic YouTube. All shares of saibabalinks.org are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.
video, berbagi, ponsel kamera, ponsel video, gratis, upload