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

OnParticleCollision what am i doing wrong?(solved mostly user error)

Discussion in 'Scripting' started by tawdry, Feb 28, 2015.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    So this has been 3 days now and it does not work I have created a simple scene with one cubewith box collider. and a script attached to first player controller that fires a fireball particle prefab at the cube. I have placed world Particle collider on the prefab and attached a script to the cube.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class particlehit : MonoBehaviour {
    5.  
    6.     void OnParticleCollision(GameObject other) {
    7.         Debug.Log ("hello");    Debug.Break ();
    8.     }
    9. }
    This is the script to fire the prefab
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class parttest : MonoBehaviour {
    5.  
    6.     GameObject Lookatme;
    7.     string hitTag;
    8.     int ray;
    9.     int hit;
    10.     Vector3 Headhere;
    11.     public GameObject fire;
    12.     void OnParticleCollision(GameObject other) {
    13.         Debug.Log ("hello");    Debug.Break ();
    14.     }
    15.    void Update(){
    16.  
    17.         if (Input.GetMouseButtonDown (1)) {
    18.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    19.             RaycastHit hit;
    20.             if (Physics.Raycast (ray, out hit, 100)) {
    21.             float distance = Vector3.Distance (hit.transform.position, transform.position);
    22.             if (distance < 50) {
    23.             hitTag = hit.collider.tag;
    24.             if (hitTag != ("null")) {
    25.                         Lookatme = hit.transform.gameObject;
    26.                         Headhere = hit.collider.transform.position;
    27.                         Vector3 direction =Headhere - transform.position;
    28.                         Quaternion rotation = Quaternion.LookRotation (direction);
    29.                         Instantiate (fire, transform.position, rotation);
    30.  
    31.        }   } }  }  }}
    32.  
    33.  
    After it didnt work i just attached world particle collision and the particlecollision script everywhere but still nothing here are some screenshots of the fireball and the cube as well

    FIREBALL
    https://www.flickr.com/photos/129989537@N04/16044604054/

    Cube
    https://www.flickr.com/photos/129989537@N04/16666915465/

    Collision
    https://www.flickr.com/photos/129989537@N04/16665572271/

    I'm so frustrated what am i missing?
     
  2. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Found that if u click on the collision field it open up a drop down list but that doesnt help either clicked the world collision msg there as well and nothing.

    And the inspector pic of the new collision changes which do NOTHING!!!

    https://www.flickr.com/photos/129989537@N04/16480064640/
     
    Last edited: Feb 28, 2015
  3. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    So i can kinda get it to work in a totally broken way basically i stand right at the cube and bombard it, about 20 hites later my fireball will interact with itself a object..name call reveals that the explosion that occurs on impact is setting off the onparticlecollision not the object with collider and script but a child (explosion)of the bloody fireball WTF!
     
  4. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    i swear i see a post about particle collisions every few days. always complaining it doesn't work.

    I have no experience personally, but it sounds like a feature that's a little broken.
     
    tawdry likes this.
  5. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Yea its probably one of my posts over the last 3 days lol. Seems broken to me but then lots seem broken to me and turns out i was the broken one. The onparticlecollision is been called on the script that spawns the particle which hasnt got a collider or ever interacts with anything crazy that it makes the call and only after a consistent bombardment at nose length.
     
  6. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    And now it working I have collision msg ticked set it to medium it does not register at all on low have the same script as mentioned above attached to the cube and it now decides 6 hours later that i was right all along and it should work. I'm just scratching my head.

    To get it to work i copy pasted this script which kept giving errors stating no particle system attached so i put one on the cube and it began working then i deleted all the text except for the print removed the particlesystem off the cube and it started working. You can't make this stuff up Unity is tripping balls a lot of the time. Anyway heres the script i stripped down to nothing in the end

    Code (CSharp):
    1. private ParticleSystem.CollisionEvent[] collisionEvents = new ParticleSystem.CollisionEvent[16];
    2.      void OnParticleCollision(GameObject other)
    3.      {
    4.          int safeLength = particleSystem.safeCollisionEventSize;
    5.          if (collisionEvents.Length < safeLength)
    6.              collisionEvents = new ParticleSystem.CollisionEvent[safeLength];
    7.        
    8.          int numCollisionEvents = particleSystem.GetCollisionEvents(other, collisionEvents);
    9.          int i = 0;
    10.          print (numCollisionEvents);
    11.          while(i < numCollisionEvents)
    12.          {
    13.              print ("YOU HIT THE TRIGGER WITH THE POWER!!!");
    14.              i++;
    15.          }
    16.      }
    17. }
    18.  
     
  7. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    If its not a legacy particle don't bother with adding that world particle collider 3 hours of time wasted with that grrr. .You can have the script on either the particle or the object been hit or both i put mine on the particle as it only gives 1 call then which is how i want it.