- Home /
Filling a Container with Particles?
I'm looking to fill particles into a container (water into a glass, to be specific). Is this possible, and if so, any directions to get me started? Thanks so much!
I'm also looking for the same functionality. I wonder if you solved this and how.
Answer by thef1chesser · Oct 22, 2013 at 08:43 AM
GameObject Particle;
ParticleContainer particleContainer;
public void Create(int numOfParticles) { GameObject particle;
Vector3 tmp = new Vector3();
particle = Instantiate(Particle) as GameObject;
particle.transform.position = tmp;
particle.transform.parent = particleContainer.transform;
}
Answer by Numid · Mar 08, 2015 at 10:20 AM
I would suggest checking the position of every particle at every LateUpdate
call and reseting the particles of the particle system accordingly.
void LateUpdate()
{
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particleSystem.particleCount];
particleSystem.GetParticles(particles);
List<ParticleSystem.Particle> particlesToKeep = new List<ParticleSystem.Particle>();
foreach (ParticleSystem.Particle particle in particles)
{
if (IsInGlass(particle.position))
{
particlesToKeep.Add(particle);
}
}
particleSystem.SetParticles(particlesToKeep.toArray(), particlesToKeep.Count);
}
In the above example, IsInGlassVolume
is a method of your own that checks if the given position is within the glass volume. It can also consider the size of the particle.
Answer by Cherno · Mar 08, 2015 at 12:48 PM
If you set up collision for the particles (as in, the collision option in Shuriken), and make sure that the particle generation space does not exceed the container's inner size, then you should be all set. Of course, water physics is a whole chapter altogether if it should behave realistically.