Advertisement
zorochii

ParticleCollisionSpawn.cs

Jul 22nd, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. /*
  2. * Código usado en el vídeo de "Lluvia de Meteoros o algo así" de Orochii Zouveleki
  3. * Link: https://www.youtube.com/watch?v=UYYyNsvfAL4&lc=z23hf1vqikncxpk5w04t1aokgry3cy2xp411zekhkebubk0h00410
  4. * Úsese bajo licencia MIT.
  5. *
  6. * Tiempo después del vídeo en cuestión le hice un par de añadidos para hacer el efecto más guay. Enjoy!
  7. */
  8.  
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12.  
  13. [ExecuteInEditMode]
  14. public class ParticleCollisionSpawn : MonoBehaviour {
  15.     [SerializeField] ParticleSystem explosionParticlesPrefab;
  16.     [SerializeField] ParticleSystem ps;
  17.     [SerializeField] AudioClip particleSpawnClip;
  18.     [SerializeField] AudioSource particleSpawnSource;
  19.     [SerializeField] float destroyDelay = 1.2f;
  20.     [SerializeField] float pitchVariance = 0.1f;
  21.  
  22.     int _lastParticleCount;
  23.  
  24.     private void Update() {
  25.         if (ps.particleCount > _lastParticleCount) {
  26.             PlayParticleSpawned();
  27.             _lastParticleCount = ps.particleCount;
  28.         }
  29.     }
  30.  
  31.     private void PlayParticleSpawned() {
  32.         if (particleSpawnSource != null) particleSpawnSource.PlayOneShot(particleSpawnClip);
  33.     }
  34.  
  35.     private void OnParticleTrigger() {
  36.         // Obtiene la referencia al sistema de partículas (si no se ha definido).
  37.         if (ps == null) ps = GetComponent<ParticleSystem>();
  38.  
  39.         // Retornar si se está en modo edición.
  40.         if (!Application.isPlaying) return;
  41.  
  42.         // Obtiene una lista de partículas en estado de entrar en colisión.
  43.         List<ParticleSystem.Particle> enterList = new List<ParticleSystem.Particle>();
  44.         int particlesAmount = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enterList);
  45.        
  46.         // Iterar por cada partícula que entró en colisión en este frame.
  47.         for (int i = 0; i < particlesAmount; i++) {
  48.             // Instanciar un sistema de partículas en cada colisión.
  49.             ParticleSystem spawnedPS = Instantiate<ParticleSystem>(explosionParticlesPrefab, enterList[i].position, Quaternion.identity);
  50.             AudioSource audio = spawnedPS.GetComponent<AudioSource>();
  51.             if (audio != null) audio.pitch += (UnityEngine.Random.Range(-pitchVariance, pitchVariance));
  52.             Destroy(spawnedPS.gameObject, destroyDelay);
  53.             // Reduce en 1 la cuenta actual de partículas, porque se creará una partícula nueva al haber colisión.
  54.             _lastParticleCount -= 1;
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement