- Home /
Weapon switching
Hello everyone, I am facing a little problem with weapon switching in my game. How to do it properly?
First of all, my game is local multiplayer shooter. That means, I have several players in a scene and several weapons available to pick up/being held by each player. I am planning to have very big variety of weapons and considerable amount of players. That is why I think following solution is a very bad way how to do it:
Spawn each player with every weapon in the game, current weapon as active=true and possibly owned weapons as owned=true/false. Something like this in a script:
public GameObject[] weapons;
//somewhere in the code, "num" being currently owned weapon which I want to switch to
public void ChangeWeapon(int num)
{
for(int i = 0; i < weapons.Length; i++)
{
if(i == num)
weapons[i].gameObject.SetActive(true);
else
weapons[i].gameObject.SetActive(false);
}
}
And in the scene, drag every possible weapon to the public weapons array.
This is imho very bad way of doing this, because that means lots and lots of unnecessary objects will be in the scene (all players + all possible weapons for each of them). Performance will take a big hit I think.
Any ideas how to do this in more efficient way?
EDIT: I was thinking about loading(and instantiating) a prefab of desired weapon each time and setting it's position etc to the current weapon's position, setting parent to player object and then deleting the old weapon. Problem is, I seem to not be able to successfully do that. I am not able to instantiate the new weapon properly and give it all the parameters I need in the Awake() function - I am setting for example id of player(owner) like this:
GetComponentInParent<Player>().playerNumber
And even if - I'm not sure if this is the right solution performance-wise.
Worry about performance when/if you need to worry about performance. I used to play around with FPS Constructor that package also abased around having one of each possible weapon assigned in a list. By the way its free now, so might learn a few tricks from it.
To the right on this page there is a related Questions box, I bet a solution can be found there as well.
Giving every player every weapon is poor choice for gameplay since it does not encourage players to think strategically about the best weapon for them, the map, etc. It would be better (imho) to limit the number of each weapon type so that there are not enough to go around all players. As for the weapons themselves, you could create an array of weapons which can then be used when found/picked up/selected. The array means that GameObjects are not continuously being destroyed/instanced and will save you processor time.
With regards to your loop, you're right it's not good. A better choice would be to remember the index of the last weapon used. The function for change is then simple with no loop: disable the weapon of the old index, enable weapon of new index, update old index to equal new index.
With regards to the above though, in general looking for performance improvement gains at the beginning of game development is not going to help you much. You should just build a prototype game that runs with an ok Frame Per Second - improving only when further features become impossible to add because your existing code is so slow. Once you have your basic prototype, then you look back and reassess what you know, then rebuild with a proper architecture. Don't be afraid to have a code base littered with comments and hacks - just write little notes to self as you go (and then sort it all out with the proper design later)
in general looking for performance improvement gains at the beginning of game development is not going to help you much
This actually goes agains my whole process of doing things, but I will definitely try to switch to that with this game. It's just I find very difficult to re-factor my code after it's somehow finished and to find out new, better way if I already did it differently before.
But anyway, generaly, theoretically speaking, is my assumption right or completely wrong? Is performace affected more by, let's say
5 different players, 15 different weapons, each weapon gameObject is present in a scene, just as active=false. That makes 75 objects just for the weapons. $$anonymous$$s to many Objects in the game (memory issues?).
5 different players, 15 different weapons, instantiating/loading another weapon mesh+behaviour every time player switches weapon, or finds weapon on a ground. $$anonymous$$s to less objects, but more computational power I guess (CPU issues?).
Now I'm thinking about it, I'm not fully convinced that 2 is overall better for performance anyway.
@jiri.vyc $$anonymous$$y paragraph two will help with your original questions of removing the loop. A bank of weapons which can be reused will help performance as in paragraph one.
As for your other questions, they're more suited to the forums as they involve discussion about what else is going on in your game which deter$$anonymous$$es whether or not something is going to affect your game, whether it's worth doing something etc.
Answer by amiel_ace · Nov 08, 2014 at 06:10 PM
You don't actually have to use separate objects for different weapons. Why not try making an empty game object for the pick-ups and another one for the real weapons equipped. Then this way all you need to do is change their mesh and some behaviors, So you only got two objects.
The OP's original statement included:
weapons available to pick up/being held by each player
...Therefore the separate objects already exist (in order to be visible in the field). I'm not sure how dynamic modification of additional two objects per player will assist?
Your answer
Follow this Question
Related Questions
Weapon Switch System for Gear VR Controller 0 Answers
Weapon Switching 2 Answers
Switching weapons animation with mecanim? 2 Answers
GUI display weapon name you are looking at 1 Answer
Weapon shooting mechanism problem. 1 Answer