I’m working on a 2D Platformer, think Mario Brothers or old school Mega Man.
I want the player to be able to equip different items/weapons, and don’t want to have to do a full sprite sheet and animation of every possible combination, so instead of using traditional sprite sheets, each element of my character is its own image, Head, torso, left arm, right arm, left leg, right leg.
So in the editor, all I have to do to give it a weapon, is attach the weapon as a child element to the characters hand.
What’s holding me up is how to go about letting the player change weapons.
I’ve come up with two different ways to do it, and just want to know which you all think is best, or if there is a better way I’m not thinking of.
The first way to do it is to add every weapon as its own game object under the character rigs hand in the hierarchy,
then just have the controller script turn off all but the one the player is using.
I just wonder if having all those disabled game objects (we’re talking like maybe 10-20) will slow down the game at all, or have any other problems I’m not considering.
The other way is to have just one game object under each hand, and create a sprite array assigning each of the sprites to it, and changing which sprite is displayed via code.
The first way to do it is to add every weapon as its own game object under the character rigs hand in the hierarchy,
then just have the controller script turn off all but the one the player is using.
This feels like it would be the easiest/quickest way to achieve what you want to do, and as such I have to dismiss the idea instantly - if you do not go through at least a little pain and suffering then it cannot be the right way! Joking aside, to me, this feels like not the best way to do it, having all of those disabled game objects which the player may not use… just feels a bit, well, wrong.
The other way is to have just one game object under each hand, and create a sprite array assigning each of the sprites to it, and changing which sprite is displayed via code.
I definitely prefer the sound of this second approach. Thinking back to the Glitch Garden game as a point of reference. Each defender had a game object align with it’s, let say hand, which was named “Gun”, this was effectively your empty game object. This I believe had the projectile script attached to it which in turn had a prefab dragged into it for the corresponding “thing to throw”. It feels remarkably similar to what you want to do.
If you have the game object on the hand of the character, perhaps called “weapon”, and in the same way in Glitch the prefab was attached, instead do that programmatically, creating a new instance of the corresponding weapon prefab. Your “weapon” game object could perhaps then have a fairly standardised script for commonality between the weapons (I’m guessing each weapon will do something a little difference other than appearance etc)… so, if every weapon has a “damage” for example… and then for weapon specific stuff, have that in a script on the individual weapon prefabs - with the appropriate sprites.