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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OnParticleCollision driving me nuts!

Discussion in 'Scripting' started by denholmspurr, Apr 24, 2020.

  1. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    Ok guys, I've almost had it with these Particles. Literally spent almost all of today trying to get this to work, I'm at my wits end.

    There's clearly something very simple I'm missing.

    The long question is I'm trying to get a prefab to detect collisions with particles from a specific system.

    The short question is how the heck do i even get anything to register ANY collisions with the damn thing?

    I've trawled online articles, youtube videos and documentation to no avail. I still cant recognise collisions with them.

    to work out if it's even working I've put this script on my Game Object

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class particleCollision2 : MonoBehaviour
    6. {
    7.     private void OnParticleCollision(GameObject HurricaneInner)
    8.     {
    9.         Debug.Log("Particle hit!");
    10.     }
    11. }
    which produces zero results. The particles are definitely hitting it, one of the objects i've tried it on is even partially made of the particles and it doesn't recognise those.

    here's what i've put on Particle Collider component



    Please help me before I go insane!

    P.S: the circle collider on the game object is not set to Is Trigger.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
  3. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    Yeah, that was my first port of call. I've even tried using their example script and no luck.
     
  4. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    Ok so I've got a game Object to recognise collisions - phew!

    The problem now is 2-fold.

    Firstly I still cant get it recognise collisions within the game object I need it to recognise collisions.

    It's a child of a child of a parent prefab, if that's helpful information?

    secondly, turning the "isTrigger" function off on that game object will mess with the movement of that object.
     
  5. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    Right. So I've managed to get the game object I wanted to recognise collisions. I was trying to recognise collision in the child, not the parent which has a rigidbody and so can detect the collision, and whilst I want to be able to do that eventually, it's not necessary yet.

    HOWEVER.

    Big but here. This has broken my game because I had to uncheck the "Is Trigger" function in order for this to work. The unit now stops moving across the map because it collides with other obstacles on the map.

    So i've been wondering if it's possible to use OnParticleTrigger instead of OnParticleCollision? I've been having a look at a few articles but so far no luck. If anyone has any clarity to share I'd appreciate it.

    https://forum.unity.com/threads/getting-the-collider-in-a-particle-system-trigger-event.476498/
    https://answers.unity.com/questions/1586394/how-to-make-a-particle-system-play-on-trigger-with.html
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleTrigger.html

    UPDATE:

    Here's the script I've got so far:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class particleCollision2 : MonoBehaviour
    7. {
    8.     public GameObject colliderGO;
    9.  
    10.     void OnParticleTrigger()
    11.     {
    12.         ParticleSystem ps = GetComponent<ParticleSystem>();
    13.  
    14.         // particles
    15.         List<ParticleSystem.Particle> enter = new List<ParticleSystem.Particle>();
    16.  
    17.         // get
    18.         int numEnter = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
    19.         Debug.Log(numEnter);
    20.  
    21.         // iterate
    22.         for (int i = 0; i < numEnter; i++)
    23.         {
    24.             ParticleSystem.Particle p = enter[i];
    25.             // instantiate the Game Object
    26.             Instantiate(colliderGO, p.position, Quaternion.identity);
    27.             enter[i] = p;
    28.             Debug.Log("working");
    29.         }
    30.  
    31.         // set
    32.         ps.SetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
    33.     }
    34. }
    35.  
    I know up to // get is working, however it isn't returning any results for numEnter

    I've tried with both "is trigger" checked and unchecked. I've added the component to the "Trigger" section of the particle system.

    Why isn't this recognising??
     
    Last edited: Apr 24, 2020
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Usually you just put two colliders, one for the collidin' you want and one for the triggerin' you want.

    As my Daddy always said, "Ain't no rest for a triggered collider!"
     
  7. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    The issue is, as soon as that game object contains a collider that has "Is Trigger" unchecked, it stops moving. Or am I missing something?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Physics can be a bit fiddly to get right, but there are lots of controls to mess with still. You can put your various GameObjects into different layers, then tell the Physics engine which layers collide with what (in the Project Settings -> Physics panel). I imagine there may be some awareness from particles about layers and colliders.

    As far as why your trigger-off behavior changed, I want to urge you to start using source control so you can experiment more fearlessly with stuff like this. That way when stuff breaks, you just reset everything back the last known good state. I use git with Unity and can't code without it anymore: it gives you a real feeling of freedom of experimentation. There are tons of tutorials for setting up git.
     
  9. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    I just tried SourceTree but it isn't compatible with my Mac :( but If I update my mac it won't let me play Steam games anymore because of the 64-bit issue. Catch 22! Are there any alternatives?

    I hope you're right. It does seem based on the other thread I bumped yesterday that I'm not alone in this problem... :(
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    They do have a download archive page:

    https://www.sourcetreeapp.com/download-archives

    I'm using 2.5.1 on MacOSX Mojave, but I don't see that one up there. 2.5.3 is up there.

    As far as fiddly physics, it may be helpful to get out paper and diagram all the things you want to interact, and all the things you do NOT want to interact.
     
  11. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    Ok that's helpful, thanks.

    I've not sure which version to download.I'm using High Sierra 10.13.5...is 2.5.3 the right one for that?
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    No idea, give it a try. If not, move back further. I speculate that the versions they have elected to keep posted would be those most useful to older OS installations. You could also just move back to 3.-something, I was only pointing out that 2.5.1 is what I use and it works fine.
     
    anycolourulike and denholmspurr like this.
  13. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    Ok thank you. I'll give it a whirl.

    Also the 2D Physics in project panel worked so thank you! lifesaver.
     
  14. denholmspurr

    denholmspurr

    Joined:
    Apr 17, 2020
    Posts:
    42
    Hmm. Now the code is recognising ALL particles rather than just the one I want it to.

    The documentation seem to say

    particle system.GetCollisionEvents(GameObject, collisionEvents); is what I should use but Visual Studio tells me it's obsolete?

            int eventCount = pSystem.GetCollisionEvents(hurricane, collisionEvents);


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class particleCollision2 : MonoBehaviour
    6. {
    7.     private ParticleSystem pSystem;
    8.     private ParticleCollisionEvent[] collisionEvents;
    9.  
    10.     private void Start()
    11.     {
    12.         pSystem = GetComponent<ParticleSystem>();
    13.         collisionEvents = new ParticleCollisionEvent[8];
    14.     }
    15.  
    16.     public void OnParticleCollision(GameObject hurricane)
    17.     {
    18.         int collCount = pSystem.GetSafeCollisionEventSize();
    19.  
    20.         if (collCount > collisionEvents.Length)
    21.             collisionEvents = new ParticleCollisionEvent[collCount];
    22.  
    23.         int eventCount = pSystem.GetCollisionEvents(hurricane, collisionEvents);
    24.  
    25.         for (int i = 0; i < eventCount; i++);
    26.     }
    27.  
    28. }
    29.  
    UPDATE:

    Worked out how to update the obsolete but the updated version isn't recognising the collision. I swear this system is so frustrating!

    Code (Csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class particleCollision2 : MonoBehaviour
    8. {
    9.     public ParticleSystem pSystem;
    10.     public List<ParticleCollisionEvent> collisionEvents;
    11.     public int score;
    12.     public ParticleSystem pSystem2;
    13.     public List<ParticleCollisionEvent> collisionEvents2;
    14.     public int score2;
    15.  
    16.     private void Start()
    17.     {
    18.         collisionEvents = new List<ParticleCollisionEvent>();
    19.         collisionEvents2 = new List<ParticleCollisionEvent>();
    20.     }
    21.  
    22.     public void OnParticleCollision(GameObject hurricane)
    23.     {
    24.  
    25.         int eventCount = pSystem.GetCollisionEvents(hurricane, collisionEvents);
    26.         int i = 0;
    27.  
    28.         while (i < eventCount)
    29.         {
    30.             score++;
    31.         }
    32.  
    33.         int eventCount2 = pSystem2.GetCollisionEvents(hurricane, collisionEvents);
    34.         int i2 = 0;
    35.  
    36.         while (i2 < eventCount)
    37.         {
    38.             score2++;
    39.         }
    40.  
    41.     }
    42.  
    43. }
     
    Last edited: Apr 24, 2020
  15. virtualosus

    virtualosus

    Joined:
    Oct 5, 2020
    Posts:
    1
    Did you ever come up with a solution for the above?
     
    anycolourulike likes this.
  16. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    683
    Just enable Send Collision Message!