ParticleSystemAttractor.cs 810 B

123456789101112131415161718192021
  1. using System;
  2. using UnityEngine;
  3. public class ParticleSystemAttractor : MonoBehaviour
  4. {
  5. public void Attract(ParticleSystem ps, Transform target, Vector3 offset, float speed)
  6. {
  7. Vector3 vector = ps.transform.InverseTransformPoint(target.position + offset);
  8. ParticleSystem.Particle[] array = new ParticleSystem.Particle[100];
  9. int particles = ps.GetParticles(array);
  10. for (int i = 0; i < particles; i++)
  11. {
  12. float num = Vector2.Distance(array[i].position, vector);
  13. array[i].velocity = (vector - array[i].position) * speed * UnityEngine.Random.Range(1.5f, 1.8f);
  14. array[i].velocity = new Vector2(array[i].velocity.x, array[i].velocity.y);
  15. array[i].remainingLifetime = num / array[i].velocity.magnitude;
  16. array[i].size = 70f;
  17. }
  18. ps.SetParticles(array, particles);
  19. }
  20. }