Search Unity

[SOLVED] Activate and Deactivate a Particle System as the player leaves

Discussion in 'Scripting' started by busterlock, Mar 4, 2018.

  1. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    In order not consume too much memory, I decided to create many particle systems which are initially off, and as the player walks under them, one of them turn on, while the rest remain off until the player walks under it too (as he leaves, the particle system turns off).
    I figured I did this by placing a Box Collider in each Particle System, check the "Trigger" option and simply attach the next script to my player:

    Code (CSharp):
    1.     public GameObject animator;
    2.  
    3.     void Awake()
    4.     {
    5.         animator.gameObject.SetActive(true);
    6.     }
    7.  
    8.     void OnTriggerEnter(Collider other)
    9.     {
    10.         if (other.gameObject.tag == "Particle")
    11.         {
    12.             animator.gameObject.SetActive(true);
    13.         }
    14.     }
    15.  
    16.     void OnTriggerExit(Collider other)
    17.     {
    18.         if (other.gameObject.tag == "Particle")
    19.         {
    20.             animator.gameObject.SetActive(false);
    21.         }
    22.     }
    23. }
    As you can imagine, it's not fully working. While the particle system is turning off/on, the reason it's not working, well, I move my player with a Nav Mesh Agent and Raycast, and the mouse position I click in, while inside the collider is (very often) not detected.

    So how in the world can I solve this?

    Thanks!
     
    Last edited: Mar 4, 2018
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I'm not sure what your animator GameObject is, I'm not sure what you are trying to do with it.

    If the other tag is Particle, you want to turn on or off that Particle, but you're targetting the animator object and turning it on and off. Which, btw, since it's declared as a gameobject, you don't have to do animator.gameObject, just animator.SetActive will work.

    So if you are trying to turn on the particle, you can do.

    Code (CSharp):
    1. public GameObject animator;
    2.     void Awake()
    3.     {
    4.         animator.gameObject.SetActive(true);
    5.     }
    6.     void OnTriggerEnter(Collider other)
    7.     {
    8.         if (other.gameObject.tag == "Particle")
    9.         {
    10.             other.gameObject.SetActive(true);
    11.         }
    12.     }
    13.     void OnTriggerExit(Collider other)
    14.     {
    15.         if (other.gameObject.tag == "Particle")
    16.         {
    17.             other.gameObject.SetActive(false);
    18.         }
    19.     }
    20. }
     
  3. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    The problem is not turning it on or off, (that part) of the script worked just fine! When I stepped inside the collider the particle system turned on, when I stepped off, it turned off. The problem is that once the character is inside the particle system (a blizzard), the raycast (player movement) doesn't detect precisely the points I'm hitting.

    Is there a way I can stop the collider from interfering with my raycast, but still detect the collision with the player?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    busterlock likes this.
  5. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    In case someone every comes in here looking for an answer, here's how I solved it, I simply attached each particle system to its own collider, activated the "Is Trigger", decreased the size of the collider (in the y axis), and the raycast worked perfectly.

    The script is as follows:

    Code (CSharp):
    1.     public GameObject animator;
    2.  
    3.     void Awake()
    4.     {
    5.         animator.gameObject.SetActive(true);
    6.     }
    7.  
    8.     void OnTriggerEnter(Collider other)
    9.     {
    10.         if (other.gameObject.tag == "Player")
    11.         {
    12.             animator.gameObject.SetActive(true);
    13.         }
    14.     }
    15.  
    16.     void OnTriggerExit(Collider other)
    17.     {
    18.         if (other.gameObject.tag == "Player")
    19.         {
    20.             animator.gameObject.SetActive(false);
    21.         }
    22.     }
    The script is attached to the collider, and the particle system is a child to the object!
     
  6. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi busterlock, I have a scene with a third person controller that opens a chest with a trigger. I would like the same trigger to activate the particle system too. Your trigger can work for this, because I tried but it does not work.
     
  7. gysmo_the_diamond21

    gysmo_the_diamond21

    Joined:
    Feb 9, 2023
    Posts:
    1
    I would recommend to utilise the "Animator.gameObject.SetActive(True);" And just stick it onto the script that opens the chest with a trigger