• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by martijn-vegas · Nov 13, 2014 at 03:19 PM · meshparticlesystem

Get meshes or GameObjects from particleSystem

Hello,

I am making a particleSystem with dropping cards. I want to click on a card and move them to the camera. I have a few questions:

  • How can I get the meshes? In all the examples, there aren't any examples on how to do this :(

  • If this isn't possible, how can I get the exact position of a particle in the world?

alt text

Here's my code:

 void LateUpdate () {
 
         if(Input.GetMouseButtonDown(0))
     {
             GameObject cam = GameObject.Find("Main Camera");
 
 
             Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
             getSelectedParticle(position);
             
         }
     }
 
     Vector3 getSelectedParticle(Vector3 position)
     {
         Vector3 r = Vector3.zero;
         particleSystem.GetParticles (particles);
         GameObject model = GameObject.Find("Plane");
 
     
         for (int i = 0 ;i < amountParticles ; i++ )
         {
             ParticleSystem.Particle particle = particles[i];
 
             float dist = Vector3.Distance(position,particle.position);
             Vector3 newPos = new Vector3(particle.position.x, -particle.position.y, particle.position.z);
             Debug.DrawRay(position,particle.position);
             Debug.DrawLine(position,newPos,Color.red);
 
             if (dist < 20) { 
                                 // particle position is not equal to model position
                 model.transform.position = particle.position;
                 print (particle.position);
                                 // rotation is quarternion, how to translate this?
                 //var rotation = Quaternion.Euler(0, 30, 0);
 
                 //model.transform.rotation = particle.rotation;
                 break;
             //    break;
             }
 
         }
         return r;
     }
 

This is an example of world to screen translation of the particle positions. No luck yet!

alt text

screen shot 2014-11-13 at 3.44.19 pm.png (106.0 kB)
screen shot 2014-11-13 at 5.10.14 pm.png (127.4 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smoggach · Nov 13, 2014 at 03:49 PM 0
Share

You can't get them. You will have to replace the particle you clicked with a new GameObject.

avatar image martijn-vegas · Nov 13, 2014 at 03:55 PM 0
Share

Okay, I expected that :(

But still, when I get the positions of the particles, they don't look right. Even when I draw a line to a particle, they don't look like they aim the right way. So how can I detect an hit of the particles? I've allready looked at the Raycast, but I only get specific objects back and not the particle position :(

RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out hit)){ print (hit.transform.name); }

Next to that I want to get the parameters from the particle,but Quaternion doesn't exist.

avatar image smoggach · Nov 13, 2014 at 04:00 PM 0
Share

I suppose that depends on the particle system space. Is it local or world? If it's local then you'll probably get a different value from particle.position than if it wasn't. I think the first order of business is figuring out how translate a particle.position to world space.

avatar image martijn-vegas · Nov 13, 2014 at 04:03 PM 0
Share

It's world. Ok, how do you do that. Thanks for replying by the way.

Vector3 screenParticle = Camera.main.WorldToScreenPoint(particle.position);

would be my guess. but still the particle position is not equal to the world position. $$anonymous$$ust I do something with the particleSystem gameObject?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Nov 14, 2014 at 05:38 AM

Here is some example code on how it can be done. This code require a specific setup to work, and therefore will need to be tuned for other setups. It works by cycling through the particles and generating a normal to the surface. Using the position, normal, and Unity's mathematical Plane class, the position of a hit on the infinitie plane is calculated. Then the position of that hit is check against the bounds of the quad to see if the hit is on the particle.

Setup:

For a mesh, it uses a vertical plane of size 1 x 1 anchored at the center create by the CreatePlane editor script from the Unity Wiki.

The simulation space for the ParticleSystem must be 'World'.

The following code deletes the particle clicked on:

 #pragma strict

  // width and height of the mesh used when scaled to (1,1,1)
  public var baseWidth = 1.0;
  public var baseHeight = 1.0;
  
  private var ps : ParticleSystem;
  private var particles : ParticleSystem.Particle[];
  private var count : int;
  
  function Start() {
      ps = particleSystem;
      particles = new ParticleSystem.Particle[ps.maxParticles];
  }
  
  function Update() {
     if (Input.GetMouseButtonDown(0)) {
         var i = HitTest();
         
         if (i != -1) {
             Debug.Log("Hit particle "+i);
             particles[i].lifetime = -1;
             ps.SetParticles(particles, count);
         }
     }
 }
  
  // Returns the index of the particle hit or -1 if none are hit 
  function HitTest() : int {
      var plane : Plane;
      var dist : float = Mathf.Infinity;
      var iHit = -1;
      var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       count = ps.GetParticles(particles);
     
      for (var i = 0; i < count; i++) {
          var p = particles[i];
          var q = Quaternion.AngleAxis(p.rotation, p.axisOfRotation);
          var v = q * Vector3.back;  //<--- Use axis as appropriate to mesh
          plane = new Plane(v, p.position);
          var distance : float;
          if (plane.Raycast(ray, distance)) {
              var point = ray.GetPoint(distance);
              var d = (Camera.main.transform.position - point).magnitude;
              if (d < dist) {
                  var vv = point - p.position;
                 // Normalize to local coordinates
                  vv = Quaternion.Inverse(q) * vv;
 
                  var width = baseWidth * p.size / 2.0;
                  var height = baseHeight * p.size / 2.0;
                  
                  if (Mathf.Abs(vv.x) < width && Mathf.Abs(vv.y) < height) {
                      iHit = i;
                      dist = d;
                  }
              }
          } 
      }
      return iHit;
  }

Note you ask about getting the rotation of each particle. The is an (undocumented?) variable for particles called 'axisOfRotation'. This can be combined with the 'rotation' of a particle in AngleAxis() to produce a Quaternion rotation of that particle.

I never know without testing whether making function calls to UnityEngine routines is faster or slower than C# code. But if you want to do the raycast by hand, you will find line/plane intersection code in the Math3D class in the Unity Wiki.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image martijn-vegas · Nov 17, 2014 at 09:30 AM 0
Share

Yes, works prerfect! Thanks allot!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I get vertex positions of the referenced game object, and not its mesh? 2 Answers

How to change the mesh of a particle system at runtime C# 2 Answers

Is there a way to add mesh, being modified during play mode, to particle component to emit particles from it? 2 Answers

Create entity by particles 0 Answers

ParticleSystem mesh particles scaling differently in (1,1,1) scale between Simulation Space = World or Local 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges