How to detect which particle collided with a rigid body in Unity?












1















Lets say a rigid body is going through a bunch of particles that belong to a single particle system and you want each particle that collides with rigid body to bounce off. How would you do that ?



void OnParticleCollision(GameObject other) is called when a rigid body has collided with a Particle system. However, I need to know which particle in the particle system has collided with the body.



Any thoughts ?










share|improve this question





























    1















    Lets say a rigid body is going through a bunch of particles that belong to a single particle system and you want each particle that collides with rigid body to bounce off. How would you do that ?



    void OnParticleCollision(GameObject other) is called when a rigid body has collided with a Particle system. However, I need to know which particle in the particle system has collided with the body.



    Any thoughts ?










    share|improve this question



























      1












      1








      1








      Lets say a rigid body is going through a bunch of particles that belong to a single particle system and you want each particle that collides with rigid body to bounce off. How would you do that ?



      void OnParticleCollision(GameObject other) is called when a rigid body has collided with a Particle system. However, I need to know which particle in the particle system has collided with the body.



      Any thoughts ?










      share|improve this question
















      Lets say a rigid body is going through a bunch of particles that belong to a single particle system and you want each particle that collides with rigid body to bounce off. How would you do that ?



      void OnParticleCollision(GameObject other) is called when a rigid body has collided with a Particle system. However, I need to know which particle in the particle system has collided with the body.



      Any thoughts ?







      c# unity3d






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 20:10









      Programmer

      76.3k1086151




      76.3k1086151










      asked Nov 22 '18 at 11:11









      Mert SevinçMert Sevinç

      3201317




      3201317
























          1 Answer
          1






          active

          oldest

          votes


















          2














          With the ParticleSystem.GetParticles function you could catch all your "living" particles, then assign them an index (you should inherit from particle class as the Particle class do not have any index or id variable).



          GertParticles:



          https://docs.unity3d.com/ScriptReference/ParticleSystem.GetParticles.html



          How do you access the individual particles of a particle system?: https://answers.unity.com/questions/639816/how-do-you-access-the-individual-particles-of-a-pa.html



          As I said, particles do not have any ID to identify them, I know that do not seem the "optimal approach" but look this example from Unity documentation about SetCustomParticleData function (https://docs.unity3d.com/ScriptReference/ParticleSystem.SetCustomParticleData.html) they iterate all of them.



          Also on the same page, you can see one example to assign unique ID to each particle when it is born:



          using UnityEngine;
          using UnityEditor;
          using System.Collections.Generic;

          public class ExampleClass : MonoBehaviour {

          private ParticleSystem ps;
          private List<Vector4> customData = new List<Vector4>();
          private int uniqueID;

          void Start() {

          ps = GetComponent<ParticleSystem>();
          }

          void Update() {

          ps.GetCustomParticleData(customData, ParticleSystemCustomData.Custom1);

          for (int i = 0; i < customData.Count; i++)
          {
          // set custom data to the next ID, if it is in the default 0 state
          if (customData[i].x == 0.0f)
          {
          customData[i] = new Vector4(++uniqueID, 0, 0, 0);
          }
          }

          ps.SetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
          }
          }





          share|improve this answer


























          • I am only interested in the particle that crashed. Why would I get all living particles and iterate ? Isn't there a simple way ?

            – Mert Sevinç
            Nov 22 '18 at 11:45






          • 1





            I've updated the question to answer your comment, if there is an optimal way, I don't know how to do it, but either the people from Unity ^^'

            – Lotan
            Nov 22 '18 at 12:02











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429685%2fhow-to-detect-which-particle-collided-with-a-rigid-body-in-unity%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          With the ParticleSystem.GetParticles function you could catch all your "living" particles, then assign them an index (you should inherit from particle class as the Particle class do not have any index or id variable).



          GertParticles:



          https://docs.unity3d.com/ScriptReference/ParticleSystem.GetParticles.html



          How do you access the individual particles of a particle system?: https://answers.unity.com/questions/639816/how-do-you-access-the-individual-particles-of-a-pa.html



          As I said, particles do not have any ID to identify them, I know that do not seem the "optimal approach" but look this example from Unity documentation about SetCustomParticleData function (https://docs.unity3d.com/ScriptReference/ParticleSystem.SetCustomParticleData.html) they iterate all of them.



          Also on the same page, you can see one example to assign unique ID to each particle when it is born:



          using UnityEngine;
          using UnityEditor;
          using System.Collections.Generic;

          public class ExampleClass : MonoBehaviour {

          private ParticleSystem ps;
          private List<Vector4> customData = new List<Vector4>();
          private int uniqueID;

          void Start() {

          ps = GetComponent<ParticleSystem>();
          }

          void Update() {

          ps.GetCustomParticleData(customData, ParticleSystemCustomData.Custom1);

          for (int i = 0; i < customData.Count; i++)
          {
          // set custom data to the next ID, if it is in the default 0 state
          if (customData[i].x == 0.0f)
          {
          customData[i] = new Vector4(++uniqueID, 0, 0, 0);
          }
          }

          ps.SetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
          }
          }





          share|improve this answer


























          • I am only interested in the particle that crashed. Why would I get all living particles and iterate ? Isn't there a simple way ?

            – Mert Sevinç
            Nov 22 '18 at 11:45






          • 1





            I've updated the question to answer your comment, if there is an optimal way, I don't know how to do it, but either the people from Unity ^^'

            – Lotan
            Nov 22 '18 at 12:02
















          2














          With the ParticleSystem.GetParticles function you could catch all your "living" particles, then assign them an index (you should inherit from particle class as the Particle class do not have any index or id variable).



          GertParticles:



          https://docs.unity3d.com/ScriptReference/ParticleSystem.GetParticles.html



          How do you access the individual particles of a particle system?: https://answers.unity.com/questions/639816/how-do-you-access-the-individual-particles-of-a-pa.html



          As I said, particles do not have any ID to identify them, I know that do not seem the "optimal approach" but look this example from Unity documentation about SetCustomParticleData function (https://docs.unity3d.com/ScriptReference/ParticleSystem.SetCustomParticleData.html) they iterate all of them.



          Also on the same page, you can see one example to assign unique ID to each particle when it is born:



          using UnityEngine;
          using UnityEditor;
          using System.Collections.Generic;

          public class ExampleClass : MonoBehaviour {

          private ParticleSystem ps;
          private List<Vector4> customData = new List<Vector4>();
          private int uniqueID;

          void Start() {

          ps = GetComponent<ParticleSystem>();
          }

          void Update() {

          ps.GetCustomParticleData(customData, ParticleSystemCustomData.Custom1);

          for (int i = 0; i < customData.Count; i++)
          {
          // set custom data to the next ID, if it is in the default 0 state
          if (customData[i].x == 0.0f)
          {
          customData[i] = new Vector4(++uniqueID, 0, 0, 0);
          }
          }

          ps.SetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
          }
          }





          share|improve this answer


























          • I am only interested in the particle that crashed. Why would I get all living particles and iterate ? Isn't there a simple way ?

            – Mert Sevinç
            Nov 22 '18 at 11:45






          • 1





            I've updated the question to answer your comment, if there is an optimal way, I don't know how to do it, but either the people from Unity ^^'

            – Lotan
            Nov 22 '18 at 12:02














          2












          2








          2







          With the ParticleSystem.GetParticles function you could catch all your "living" particles, then assign them an index (you should inherit from particle class as the Particle class do not have any index or id variable).



          GertParticles:



          https://docs.unity3d.com/ScriptReference/ParticleSystem.GetParticles.html



          How do you access the individual particles of a particle system?: https://answers.unity.com/questions/639816/how-do-you-access-the-individual-particles-of-a-pa.html



          As I said, particles do not have any ID to identify them, I know that do not seem the "optimal approach" but look this example from Unity documentation about SetCustomParticleData function (https://docs.unity3d.com/ScriptReference/ParticleSystem.SetCustomParticleData.html) they iterate all of them.



          Also on the same page, you can see one example to assign unique ID to each particle when it is born:



          using UnityEngine;
          using UnityEditor;
          using System.Collections.Generic;

          public class ExampleClass : MonoBehaviour {

          private ParticleSystem ps;
          private List<Vector4> customData = new List<Vector4>();
          private int uniqueID;

          void Start() {

          ps = GetComponent<ParticleSystem>();
          }

          void Update() {

          ps.GetCustomParticleData(customData, ParticleSystemCustomData.Custom1);

          for (int i = 0; i < customData.Count; i++)
          {
          // set custom data to the next ID, if it is in the default 0 state
          if (customData[i].x == 0.0f)
          {
          customData[i] = new Vector4(++uniqueID, 0, 0, 0);
          }
          }

          ps.SetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
          }
          }





          share|improve this answer















          With the ParticleSystem.GetParticles function you could catch all your "living" particles, then assign them an index (you should inherit from particle class as the Particle class do not have any index or id variable).



          GertParticles:



          https://docs.unity3d.com/ScriptReference/ParticleSystem.GetParticles.html



          How do you access the individual particles of a particle system?: https://answers.unity.com/questions/639816/how-do-you-access-the-individual-particles-of-a-pa.html



          As I said, particles do not have any ID to identify them, I know that do not seem the "optimal approach" but look this example from Unity documentation about SetCustomParticleData function (https://docs.unity3d.com/ScriptReference/ParticleSystem.SetCustomParticleData.html) they iterate all of them.



          Also on the same page, you can see one example to assign unique ID to each particle when it is born:



          using UnityEngine;
          using UnityEditor;
          using System.Collections.Generic;

          public class ExampleClass : MonoBehaviour {

          private ParticleSystem ps;
          private List<Vector4> customData = new List<Vector4>();
          private int uniqueID;

          void Start() {

          ps = GetComponent<ParticleSystem>();
          }

          void Update() {

          ps.GetCustomParticleData(customData, ParticleSystemCustomData.Custom1);

          for (int i = 0; i < customData.Count; i++)
          {
          // set custom data to the next ID, if it is in the default 0 state
          if (customData[i].x == 0.0f)
          {
          customData[i] = new Vector4(++uniqueID, 0, 0, 0);
          }
          }

          ps.SetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 12:01

























          answered Nov 22 '18 at 11:32









          LotanLotan

          1,150215




          1,150215













          • I am only interested in the particle that crashed. Why would I get all living particles and iterate ? Isn't there a simple way ?

            – Mert Sevinç
            Nov 22 '18 at 11:45






          • 1





            I've updated the question to answer your comment, if there is an optimal way, I don't know how to do it, but either the people from Unity ^^'

            – Lotan
            Nov 22 '18 at 12:02



















          • I am only interested in the particle that crashed. Why would I get all living particles and iterate ? Isn't there a simple way ?

            – Mert Sevinç
            Nov 22 '18 at 11:45






          • 1





            I've updated the question to answer your comment, if there is an optimal way, I don't know how to do it, but either the people from Unity ^^'

            – Lotan
            Nov 22 '18 at 12:02

















          I am only interested in the particle that crashed. Why would I get all living particles and iterate ? Isn't there a simple way ?

          – Mert Sevinç
          Nov 22 '18 at 11:45





          I am only interested in the particle that crashed. Why would I get all living particles and iterate ? Isn't there a simple way ?

          – Mert Sevinç
          Nov 22 '18 at 11:45




          1




          1





          I've updated the question to answer your comment, if there is an optimal way, I don't know how to do it, but either the people from Unity ^^'

          – Lotan
          Nov 22 '18 at 12:02





          I've updated the question to answer your comment, if there is an optimal way, I don't know how to do it, but either the people from Unity ^^'

          – Lotan
          Nov 22 '18 at 12:02


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429685%2fhow-to-detect-which-particle-collided-with-a-rigid-body-in-unity%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen