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

How do you make particle system follow a game object?

Discussion in '2D' started by callmepaulb, May 7, 2015.

  1. callmepaulb

    callmepaulb

    Joined:
    Apr 8, 2015
    Posts:
    2
    I'm currently making a space invaders clone and I want to make it so that when my player's laser collides with one of my enemies, that enemy will begin smoking like it's taken serious damage. I have been able to have the particle system appear once the enemy is hit but I can't get the particle system to follow the movement of the enemy. It stays in the exact place it was instantiated at. And once the enemy is killed the particle system continues to exist. Can anyone link me to a walkthrough for this or give me some tips? Thanks in advance.
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Make the game object with the particle system a child of the other object.
     
    MisterSkitz likes this.
  3. callmepaulb

    callmepaulb

    Joined:
    Apr 8, 2015
    Posts:
    2
    How do I do that? I had attached my particle system prefab named Smoke to my enemy in the hierarchy but then it the enemy starts smoking right away rather than after it has been hit.
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    I'm guessing that you instantiate the particle system prefab when you need it. That makes it a root object, though. After instantiating, parent it to the enemy:

    Code (csharp):
    1. particleSystemObject.transform.parent = enemyObject.transform;
     
    Gopern, millej23, MisterSkitz and 2 others like this.
  5. MrCringeGaming

    MrCringeGaming

    Joined:
    Apr 27, 2019
    Posts:
    6
    Umm, the problem with this is if you make the particle system a child of the parent and you want to destroy the parent you would be destroying the particle system too which would make your particle not play...So, the question still needs to be answered here. Unfortunately, i'm wondering the same thing: How do you make particle system follow a game object?....
     
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I think the answer will be almost the same :rolleyes:

    with the child befor the parent will be destroyed:
    Code (CSharp):
    1. particleSystemObject.transform.parent = null;
    without to be a child (be careful and check if this object isn't destroyed)
    Code (CSharp):
    1. void Update () {
    2. if (followedObject.activeSelf)
    3. gameObject.transform.position = followedObject.transform.position;
    4. }
     
    Last edited: Oct 11, 2019
    Tural-Muslim likes this.
  7. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    Just make an empty game object, place the ship and the particle systems as children. They will be linked and you can destroy either one.
     
  8. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Code (CSharp):
    1. yourParticleSystem.Play();
    2. Destroy(enemyObject, yourParticleSystem.main.duration);
    You'll need to declare the particle system as a variable.

    Code (CSharp):
    1. [SerializeField] private ParticleSystem yourParticleSystem;
    Edit:
    It's usually a good idea to set your duration a little longer than your particle's lifetime. Duration is default 5f (5.00) so if you make your particle life time set to say 2, then make the duration something like 2.25 or whatever looks good. Otherwise, it'll destroy the very instant it finishes which doesn't look that smooth, in my opinion, for most situations.
     
    Last edited: Oct 11, 2019
  9. L1te_

    L1te_

    Joined:
    Apr 1, 2021
    Posts:
    1
    How do you change the duration?