Search Unity

particle system trigger

Discussion in 'Editor & General Support' started by txarly, Apr 8, 2018.

  1. txarly

    txarly

    Joined:
    Apr 27, 2016
    Posts:
    197
    hey,

    I have a house in my game and don´t want the weather particles come inside.So i am trying to use the triggers option in the particle system.I created a trigger (Cube) over my house to work as a trigger(is trigger checked).I am trying to assign this trigger to the collider list, but doesn´t allow me to do.
    The particle system spawns on runtime as part of the weather system.In playmode,if i select the particle system in runtime,it lets me to assign the trigger without problems and works well, but in the editor i am trying to create a list of triggers(houses) and doesn´t allow me.Any idea why?

    thanks to all

    trigger.png
     
  2. OneManBandGames

    OneManBandGames

    Joined:
    Dec 7, 2014
    Posts:
    207
    I think the problem is that you are trying to assign a collider that is currently part of your scene to a particle system that is part of a prefab. Unity probably does not allow this to prevent that you use that prefab in another scene where your housecollider won't exist then.
    You could add a box collider component to your particle system prefab, and then drag the prefab itself into the collider slot, that should work then since the collider then is a part of the prefab itself. You could then arrange the box collider / the particle prefab in such way that the box collider sits in the right place.
    If you are in control of the weather spawning system you could also look into the part where the weather particle system is spawned and make sure that the right collider from the scene is assigned via script.
     
  3. txarly

    txarly

    Joined:
    Apr 27, 2016
    Posts:
    197
    Thanks Onemanbandgames .I tried the box collision, but as the weather prefab spawns child of the player(so while is moving the weather moves around him), the box collider moves too.

    About the second solution,i am not a very good coder, i have to create a list of triggers(as i have many houses) and assign them in the particle system?A bit guide on how to do this?
     
  4. OneManBandGames

    OneManBandGames

    Joined:
    Dec 7, 2014
    Posts:
    207
    There are many different ways to achieve this, I think this is one of the easier ways:

    1. Create a new tag called "SnowParticleSystemTrigger" in Unity
    2. Mark all the triggers you want to be assigned to the particle system with the new tag. The triggers need to have a collider attached to them, should work with any collider (Box, Sphere, etc.)
    3. Select your snow particle system prefab and Add a new Script to it, called "AssignColliders"
    4. Double click on the script, paste in the following code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AssignColliders : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         // The name of our custom tag for the weather particle system triggers
    10.         string tag = "SnowParticleSystemTrigger";
    11.  
    12.         //We get the Particle System Component from this gameobject first
    13.         ParticleSystem ps = GetComponent<ParticleSystem>();
    14.  
    15.         //if we can't find it, we abort with an error message
    16.         if (ps == null)
    17.         {
    18.             Debug.LogError("Could not find a particle system component on the weather particle system game object!");
    19.             return;
    20.         }
    21.  
    22.         //We look for all game objects with the weather collider tag from above
    23.         var allColliderGameObjects = GameObject.FindGameObjectsWithTag(tag);
    24.  
    25.         //we create a counter so we can add each collider on its own index later
    26.         int index = 0;
    27.  
    28.         //Now we look at each collider game object that we found
    29.         foreach (GameObject colliderObject in allColliderGameObjects)
    30.         {
    31.             //We try to get the collider component from the game object
    32.             Collider coll = colliderObject.GetComponent<Collider>();
    33.             //If we can't find the collider component, we display an error in the console so we know something is fishy
    34.             if (coll == null)
    35.             {
    36.                 Debug.LogError("Could not find a collider component on one of the weather particle system trigger objects!");
    37.             }
    38.             else
    39.             {
    40.                 //if we have found the component, we can add it to the weather particle system
    41.                 ps.trigger.SetCollider(index, coll);
    42.                 index++;
    43.             }
    44.         }
    45.     }
    46.  
    47.     // Update is called once per frame
    48.     void Update () {
    49.      
    50.     }
    51. }
    52.  
    Now you can create as many triggers/colliders as you want, as long as you tag them with the "SnowParticleSystemTrigger" tag, they will be added to the particle system as soon as it is spawned.
     
  5. txarly

    txarly

    Joined:
    Apr 27, 2016
    Posts:
    197
    I have no words.Thank you very much OneManBandGames for your help.It is working perfectly!!!.
     
    richardkettlewell likes this.
  6. CoderSi

    CoderSi

    Joined:
    Jun 1, 2018
    Posts:
    4
    Hi, after a quick search this is the closest thread i can see to my problem.

    I want to use particle collision but with triggers.

    The particle system collision module with OnParticleCollision only works with non trigger colliders. The particle system trigger module with OnParticleTrigger only works with triggers that are set in the trigger module, which is really not helpful as the triggers i want it to collide with are on various objects throughout the scene that could be instantiated or destroyed at any point.

    Is there any way to do what i want to do? The trigger module really ought to work with a layer mask like the collision module does, why why why must we specify the list of triggers in the particle system?!

    Thanks.