Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Spawning particle effect on particle death

Discussion in 'Scripting' started by VentariWylde, Jul 29, 2023.

  1. VentariWylde

    VentariWylde

    Joined:
    Jun 16, 2021
    Posts:
    12
    So i have this bullet hell spawner here -

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletHellSpawner : MonoBehaviour
    6. {
    7.     public int number_of_columns;
    8.     public float speed;
    9.     public Sprite texture;
    10.     public Color color;
    11.     public float lifetime;
    12.     public float firerate;
    13.     public float size;
    14.     private float angle;
    15.     public Material material;
    16.     public float spin_speed;
    17.     private float time;
    18.     private ParticleSystem[] childSystems;
    19.  
    20.     public ParticleSystem system;
    21.  
    22.     private void Awake()
    23.     {
    24.         Summon();
    25.     }
    26.  
    27.     private void FixedUpdate()
    28.     {
    29.         system = GetComponentInChildren<ParticleSystem>();
    30.  
    31.         if (system != null)
    32.         {
    33.             system.GetComponent<Renderer>().sortingLayerName = "Weapon";
    34.         }
    35.  
    36.         childSystems = GetComponentsInChildren<ParticleSystem>();
    37.         foreach (var system in childSystems)
    38.         {
    39.             system.GetComponent<Renderer>().sortingLayerName = "Weapon";
    40.             var collisionModule = system.collision;
    41.             collisionModule.enabled = true;
    42.             collisionModule.type = ParticleSystemCollisionType.World;
    43.             collisionModule.mode = ParticleSystemCollisionMode.Collision2D;
    44.             collisionModule.dampen = 1f;
    45.             collisionModule.bounce = 0f;
    46.             collisionModule.lifetimeLoss = 1f;
    47.         }
    48.  
    49.         time += Time.fixedDeltaTime;
    50.  
    51.         transform.rotation = Quaternion.Euler(0,0, time * spin_speed);
    52.     }
    53.  
    54.     void Summon()
    55.     {
    56.         angle = 360f / number_of_columns;
    57.  
    58.         for(int i = 0; i<number_of_columns; i++)
    59.         {
    60.             Material particleMaterial = material;
    61.  
    62.             var go = new GameObject("Particle System");
    63.             go.transform.Rotate(angle * i, 90, 0);
    64.             go.transform.parent = this.transform;
    65.             go.transform.position = this.transform.position;
    66.             system = go.AddComponent<ParticleSystem>();
    67.             go.GetComponent<ParticleSystemRenderer>().material = particleMaterial;
    68.             var mainModule = system.main;
    69.             mainModule.startColor = Color.green;
    70.             mainModule.startSize = 0.5f;
    71.             mainModule.startSpeed = speed;
    72.             mainModule.maxParticles = 100000;
    73.             mainModule.simulationSpace = ParticleSystemSimulationSpace.World;  
    74.  
    75.             var emission = system.emission;
    76.             emission.enabled = false;
    77.  
    78.             var forma = system.shape;
    79.             forma.enabled = true;
    80.             forma.shapeType = ParticleSystemShapeType.Sprite;
    81.             forma.sprite = null;
    82.  
    83.             var text = system.textureSheetAnimation;
    84.             text.mode = ParticleSystemAnimationMode.Sprites;
    85.             text.AddSprite(texture);
    86.         }
    87.         InvokeRepeating("DoEmit", 0f, firerate);
    88.     }
    89.  
    90.     void DoEmit()
    91.     {
    92.         foreach(Transform child in transform)
    93.         {
    94.             system = child.GetComponent<ParticleSystem>();
    95.             var emitParams = new ParticleSystem.EmitParams();
    96.             emitParams.startColor = color;
    97.             emitParams.startSize = size;
    98.             emitParams.startLifetime = lifetime;
    99.             system.Emit(emitParams, 10);
    100.         }
    101.     }
    102. }
    and i want to make it so when the individual particle colliders with something and disappears to spawn a small particle effect on the point of collision to simulate the particle breaking.

    for 1: is this even possible?
    and 2 how could i achieve this?

    thanks to anyone that can help
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    You can use a sub emitter. So when a particle collides it will spawn another particle system at the point of impact.

    Warning - The sub emitter needs to be in burst mode (not looping) or it won't work. You enable burst mode from the Emission module.
     
  3. VentariWylde

    VentariWylde

    Joined:
    Jun 16, 2021
    Posts:
    12
    i have experimented with sub-emitters and that is the perfect solution, but when i set it to play on "collide" or "death" it will not play but it does work on "birth" any idea why?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Anytime you have the thought "any idea why?" that means ...

    Time to start debugging!

    Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,833
    Kurt-and-Paste-without-Reading strikes again.
     
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,833
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    If you wish to remote-debug a piece of code using 57 GetComponent<T>() calls, be my guest.

    I would simply prefer OP does work that and finds his own problem. I think it will save everybody else a lot of time.

    Also, for OP, just so you know:

    Keep in mind that using GetComponent<T>() and its kin (in Children, in Parent, plural, etc) to try and tease out Components at runtime is definitely deep into super-duper-uber-crazy-Ninja advanced stuff.

    This sort of coding is to be avoided at all costs unless you know exactly what you are doing.

    If you run into an issue with any of these calls, start with the documentation to understand why.

    There is a clear set of extremely-well-defined conditions required for each of these calls to work, as well as definitions of what will and will not be returned.

    In the case of collections of Components, the order will NEVER be guaranteed, even if you happen to notice it is always in a particular order on your machine.

    It is ALWAYS better to go The Unity Way(tm) and make dedicated public fields and drag in the references you want.

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

    If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way(tm) success of accessing things in your game.
     
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,833
    FFS, Debug.Log isn't going to help find problems in how a Particle System works, it's a black box.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    ...that we know works perfectly.

    And we know it requires sub-emitters to be children of parent.

    And we know that this method:

    returns both self AND children.

    And then the loop iterating childSystems doesn't discern self or children: it just does the same stuff for all, both main and sub particle systems.

    And it runs every FixedUpdate() (which should be in Update() )

    I'm simply saying that the complexity of all that getting and getting in children every frame is almost certainly the cause of OP's woes, that's all.

    And yes, I did the work: see attached .unitypackage for fully-operational example.

    I bet if you put OPs script on my scene here that it will suddenly stop working.

    Screen Shot 2023-07-30 at 10.01.14 AM.png
     

    Attached Files:

    Last edited: Jul 30, 2023