- Home /
Weapon switching (a lot)
I have a huge number of weapon in my game and I want the most efficient way to switch between them. I know you can disable and enable weapon but I don't think its the most efficient way.
Thanks
Sorry for my english (I'm french)
How many is a lot of weapons?
Even though being able to potentially cycle through 20 or so weapons on command would be useful, if it's a fast action game requiring a keyboard it is usually most effective to just allow the player to map their 5 most favored weapons to hotkeys. The limitation of hotkeys serves as a sense of balance and gives the player the responsibility of making decisions.
If this is a slower paced game which still uses a keyboard, I'd recommend a shell like system for example the ' (apostrophe key) opens up a command input, you type in the name (or the shortcut of the name) and press enter and then it switches weapons accordingly.
e.g
'swd9
Opens up the prompt, changes to equip sword 9 (swd9 is a shortcut that the user can customize themselves)
If this is a console game, I liked how in Turok 2 (I think), you hold a button and it shows a UI, you then press the analog stick in the direction of the weapon you want to switch to and then release the held button to switch to it.
I want to be able to add weapon later in développement so I expect there will be a lot of weapon at the end. Thanks
Answer by Dibbie · Nov 19, 2016 at 08:38 AM
The easiest and most efficient way I can think of, is to create a weapon switching script on the player, with 2 public variables: 1. for the current weapon they are using 2. for the array of all their possible weapons
You can then code it however youd like to switch weapons with the scroll wheel or input keys, and change the current weapon, to the next element of your array (depending on the direction it should be searching in the array - if they want to switch to a previous weapon for example, it would search 1 UP(--) the array, and next weapon would search 1 DOWN(++) the array)
Generally, that could look something like (C#, all untested code):
//global variables
public GameObject currentWeapon;
public Transform playersHands; //you dont technically NEED this, if you dont have a lot going on in your game, to do a GameObject.Find(), otherwise, it would be best to know exactly where to place the weapon
public GameObject[] WeaponInventory;
private int index = 0; //this will keep track of where you are in your array, when switching weapons
//in Start
currentWeapon = WeaponInventory[0]; //the first index of the array is the weapon they will start with - change "0" to a different number if you want them to start with a different weapon
//in Update
if(Input.GetKeyDown(KeyCode.Q){
//switch to the NEXT weapon
if(index + 1 > WeaponInventory.Lenth){ index = 0; } else { index++;} //keep index, within the bounds of the array -- if they are on the last weapon of the array, loop back to the first weapon
currentWeapon = WeaponInventory[i]; //switch the weapon
if(hands.childCount > 0){ //if the player HAS a weapon currently out
Destroy(hands.GetChild(0)); //destroy their current weapon
}
GameObject weapon = (GameObject) Instantiate(currentWeapon,hands.position, hands.rotation); //create their selected weapon at their hands
weapon.transform.parent = hands; //parent the weapon to their hands
//at this point here, is where you can play the weapons "withdraw" or "switching" animation if you have one.
}
You could even go so far to say you dont even need "currentWeapon", and use the array at the index in the Instantiate.