Trying to repel/scatter particles in the vicinity of a GameObject, have particles return to position after
Hey there, I'm having some trouble figuring out how particles work with what I have in mind.
Right now I have a particle system that places a particle for each pixel of an image texture. I want a simple sphere (assume attached to the mouse cursor) that repels/scatters the particles, like a magnet, and have the particles move back into place. They would ideally have velocity tied to how fast they were collided with so it's not just static. I'm picturing, basically something like this: (best I could find)
This is the script I'm using for the text to particle... I'm having trouble going from here, though. Any help would be great! I've been playing with attractors and it isn't working how I want... to have the particles come back after being scattered/dispersed.
public Texture2D texture;
public float scale = 1;
public float particleSize = 1;
public ParticleSystem system;
public int numberParticlesX = 64;
public int numberParticlesY = 64;
void Update ()
{
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[numberParticlesX * numberParticlesY];
system.maxParticles = particles.Length;
system.Emit(particles.Length); // Creates the particles.
system.GetParticles(particles); // Get them so we can adjust the values
Vector2 pixelOffset = new Vector2(texture.width / numberParticlesX, texture.height / numberParticlesY);
int index = 0;
Vector3 pos = Vector3.zero;
for(int y = 0; y < numberParticlesY; ++y)
{
for (int x = 0; x < numberParticlesX; ++x)
{
// Sample the pixel value(we could so some interpolation here).
particles[index].position = pos;
particles[index].color = texture.GetPixel(Mathf.RoundToInt(pixelOffset.x * x), Mathf.RoundToInt(pixelOffset.y * y));
particles[index].size = particleSize;
index++;
pos.x += pixelOffset.x * scale;
}
pos.y += pixelOffset.y * scale;
pos.x = 0;
}
system.SetParticles(particles, particles.Length);
}
Anyway thanks so much for taking the time to read this. I appreciate any help or suggestions!
Your answer
![](https://koobas.hobune.stream/wayback/20220612214513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Circle shape emitter is flipping particles on one side 1 Answer
Ignore collision for a single billboard particle? 0 Answers
RayCast on particle system 0 Answers
How to Instantiate decals where particles from particle system hits? 1 Answer
Particles with collisions slip/glitch through corners of edge Collider 0 Answers