Hyluss

[Unity] Particle System - magnetic field

Nov 29th, 2017
334
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using UnityEngine;
  2.  using System.Collections;
  3.  
  4.  public class ParticleAttractor : MonoBehaviour {
  5.      
  6.      [SerializeField]
  7.      private Transform _attractorTransform;
  8.  
  9.      private ParticleSystem _particleSystem;
  10.      private ParticleSystem.Particle[] _particles = new ParticleSystem.Particle[1000];
  11.  
  12.      public override void Start ()
  13.      {
  14.          _particleSystem = GetComponent<ParticleSystem> ();
  15.      }
  16.  
  17.      public void LateUpdate()
  18.      {
  19.          if (particleSystem.isPlaying) {
  20.              int length = _particleSystem.GetParticles (_particles);
  21.              Vector3 attractorPosition = _attractorTransform.position;
  22.  
  23.              for (int i=0; i < length; i++) {
  24.                      _particles [i].position = _particles [i].position + (attractorPosition - _particles [i].position) / (_particles [i].lifetime) * Time.deltaTime;
  25.              }
  26.              _particleSystem.SetParticles (_particles, length);
  27.          }
  28.  
  29.      }
  30.  }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment