Looked all over Google for a solution to this but it seems everyone hits a dead end in some way. So here I am asking again. I need to make the particles created by my Particle System move towards a target in world space. Here’s the script that I attached to the Particle System GameObject.
using UnityEngine;
using System.Collections;
public class ParticleAttraction : MonoBehaviour
{
public ParticleSystem.Particle[] particles;
public Transform target;
void Start()
{
particles = new ParticleSystem.Particle[particleSystem.particleCount];
}
void Update()
{
particleSystem.GetParticles(particles);
for (int i = 0; i < particles.Length; i++)
{
float distance = Vector3.Distance(target.position, particles[i].position);
if (distance > 0.1f)
{
particles[i].position = Vector3.Lerp(particles[i].position, target.position, Time.deltaTime / 2.0f);
}
}
particleSystem.SetParticles(particles, particles.Length);
}
}
The problem is the particles just jump around wildly inside their emitter and die. They don’t actually move towards the target. In fact they’re dying as soon as they’re created.
Is something in the particle system overwriting your changes to them? Maybe the particle system is doing its own simulation after you have modified the particles, overwritting your changes?
@DRRosen3 I moved one line of code and changed the deltatime.time / 2.0f to deltatime.time * speed and it works for me thanks.
particles = new ParticleSystem.Particle[particleSystem.particleCount];
I put that line in update. done!. you need to know count each frame if the particle system is still spawning particles. Start only happens once and i noticed only a couple of the particles seemed to only go to to the target. so moving to update made it work for me.
and i put a public float speed; in the script so i can change how fast it goes to the locations. works great.
edit: oh and i got rid of lerp and used movetowards instead. i think this is also what fixes it. lerp i dont like.
A solution I’m using is to have the particles explode outward, and then back inward. And then in code, you move the entire GameObject the particle system is on towards your target. Hope this helps future ppl.