Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Particle system used for projectiles

Discussion in '2D' started by NeilB133, Jun 5, 2022.

  1. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    207
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    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.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    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.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    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.
     
    NeilB133 likes this.
  3. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    207
    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.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,320
    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. :)
     
  5. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    207
    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!
     
  6. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    207
    This was a nice video in relation to movement
    if anyone is reading this and wants to learn :)
     
  7. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    207
    Testing out the AddForce to a rigid body approach :)
     
  8. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,677
    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.

    Since we are talking about tutorials already.. This one should be worth a watch: https://m.youtube.com/watch?v=wZ2UUOC17AY
     
    Ryiah likes this.