- Home /
How do you set Particles to known positions?
I would like to be able to draw point clouds using particles. To do this I need to be able to set an array of particles to a known position, zero velocity, emit them and have them last forever. However, I cannot the setting of the position to work.
My set up is as follows.
Empty GameObject with a ParticleSystem attached
Duration = 1
Looping = true
Start Lifetime = 10
Start Speed = 0
Start Size = 0.1
Emission is turned off
Shape is turned off
Renderer is turned on.
The following script is attached to the GameObject
using UnityEngine;
using System.Collections;
public class PointCloud : MonoBehaviour {
ParticleSystem.Particle[] cloud;
void Start ()
{
float v = 1.0f;
cloud = new ParticleSystem.Particle[4];
cloud[0].position = new Vector3(v,v,v);
cloud[1].position = new Vector3(-v,v,-v);
cloud[2].position = new Vector3(-v,v,v);
cloud[3].position = new Vector3(v,v,-v);
for(int ii = 0; ii < cloud.Length; ++ii)
{
ParticleSystem.Particle p = cloud[ii];
p.color = Color.red;
p.velocity = Vector3.zero;
p.angularVelocity = 0.0f;
p.rotation = 0.0f;
p.size = 0.1f;
p.lifetime = 1.0f;
p.randomValue = 0.0f;
}
particleSystem.SetParticles(cloud, cloud.Length);
particleSystem.Emit(cloud.Length);
}
}
All that happens though is I get an single (or more, I can't tell) particle at the system's origin.
The particle has the size and color set in the inspector and not that in the script.
If I add a Shape then I do get 4 points, but at random locations and not the locations specified.
Also, if I get the particles after setting them, they do have the positions and other characteristics I specified.
I am using Unity 3.5.2f2
These posts indicate that it is possible:
Answer by Robert-Castle · Jun 03, 2012 at 11:35 AM
Finally got it working. Here is a minimal example with no error checking:
using UnityEngine;
using System.Collections;
public class PointCloud : MonoBehaviour
{
ParticleSystem.Particle[] cloud;
bool bPointsUpdated = false;
void Start ()
{
}
void Update ()
{
if (bPointsUpdated)
{
particleSystem.SetParticles(cloud, cloud.Length);
bPointsUpdated = false;
}
}
public void SetPoints(Vector3[] positions, Color[] colors)
{
cloud = new ParticleSystem.Particle[positions.Length];
for (int ii = 0; ii < positions.Length; ++ii)
{
cloud[ii].position = positions[ii];
cloud[ii].color = colors[ii];
cloud[ii].size = 0.1f;
}
bPointsUpdated = true;
}
}
Attach this script to a GameObject with a Shuriken particle system, and set the particle system as follows to make it inert:
Looping false
Play On Awake false
Max Particles*to the max number of particles you want*
Turn off everything except Renderer
In the Renderer:
Sort Mode By Distance
Cast Shadows false
Receive Shadows false
This tutorial was very enlightening and awesome: Graphs - visualizing data
I followed your example exactly and I couldn't see anything! I checked through the checklist twice and I copied the code to an object with Shuriken particle system. -okay this page is awesome indeed http://catlikecoding.com/unity/tutorials/graphs/
Answer by Berenger · Jun 01, 2012 at 04:37 PM
I've never tried to do that with the shuriken system, but from the lightning bolt example you should
Emit 4 particles.
Store them into an array with GetParticles.
Apply the modifications.
Use SetParticles to assign them back.
Do you $$anonymous$$d sharing the link of the example you are talking about?
Answer by Artifact-Jesse · Nov 21, 2018 at 08:05 AM
For those stumbling across this in Unity 2018.x, check out my answer here with a functional example. http://answers.unity.com/answers/1573665/view.html Thank you to all the answers here, you helped me find a solution!
Answer by crogers · Jul 18, 2012 at 09:08 PM
I am trying the same thing. Although I can call SetParticles() without error, I do not see my particles and GetParticles() returns nothing, even on a "stock" ParticleSystem that is visibly emitting particles.
Do GetParticles() and SetParticles() work (using Javascript and UnityPro 3.5.3)?
function emitTex () {
var mult : float = 0.5; var x : int; var y : int; var c : Color; var p : ParticleSystem.Particle[]; p = new ParticleSystem.Particle[4096]; var i : int = 0; for (x = 0 ; x < map.width ; x = x + 1 ) { for (y = 0 ; y < map.height ; y = y + 1 ) { c = map.GetPixel(x,y); p[i].position = Vector3(x mult, y mult, 0 ); p[i].color = c; p[i].size = 4.0; Debug.Log(i+" "+p[i].size.ToString()+" "+p[i].position.ToString()+" c="+c.ToString()); i = i + 1; } } ps.Play(); particleSystem.SetParticles(p,p.Length); Debug.Log("made "+i+" particles\n"); //ps.Play(); var pp : ParticleSystem.Particle[]; var n : int; n = particleSystem.GetParticles(pp); Debug.Log("got "+n+" particles\n"); flag = 0; }
(by the way, my code showed up correctly in the preview but gets mangled in the post.....)
Your array pp needs to be assigned first, and be large enough to hold the required number of particles. If pp = 10 and particleSystem = 100, you will get back 10. If pp = 10000 and particleSystem = 100, you will get back 100. Use particleSystem.particleCount to find out how many there currently are.
var pp : ParticleSystem.Particle[];
var n : int;
pp = new ParticleSystem.Particle[particleSystem.particleCount];
n = particleSystem.GetParticles(pp);