jongallant / Unity-Bullet-Hell

An extremely efficient projectile generator for unity.
MIT License
308 stars 40 forks source link

Guideline for Emitters attached to game objects with lyfecycle? #6

Closed Oskar619 closed 4 years ago

Oskar619 commented 4 years ago

I'm currently working on a project that has the following conditions:

Every time I switch a weapon, and therefore a new Emitter is created, I get a weird Overflow Exception in IndirectRenderer class.

I did some dive into the code, and the issue seems to happen because the previous emitter, will pass all the assigned projectiles + 1 to the new emitter active projectiles. This results in 50001 which overflows the TransformBuffer in the Draw method for IndirectRenderer.

Here's what I'm doing every time I equip a new weapon:

// I set emitter prefab from the editor. 
// emitterPosition is also set in editor, and it's an empty transform containing the position where I want the emitter to be.
GameObject emitter = Instantiate(emitterPrefab, emitterPosition); 

// Add the emitter to the Projectile Manager.
ProjectileManager.Instance.AddEmitter(emitter.GetComponent<ProjectileEmitterAdvanced>(), 100);
// Register this emitter.          
ProjectileManager.Instance.RegisterEmitter(emitter.GetComponent<ProjectileEmitterAdvanced>());

I tried fixing the bug, but I still need to familiarize a little more with the process. I'd be happy to help out fixing it, but I'd like to know first your insight on what is going on, and how I Could handle multiple dynamic emitter instantiations or, in other words, emitters attached to equipped weapons aligned to their lyfecycle.

Edit

I tried with two different prefabs, and the bug was gone. I think I'm ok with creating a new emitter per weapon, I don't think this is a bug anymore.

Thanks for this awesome lib !

Edit 2

Wow, I was so excited by seeing the bug gone that I didn't pay attention that also the running particles that were created by the previous emitter (previous equipped weapon) will also dissapear when I switch weapons.

So this is no longer the bug I created for this issue, but I'd love some insight on this issue, and what would you recommend doing.

Edit 3

I was able to make it work. I had to do some trickery.. For instance, I had to implement a class that handles the recycling of these emitters, if a weapon is equipped that needs an emitter that has been instantiated before, we return the instance of the emitter instead. Before destroying the equipped weapon, I had to unattach this emitter from it to avoid it being destroyed.

This seems to make everything work as expected. Just one quick question for you: Do you think this implementation is the right one? Is there anything you'd recommend for dynamic usage of emitters?