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

Controlling Particle System through scripting

Discussion in 'Scripting' started by Sapietum, Jan 29, 2015.

  1. Sapietum

    Sapietum

    Joined:
    Jan 29, 2015
    Posts:
    4
    Hi all,

    I'm trying to create a trailing shadow effect with a particle system, and I pretty much have it working, except for one small thing.

    In this video, you can see it's current state:


    What is supposed to happen, is that when the creature reaches the green fog, I begin playing the particle system, which then emits trailing shadows, but as you can see, when it starts, the particle system is not empty.
    Below you can see my particle systems settings.



    In my script, I use it as following:

    Code (CSharp):
    1.     void Start () {
    2.         particles.Stop();
    3.         particles.Clear();
    4.     }
    5.  
    6.     public void DetectSpore () {
    7.         particles.Clear();
    8.         particles.Play();
    9.     }
    10.  
    I feel like I've tried everything to try and get rid of the pre-emitted shadows, when I .Play() the particle system, but nothing seems to work...
    I've also tried with playOnAwake set to false...

    Any idea what I'm doing wrong?
     
  2. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
  3. Sapietum

    Sapietum

    Joined:
    Jan 29, 2015
    Posts:
    4
  4. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    Can you show more of your code? Is the particle system a child of the GameObject that collides with the green fog?
     
  5. Sapietum

    Sapietum

    Joined:
    Jan 29, 2015
    Posts:
    4
    I'm afraid I can't show too much of my code, but yes. The particle system is a child to a GameObject.


    The green fog has a collider attached which acts as a trigger and a kinematic RigidBody2D, and the GameObject that the particle system is attached to has a collider and a RigidBody2D.
    Below you can see a bit of the setup on the GameObject "FungusEnemySmall" from above, that has the Particle System as a child and connected through an outlet in the editor.



    Here's a sample of my code some of the code... The code that's missing shouldn't have anything to do with this, but is mostly applying damage to the enemies and handling animation states.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy : MonoBehaviour {
    5.  
    6.     private Animator animator;
    7.  
    8.     public GameObject spore;
    9.     private GameObject state;
    10.     public GameObject piercingShotFX;
    11.     public GameObject bombShotFX;
    12.     public GameObject fireFX;
    13.     public GameObject iceFX;
    14.  
    15.     public ParticleSystem particles;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         animator = GetComponent<Animator> ();
    20.         rotation = gameObject.transform.rotation;
    21.         x = gameObject.transform.position.x;
    22.         previousY = gameObject.transform.position.y;
    23.         state = GameObject.Find("GameState");
    24.  
    25.         animator.SetInteger ("Health", health);
    26.  
    27.         particles.Stop();
    28.         particles.Clear();
    29.         particles.enableEmission = false;
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update () {
    34.         if (smellingSpore && !crazed) {
    35.             if (animator.GetCurrentAnimatorStateInfo(0).IsName("Base.RunAnimation")) {
    36.                 crazed = true;
    37.             }  else if (animator.GetCurrentAnimatorStateInfo(0).IsName("Base.SmellSporeAnimation")) {
    38.                 return;
    39.             }
    40.         }
    41.  
    42.         if (hit) {
    43.             if (animator.GetInteger ("AnimState") == 1 && animator.GetBool ("Hit")) {
    44.                 if (animator.GetCurrentAnimatorStateInfo(0).IsName("Base.HitAnimation")) {
    45.                     hit = false;
    46.                     animator.SetBool ("Hit", false);
    47.                 }
    48.             }  else {
    49.                 animator.SetInteger ("AnimState", 1);
    50.                 animator.SetBool ("Hit", true);
    51.             }
    52.         }  else {
    53.             if (animator.GetInteger ("AnimState") == 1) {
    54.                 animator.SetInteger ("AnimState", 0);
    55.             }
    56.  
    57.             if (animator.GetBool ("TurnToSpore")) {
    58.                 return;
    59.             }
    60.  
    61.             if (!animator.GetCurrentAnimatorStateInfo(0).IsName("Base.HitAnimation") &&
    62.                 !animator.GetCurrentAnimatorStateInfo(0).IsName("Base.DieAnimation") &&
    63.                 animator.GetInteger ("AnimState") != 4) {
    64.                 MoveGameObject ();
    65.             }
    66.         }
    67.     }
    68.  
    69.     void OnTriggerEnter2D(Collider2D other)
    70.     {
    71.         if (animator.GetCurrentAnimatorStateInfo(0).IsName("Base.DieAnimation") || health < 1) {
    72.             return;
    73.         }
    74.  
    75.         // Hit detection
    76.     }
    77.  
    78.     void Hit (Collider2D other, int damage) {
    79.         other.GetComponent<Arrow>().OnHit();
    80.  
    81.         if (frozen) {
    82.             animator.SetBool("Destroy", true);
    83.         }  else {
    84.             hit = true;
    85.             UpdateHealth(damage);
    86.         }
    87.     }
    88.  
    89.     void HitEffect (int damage) {
    90.         if (frozen) {
    91.             animator.SetBool("Destroy", true);
    92.         }  else {
    93.             hit = true;          
    94.             UpdateHealth(damage);      
    95.         }
    96.     }
    97.  
    98.  
    99.     void MoveGameObject () {
    100.         float positionY = transform.position.y;
    101.  
    102.         if (enemySize == EnemySize.Small) {
    103.             if (crazed) {
    104.                 positionY -= speedEnemySmallCrazed;
    105.             }  else {
    106.                 positionY -= speedEnemySmall;
    107.             }
    108.         }  else if (enemySize == EnemySize.Medium) {
    109.             if (crazed) {
    110.                 positionY -= speedEnemyMediumCrazed;
    111.             }  else {
    112.                 positionY -= speedEnemyMedium;
    113.             }
    114.         }  else if (enemySize == EnemySize.Large) {
    115.             if (crazed) {
    116.                 positionY -= speedEnemyLargeCrazed;
    117.             }  else {
    118.                 positionY -= speedEnemyLarge;
    119.             }
    120.         }
    121.  
    122.         previousY = positionY;
    123.         transform.position = new Vector3 (transform.position.x, positionY, transform.position.z);
    124.     }
    125.  
    126.     void OnBecameInvisible() {
    127.         Destroy(gameObject);
    128.     }
    129.  
    130.  
    131.     bool ConvertToSpore () {
    132.         float trigger = 100 * sporeTriggerAppearance;
    133.         float random = Random.Range(0, 100);
    134.  
    135.         if (random > trigger) {
    136.             return false;
    137.         }  else {
    138.             return true;
    139.         }
    140.     }
    141.  
    142.     IEnumerator InstantiateSpore () {
    143.         animator.SetBool ("TurnToSpore", false);
    144.         yield return new WaitForSeconds(0.2f);
    145.  
    146.         Instantiate (spore, transform.position, Quaternion.identity);
    147.         yield return null;
    148.     }
    149.  
    150.     public void DetectSpore () {
    151.         animator.SetInteger ("AnimState", 2);
    152.         animator.SetBool ("Crazed", true);
    153.         smellingSpore = true;
    154.  
    155.         particles.Clear();
    156.         particles.Play();
    157.         particles.enableEmission = true;
    158.     }
    159. }
    160.  
     
  6. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    aah this looks like it has something to do with your states/animation logic.
    Can you fire up the Animator view and see which states the enemy
    is in ? I think this will give you a clue.

    Also, you might want to set that particles.gameobject.SetActive(false) and later set it to true
    when you want ?
     
  7. Sapietum

    Sapietum

    Joined:
    Jan 29, 2015
    Posts:
    4
    SetActive actually did the trick alone! Thanks a lot!