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*.gameObject.SetActive(true);*
else
weapons*.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().playerNumber
And even if - I’m not sure if this is the right solution performance-wise.