I’ve heard that it’s not an optimal thing to use a particle system for projectiles that damage enemies. So essentially I’ve added a sprite to the particle, and made the particle a trigger. With this I could control how the particles move, the frequency of the particles, speed etc.
The other way I’ve done it is to create a game object, assign it a sprite, a collider and instantiate it when it needs to be shot/thrown at enemies.
if the first method (the particle way) is not a good way, why is this not good? Is it because it’s too much processing power when particles are used in a way as projectiles when hitting other colliders? Like an enemy collider.
This all sounds like you’re chasing fantasy ghost problems.
DO NOT OPTIMIZE “JUST BECAUSE…” If you don’t have a problem, DO NOT OPTIMIZE!
If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:
Window → Analysis → Profiler
Failure to use the profiler first means you’re just guessing, making a mess of your code for no good reason.
Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.
Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.
At a minimum you want to clearly understand what performance issues you are having:
running too slowly?
loading too slowly?
using too much runtime memory?
final bundle too large?
too much network traffic?
something else?
If you are unable to engage the profiler, then your next solution is gross guessing changes, such as “reimport all textures as 32x32 tiny textures” or “replace some complex 3D objects with cubes/capsules” to try and figure out what is bogging you down.
Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.
This sort of speculative optimization assumes you’re properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
So you’re saying just go for it whatever way, then if you have problems fix them after? I just didn’t want to start off a particular way that isn’t best practise, to find out later it all needs to be scrapped because the approach is bad for X reason.
If you’ve not added a Rigidbody2D which is the only thing that really moves in 2D physics then moving a Collider2D means you were doing so with a Transform which is the worst thing you can do in 2D physics. The performance there would be terrible.
The trick is to try to understand things or do it at scale on your device and as Kurt suggests, use the profiler. Whatever you’re doing might not be efficient but at the scale you’re doing it it might not matter.
This is the point, you don’t have to commit to anything then have to change it all if you quickly write a prototype to test it. The alternative you’re leaving yourself to is asking a stranger on the Internet and assuming they are correct.
Thank you both for your answers here Yeah I was moving it with a transform, as I wanted the projectile to pass through enemies and I thought that adding a rigid body would cause it to not pass through the enemy. I’m super new to unity and not yet worked with the profiler, I’ll have a look into it!
Unless you make some rough mistake (which is naturally well possible if you start out), the particles way is normally the WAY more performant one (A. because it uses simplified circle/sphere physics and B. rendering happens internally all at once). For an actual bullet hell game (where you want to have hundreds of bullets), I’d also recommend that solution.
The downside is, it’s a bit more unwieldy because a particle system is rather designed for effects and not so much gameplay elements. Still it can clearly be done.
Rigidbodies are the more intuitive way. Those are meant for anything that’s supposed to move by physical rules, aka represent something physical. Just do not go into higher tripple digits with them if you aim for low end hardware or mobile.