DemoProductions / shmup

3 stars 2 forks source link

Weapon as gameobject #58

Closed ghost closed 8 years ago

ghost commented 8 years ago

And scene update

Also noticed I have some changes made previously here. Don't want to redo the changes I just made >_>. They should be harmless though

flip40 commented 8 years ago

Good so far, but I think we should go a bit further... Instead of having the weapon already attached as a child of the player, lets instead instantiate the weapon on Player.Start() and set it's parent (would be something like weapon.parent = this.gameobject). Also have a weapon variable that is just public GameObject weapon that can hold the weapon prefab like the Weapon holds bullet prefabs.

Doing this means we can make multiple weapon prefabs and assign these to the player's weapon variable, allowing easier editing of the weapon or swapping of Weapons.

There is definitely more we would need to do to optimize a system like this but I think it seems like the best way to go. The more modularity we have, the easier it is to swap things out on the fly via the editor.

(Just as an example of the optimizations we will need, swapping weapons by Destroy(); Instantiate(); would be costly, but at the moment we don't have multiple weapons either... the solution however would be to simply Instantiate new weapons, and then rely on SetActive to swap weapons)

ghost commented 8 years ago

Updated the branch. Did the same for Enemy

flip40 commented 8 years ago

I'll review this tonight ..

ghost commented 8 years ago

Ok no rush

flip40 commented 8 years ago

Grabbing the Weapon component each frame with GetComponent is somewhat inefficient, though that is entirely my fault for insisting on having Weapon as a GameObject so strongly. The initial solution would be to just have a Weapon variable to store the component so we don't have to call it repeatedly, although this also sucks because having a variable for instantiating the game object and one for the component is messy looking. I did some research and apparently (thankfully) Unity can instantiate a GameObject by its attached component (yay!) So I have just changed the GameObject references to Weapon, and returned the shooting to weapon.Shoot(), and so we can get the game object instantiated, and yet only keep track of the Weapon component variable we care about for the Shoot() calls.

Other than that small change, everything looks good, will merge.