Skip to content

Instantly share code, notes, and snippets.

@NVentimiglia
Created July 23, 2016 23:48
Show Gist options
  • Save NVentimiglia/f5e3ff1ac31950f27079789399ac2812 to your computer and use it in GitHub Desktop.
Save NVentimiglia/f5e3ff1ac31950f27079789399ac2812 to your computer and use it in GitHub Desktop.
ParticleMagnet / Particle Attractor
using UnityEngine;
/// <summary>
/// Particle Attractor
/// </summary>
[AddComponentMenu("NVenti/ParticleMagnet")]
public class ParticleMagnet : MonoBehaviour
{
public ParticleSystem System;
public Transform MagnetPoint;
public float Delay = 1;
public bool AutoPlay = true;
public bool Looping = true;
protected float delta;
[ContextMenu("Play")]
public void Play()
{
delta = 0;
System.Play();
}
void Start()
{
if (AutoPlay)
{
Play();
}
}
void Update()
{
delta += Time.deltaTime;
//pause
if (delta < Delay)
{
return;
}
ParticleSystem.Particle[] ps = new ParticleSystem.Particle[System.particleCount];
System.GetParticles(ps);
for (int i = 0;i < ps.Length;i++)
{
ps[i].velocity = Vector3.Lerp(ps[i].velocity, (MagnetPoint.position - ps[i].position).normalized, 0.1f);
}
System.SetParticles(ps, ps.Length);
if (Looping)
{
//reset
if (delta > System.duration)
{
Play();
}
}
else
{
enabled = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment