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:
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 Particlesto 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’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.
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!
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_.position = Vector3(x * mult, y * mult, 0 );_
p*.color = c;*
p*.size = 4.0;*
Debug.Log(i+" “+p_.size.ToString()+” “+p*.position.ToString()+” c="+c.ToString());
i = i + 1;
}
}
ps.Play();
particleSystem.SetParticles(p,p.Length);
Debug.Log("made “+i+” particles
");
//ps.Play();
var pp : ParticleSystem.Particle[];
var n : int;
n = particleSystem.GetParticles(pp);
Debug.Log("got “+n+” particles
");
flag = 0;
}*_