You could also use modulo (aka the % operator) for the upward weapon switch. It would look like this: selectedWeapon++; selectedWeapon %= transform.childCount; If you don't know what modulo is, it basically gets the remainder from the division statement. You don't have to write a bunch of if statements that way. This saves a few lines and is actually quite efficient.
Definitely should continue these smaller videos Brackeys!! Love them! I saw someone request an ammo system and I think that would be awesome! Along with a ui of course.
Funny enough, I think I did 80% of this video on my own before seeing this. I think it just makes a lot of sense to do the way described here. It is good confirmation that I'd done things in a good way. There was one thing I did differently though.. I didn't have a "weapon holder" object, and just simply reference children below it, but I think that's just because of my game design. I have more weapons as children than the player can use, so I have something else to manage the weapons they're carrying and have armed. All in all, though, this is a great tutorial, and the method described here about the weapon holder is actually a really good idea. Thumbs up!
Nice tutorial as always. As I want to free up memory as much as I can, I do the switch by instantiating the weapon at a placed spawn point every time it is selected. Also, I store all the functions in the weapon object, even key presses like fire. It was easier because of the differences in their behaviour. After watching your tutorial however I might go with your method as it seems much more fluid in the end, though it might get heavy with 10+ weapons, so some testing might be needed. Not sure how much extra load the inactive stuff means.
I had two animator components on my player, one for upper body and one for lower body. I made different controllers for each weapon and if that weapon is equipped, I simply switch animator controllers. Like this, I can do everything infinitely. And also, this took me a month to figure out. And another to do it.
You are just so awesome. words cant describe how you help. thank you brackey,s i miss you a lot. and hope that you are living your dream wherever you are.
Saad Tv It's really really simple, I think a few really good videos on RUclips goes over it. I feel like the only challenging part for a lot of people is the mecanim portion.
switching with animations are easy you just need to make a default state anim and then make a sound fx play on awake and walla. you didn't understand did you? :(
When you want to restrict an index to a range of numbers (like wrapping the index back to 0 when incrementing) it's almost always better practice to use modulus. Write `selectedWeapon = (selectedWeapon + 1) % transform.childCount` instead. This is a slight performance increase over using an if statement, and it's much more succinct. Even better would be this (I'll use some slight pseudocode): if (scrollUp) selectedWeapon++; else if (scrollDown) selectedWeapon--; selectedWeapon = selectedWeapon % transform.childCount; This way it's much simpler and you only need to apply modulus once to make sure the variable stays in your desired range.
Really cool! I have an idea. How about you made a tutorial on how to make an ammo amount. In sense of being able to see how many bullets you can fire before there are none left and possibly a reloading animation.
In the foreach statement in the SelectWeapon method, you could have simply said SetActive(selectedWeapon == i) instead of routing it to separate if statements.
Ok, it works and it doesn’t work at the same time. The characters switch, but only at the position of the parent object. But I dunno how to make them appear at right position.
H,i I just started with Unity and C# last week and your videos are really helping me. I was wondering whether you could use a for loop instead of having to copy paste the "if statements" and whether it's beneficial (Timestamp 9:00) I used an array for the keycodes like this private UnityEngine.KeyCode[] Keycodes = {KeyCode.Alpha1, KeyCode.Alpha2, KeyCode.Alpha3, KeyCode.Alpha4, KeyCode.Alpha5, KeyCode.Alpha6, KeyCode.Alpha7, KeyCode.Alpha8, KeyCode.Alpha9, KeyCode.Alpha0}; and then I used a for loop and it works fine for (int i = 0; i
Thank you, I tried it and it really worked and this code is not massive. However, I should specify that in my case it worked when I declared in "Update" method KeyCode array without "private" together with loop between "if" statements for mouse scroll wheel switching and if (previousWeapon != selectedWeapon).
I like this way of making weapons best as it allows me to make each individual gun unique and not have to include every single stat on a single constructor file as some guns might be fired once or be selected as an ability so fire rate would be useless. Though i'm planning on having more than 30 or even 60 different weapons
i really love the idea of pay what you want or grab it for free. I'm very sorry that i can't afford to support the model developpers but i'm grateful i can still use their creations
I am not even a Game Developer. Full-stack web developer instead but I love watching these videos! Why don't you make a game development course on Udemy? I am sure you will sell tons of it!!
Because, as stated in his description "All content by Brackeys is 100% free. I believe that education should be available for everyone. Any support is truly appreciated so I can keep on making the content free of charge." Making a payed course on Udemy would pretty much make him a full on hypocrite.
i added a transition to it so its not static using UnityEngine; using System.Collections; public class WeaponSwitching : MonoBehaviour { public int selectedWeapon = 0; public float WeaponSwitchSpeed = 5f; public Vector3 position1; public Vector3 position2; public GameObject weapon1; public GameObject weapon2; public GameObject weapon3; public bool Selected1; public bool Selected2; public bool Selected3; void Start() { StartCoroutine(SelectWeapon()); } void Update() { int i = 0; int previousSelectedWeapon = selectedWeapon; if (Input.GetAxis("Mouse ScrollWheel") < 0f) { if (selectedWeapon >= transform.childCount - 1) selectedWeapon = 0; else selectedWeapon++; } if (Input.GetAxis("Mouse ScrollWheel") > 0f) { if (selectedWeapon = 2) { selectedWeapon = 1; } if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3) { selectedWeapon = 2; } if (previousSelectedWeapon != selectedWeapon) { StartCoroutine(SelectWeapon()); SelectWeapon(); }
you could also make it so if the gun isn't in view the gun wont fire then you could make a cube that has all 3 guns attached to it and it spins a certain amount when you press a button
A better way for wrapping around would be using variable % number, I remember using it in another coding language but I'm not sure if it works in C#, get back to me on that, thanks
wrote the code step by step like in video but when i run the game..all weapons are enabled when selectedweapon index is 0 and when is 1 all weapons are disabled(((
You do have to add the number keys code for each weapon you add though. You don't need to add it for the mouse wheel though. There are ways to make that automated, but it's better not to get into it for a beginner tutorial.
re: 6:05. I think a better solution would be (in pseudocode) "selectedWeapon++; selectedWeapon = selectedWeapon % weaponList.length". And the same, but with "selectedWeapon--" for scrolling down. Same effect, but a lot cleaner IMO
Hello Brackeys, can you make a Weapon pick up tutorial please, like picking a weapon by pressing 'F' and and 'Q' to drop. To be simple, like COD weapon picking...(pick, drop, swap)... your videos are really amazing and easy to understand... thanks...
5:50 well we can easily do that by selectedWeapon++; selectedWeapon%=transform.childCount; That means whenever selectedWeapon gets to the limit.. it will be set to 0
For some people like me you might have this problem where when foreach the transform, it acess the transform on the WeaponSwitcher too, so when you use your scroll wheel and keep scrolling up, you could end up in a empty space where the player is holding nothing. To deal with that, I add - public Transform weaponSwitcher -, and under the foreach -if(weapon.transform.IsChildOf(weaponSwitcer)){//rest of the code}
These small but useful tutorials are best to watch.
also, i would like to see switch animation
@@talha9585 It is really not so hard, you can search up how to do it
Edit: Never mind, it's harder than I thought
@@coolboidoesstuff9828 no making games is much easier than you think! .. :D
@@dmudda90 No I was talking about him asking for switching animation
@@dmudda90 "You should take this course on Udemy"
your tutorials are super helpful!
Thanks, glad you like it! :)
Yeah ;)
You could also use modulo (aka the % operator) for the upward weapon switch. It would look like this:
selectedWeapon++;
selectedWeapon %= transform.childCount;
If you don't know what modulo is, it basically gets the remainder from the division statement. You don't have to write a bunch of if statements that way. This saves a few lines and is actually quite efficient.
Your newest tutorials are so professional I can't imagine how long it takes to make a 2 minute video for you...
you're helping me create a universe, brackeys. i love you
Did you finish? Can I buy it?
Red Bepis no lmao
Tutorial unclear.. Accidentally created CS:GO
lol
I hate it when that happens
Task Failed Successfully
That's litterally how good these tutorials are😂😂
@@KlutzOfAMan XD
As always, these small tutorials are really great! I would love to see one about animations :)
Definitely should continue these smaller videos Brackeys!! Love them! I saw someone request an ammo system and I think that would be awesome! Along with a ui of course.
I was going to need something similar like this for switching characters in my game, and this is perfect!
Funny enough, I think I did 80% of this video on my own before seeing this. I think it just makes a lot of sense to do the way described here. It is good confirmation that I'd done things in a good way.
There was one thing I did differently though.. I didn't have a "weapon holder" object, and just simply reference children below it, but I think that's just because of my game design. I have more weapons as children than the player can use, so I have something else to manage the weapons they're carrying and have armed. All in all, though, this is a great tutorial, and the method described here about the weapon holder is actually a really good idea. Thumbs up!
Very helpful as always. Been watching you since the first Survival Game tutorial
i'd love to see a continuation of the Multiplayer FPS Shooter with a Single Player Part of the Shooter
Been following Brackeys since survival game, awesome channel !
Great tutorial, got weapon switching working in my game before even having my coffee.
Best coding tutorial channel I've ever seen in this world!
Thanx Brackeys. Good efficient tutorial. Exactly what I was looking for. Keep it up! Looking forward to future tutorials.
thx brackeys loving this series
Thank you! I really like your simple and clean tutorials! Very helpful
Nice tutorial as always. As I want to free up memory as much as I can, I do the switch by instantiating the weapon at a placed spawn point every time it is selected. Also, I store all the functions in the weapon object, even key presses like fire. It was easier because of the differences in their behaviour. After watching your tutorial however I might go with your method as it seems much more fluid in the end, though it might get heavy with 10+ weapons, so some testing might be needed. Not sure how much extra load the inactive stuff means.
How to make a weapon appear when you pick it up and not just a full set of weapons? Good lessons, thank you.
You would set the weapons all to inactive and then make a power up collectible that activates only one type of weapon for each.
Hey were you able to figure this out?
Love your model, it’s so detailed
I had two animator components on my player, one for upper body and one for lower body. I made different controllers for each weapon and if that weapon is equipped, I simply switch animator controllers. Like this, I can do everything infinitely. And also, this took me a month to figure out. And another to do it.
Thank you. The script is straightforward to understand
You are just so awesome. words cant describe how you help. thank you brackey,s i miss you a lot. and hope that you are living your dream wherever you are.
I thought the whole process was much more difficult than this!! But this tutorial made it all simple!!!! Thanks!!
can we make an animation for weapon switching
also shooting/aiming animations!
Saad Tv yes will be nice to see it (and check if i'm doing it correctly)
Saad Tv It's really really simple, I think a few really good videos on RUclips goes over it. I feel like the only challenging part for a lot of people is the mecanim portion.
Actually it isn't that hard, will put it to my list.
switching with animations are easy you just need to make a default state anim and then make a sound fx play on awake and walla. you didn't understand did you? :(
This is really useful and helpful. Thank you.
Pretty much everyone that makes an FPS is going to need this.
Simple, short and easy as it should be for programming
Could you do a tutorial on enemy ai with shooting
Boss Hog306 do you still need that tutorial? I’m considering making one
@@BlackMesaEmployee you don't make video :(
@@sconosciutosconosciuto2196 he said he was considering. Duh?
@@BlackMesaEmployee please
@@BlackMesaEmployee do it
One of the most reused scripts Ive ever needed.
When you want to restrict an index to a range of numbers (like wrapping the index back to 0 when incrementing) it's almost always better practice to use modulus. Write `selectedWeapon = (selectedWeapon + 1) % transform.childCount` instead. This is a slight performance increase over using an if statement, and it's much more succinct.
Even better would be this (I'll use some slight pseudocode):
if (scrollUp) selectedWeapon++;
else if (scrollDown) selectedWeapon--;
selectedWeapon = selectedWeapon % transform.childCount;
This way it's much simpler and you only need to apply modulus once to make sure the variable stays in your desired range.
Really cool! I have an idea. How about you made a tutorial on how to make an ammo amount. In sense of being able to see how many bullets you can fire before there are none left and possibly a reloading animation.
HE MADE IT ATLAST I LOVE YOU BRACKEYS YOU ARE THE
we miss u brackeys! come back plzzz
I don't think he ever will. He is leaving youtube, forever (or is he?) But there is a chance that he will come back
Brackeys is so underrated
3:17 Brackeys farting is the last thing I expected to hear today...
been absolutely buzzing for this video since Wednesday
JammyDodger16 me too!
thank you! I waited for this for so long!
You have no right to be so good
Best tutorial channel, why dislikes!?!
your tutorials are super awesome 🙆
In the foreach statement in the SelectWeapon method, you could have simply said SetActive(selectedWeapon == i) instead of routing it to separate if statements.
You are one of the best youtuber for scripting and youtube
i watched 1 video of yours and my mouse automatically went to subscribe this guy
All the way first class tutorial. Love your tutorial
Barckeys you are my idole i like you so much i love your tutorials they teach me allot
Now that's called a awesome tutorial
thnx a loot brackeys, awesome video again...
wow! I didn't know the transform class implemented the IEnumerator interface that returned the children, good to know. thanks brackeys!
that feeling when you realize you put all the player variables (like health) in the shoot script and now u must break it apart
lol thats pretty easy to remove and CTR+V to a new script lol its been 3yrd
@@seraphimsarecoool ctrl x
I really like the new thumbnails.
Thanks Brackeys! I love your videos!
I used this for character swapping on my game. It is awesome!
Ok, it works and it doesn’t work at the same time. The characters switch, but only at the position of the parent object. But I dunno how to make them appear at right position.
@@NakaLore make a script that changes their position?
Love the new thumb nails !
I love your thumbnails by the way
Thank you for the video, i have learned a lot
4:15 you could write that in one line: weapon.gameobject.SetActive(i == selectedWeapon);(instead of lines 22-25) should also be faster!
i used this for switching attachments, but instead of scroll wheel its a button. great video man
H,i I just started with Unity and C# last week and your videos are really helping me. I was wondering whether you could use a for loop instead of having to copy paste the "if statements" and whether it's beneficial (Timestamp 9:00) I used an array for the keycodes like this private UnityEngine.KeyCode[] Keycodes = {KeyCode.Alpha1, KeyCode.Alpha2, KeyCode.Alpha3, KeyCode.Alpha4, KeyCode.Alpha5, KeyCode.Alpha6, KeyCode.Alpha7, KeyCode.Alpha8,
KeyCode.Alpha9, KeyCode.Alpha0};
and then I used a for loop and it works fine
for (int i = 0; i
Thanks man.
Thank you, I tried it and it really worked and this code is not massive. However, I should specify that in my case it worked when I declared in "Update" method KeyCode array without "private" together with loop between "if" statements for mouse scroll wheel switching and
if (previousWeapon != selectedWeapon).
Great tutorials. Cheers !
thank you Brackeys you rea
lly helped me
I like this way of making weapons best as it allows me to make each individual gun unique and not have to include every single stat on a single constructor file as some guns might be fired once or be selected as an ability so fire rate would be useless. Though i'm planning on having more than 30 or even 60 different weapons
i really love the idea of pay what you want or grab it for free. I'm very sorry that i can't afford to support the model developpers but i'm grateful i can still use their creations
Thanks a lot sensei brackeys
Very helpful for both 2D and 3D
Ingeniously and simply !!!
Brackeys please do a tutorial (or tutorials) on 2D procedural generation with blocks, similar to Terraria or Starbound.
i think theres a tutorial about that on Sebastian league's channel
Thank you so much this helps me in my farming game!
why the fuck do you need a weapon in a farm game. wtf bro.
Don't ask questions...
Do you have a new thumbnail style or is it just for this type of video's? The look really cool!!!
I am not even a Game Developer. Full-stack web developer instead but I love watching these videos! Why don't you make a game development course on Udemy? I am sure you will sell tons of it!!
Because, as stated in his description "All content by Brackeys is 100% free. I believe that education should be available for everyone. Any support is truly appreciated so I can keep on making the content free of charge."
Making a payed course on Udemy would pretty much make him a full on hypocrite.
You are m'y best teacher with you i Can understand
Elsker disse simple tutorials!
Cool man! Btw a local Splitscreen tut? Just asking
i added a transition to it so its not static
using UnityEngine;
using System.Collections;
public class WeaponSwitching : MonoBehaviour
{
public int selectedWeapon = 0;
public float WeaponSwitchSpeed = 5f;
public Vector3 position1;
public Vector3 position2;
public GameObject weapon1;
public GameObject weapon2;
public GameObject weapon3;
public bool Selected1;
public bool Selected2;
public bool Selected3;
void Start()
{
StartCoroutine(SelectWeapon());
}
void Update()
{
int i = 0;
int previousSelectedWeapon = selectedWeapon;
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectedWeapon = 2)
{
selectedWeapon = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
{
selectedWeapon = 2;
}
if (previousSelectedWeapon != selectedWeapon)
{
StartCoroutine(SelectWeapon());
SelectWeapon();
}
#region Selection bools
if (selectedWeapon == 0)
{
Selected1 = true;
}
else
{
Selected1 = false;
}
if (selectedWeapon == 1)
{
Selected2 = true;
}
else
{
Selected2 = false;
}
if (selectedWeapon == 2)
{
Selected3 = true;
}
else
{
Selected3 = false;
}
#endregion
#region transitions
if(Selected1 == true && weapon1.activeSelf == true)
{
weapon1.transform.localPosition = Vector3.Lerp(weapon1.transform.localPosition, position1, WeaponSwitchSpeed * Time.deltaTime);
}
else
{
weapon1.transform.localPosition = Vector3.Lerp(weapon1.transform.localPosition, position2, WeaponSwitchSpeed * Time.deltaTime);
}
if (Selected2 == true && weapon2.activeSelf == true)
{
weapon2.transform.localPosition = Vector3.Lerp(weapon2.transform.localPosition, position1, WeaponSwitchSpeed * Time.deltaTime);
}
else
{
weapon2.transform.localPosition = Vector3.Lerp(weapon2.transform.localPosition, position2, WeaponSwitchSpeed * Time.deltaTime);
}
if (Selected3 == true && weapon3.activeSelf == true)
{
weapon3.transform.localPosition = Vector3.Lerp(weapon3.transform.localPosition, position1, WeaponSwitchSpeed * Time.deltaTime);
}
else
{
weapon3.transform.localPosition = Vector3.Lerp(weapon3.transform.localPosition, position2, WeaponSwitchSpeed * Time.deltaTime);
}
#endregion
}
IEnumerator SelectWeapon()
{
yield return new WaitForSeconds(1f);
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
}
}
}
keep the position 1 at 0x 0y 0z and for position 2 x0.5 y-0.5 z-0.5 worked for me
bro thats awesome, thanks
my gun go in my head, how would i fix?
@@Markleap move the game object coordinates (the gun) forward
your Channel is awesome! I am your Subscriber! Everything I need is Already Available on this Channel. Highly Recommended to Everyone!
Congrats!!! When did you first start investing
Another Great Tutorial
Great tutorial thank you!
loved it! can you make a tutorial all about post processing effects on the camera!
you could also make it so if the gun isn't in view the gun wont fire then you could make a cube that has all 3 guns attached to it and it spins a certain amount when you press a button
How bout a tutorial about aiming down sights?
Very good tutorial, helped me alot. Can you make a tutorial on how to add animations when switching weapons.Thanks again
A better way for wrapping around would be using variable % number, I remember using it in another coding language but I'm not sure if it works in C#, get back to me on that, thanks
can you connect this to the fps series please
wrote the code step by step like in video but when i run the game..all weapons are enabled when selectedweapon index is 0 and when is 1 all weapons are disabled(((
Simple & effective, thank you for your tutorials. Keep it up, and don't mind your view counts, you're great :).
Amazing tutorial!
Another brilliant video Asbjørn. Any idea on how to do ammo switching?
thanks for your generous sharing. :)
You do have to add the number keys code for each weapon you add though.
You don't need to add it for the mouse wheel though.
There are ways to make that automated, but it's better not to get into it for a beginner tutorial.
re: 6:05. I think a better solution would be (in pseudocode) "selectedWeapon++; selectedWeapon = selectedWeapon % weaponList.length". And the same, but with "selectedWeapon--" for scrolling down. Same effect, but a lot cleaner IMO
THANK YOU MAN.You r the best!!!!!!.
simply dope
Hey can you make a video about how to make a car moove? Great job on your videos man!
So the foreach (Transform weapon in transform) line searches only children transform, not grandchildren?
Hello Brackeys, can you make a Weapon pick up tutorial please, like picking a weapon by pressing 'F' and and 'Q' to drop. To be simple, like COD weapon picking...(pick, drop, swap)... your videos are really amazing and easy to understand... thanks...
Hey did you figure this out? I'm trying to do the same thing :)
You are a great guy !! Love you
i know its 6 years old but its almost at 10k likes we only need 8 more
5:50 well we can easily do that by
selectedWeapon++;
selectedWeapon%=transform.childCount;
That means whenever selectedWeapon gets to the limit.. it will be set to 0
its just to make the beginners understand
For some people like me you might have this problem where when foreach the transform, it acess the transform on the WeaponSwitcher too, so when you use your scroll wheel and keep scrolling up, you could end up in a empty space where the player is holding nothing. To deal with that, I add - public Transform weaponSwitcher -, and under the foreach -if(weapon.transform.IsChildOf(weaponSwitcer)){//rest of the code}