- Home /
Setting the position of particles in a (shuriken) ParticleSystem
Hi!
I'm trying to snap the particles of a particleSystem to a grid. Here is my code:
public ParticleSystem particles;//I drag the particle system in question in to here.
ParticleSystem.Particle[] gos = new ParticleSystem.Particle[300];
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
int index = 0;
int total = 0;
Vector3 pos = Vector3.zero;
total = particles.GetParticles(gos);
while(index < total)
{
pos = gos[index].position;
pos.x = Mathf.RoundToInt(pos.x);
pos.y = Mathf.RoundToInt(pos.y);
gos[index].position = pos;
index++;
}
}
Good for you! pat
(there's not an actual question here, haha!)
Answer by MichaelTodd · Jan 17, 2013 at 06:42 AM
Yes! I am an idiot!
Thanks, that works!
public ParticleSystem particles;
ParticleSystem.Particle[] gos = new ParticleSystem.Particle[300];
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
int index = 0;
int total = 0;
total = particles.GetParticles(gos);
while(index < total)
{
xa.glx = gos[index].position;
xa.glx.x = Mathf.RoundToInt(xa.glx.x);
xa.glx.y = Mathf.RoundToInt(xa.glx.y);
gos[index].position = xa.glx;
index++;
}
particles.SetParticles(gos, total);
Answer by Loius · Jan 17, 2013 at 03:36 AM
It looks like you haven't set the particles back. GetParticles returns a copy of the particle array, not the actual array.
As seen on the doc page, (http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=getparticles), you need to use SetParticles after you're done changing the particle information.
Answer by Ego65 · Oct 04, 2016 at 09:35 AM
although this is now 3 years old i say thanks !!!
thats exactly that what i needed ! to get positions of all particles.
I use the particle system itself to create a asteroid belt like a disk with a hole in the middle with the shape : mesh : Triangles method.
The disc is done in blender so its possible to deactivate the particle system after instantiating my asteroid prefab. The astprefab has its own script to have better control over collisions and destroying it by a healthscript. Because it seems the collision module in the particle system doesnt work accurately.
I changed your scripts to my needs like this :
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(ParticleSystem))]
public class ParticlePosition : MonoBehaviour
{
public GameObject thisObj;
public GameObject thisAstprefab;
public ParticleSystem particles;
ParticleSystem.Particle[] gos = new ParticleSystem.Particle[1000];
void Awake()
{
int index = 0;
int total = 0;
total = particles.GetParticles(gos);
Vector3 pos = Vector3.zero;
while(index < total)
{
pos = gos[index].position;
pos.x = Mathf.RoundToInt(pos.x);
pos.y = Mathf.RoundToInt(pos.y);
gos[index].position = pos;
Instantiate(thisAstprefab,gos[index].position,Quaternion.identity);
index++;
}
if(index >= total)
{
thisObj.SetActive(false);
}
//particles.SetParticles(gos, total);
}
}