Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Código usado en el vídeo de "Lluvia de Meteoros o algo así" de Orochii Zouveleki
- * Link: https://www.youtube.com/watch?v=UYYyNsvfAL4&lc=z23hf1vqikncxpk5w04t1aokgry3cy2xp411zekhkebubk0h00410
- * Úsese bajo licencia MIT.
- *
- * Tiempo después del vídeo en cuestión le hice un par de añadidos para hacer el efecto más guay. Enjoy!
- */
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [ExecuteInEditMode]
- public class ParticleCollisionSpawn : MonoBehaviour {
- [SerializeField] ParticleSystem explosionParticlesPrefab;
- [SerializeField] ParticleSystem ps;
- [SerializeField] AudioClip particleSpawnClip;
- [SerializeField] AudioSource particleSpawnSource;
- [SerializeField] float destroyDelay = 1.2f;
- [SerializeField] float pitchVariance = 0.1f;
- int _lastParticleCount;
- private void Update() {
- if (ps.particleCount > _lastParticleCount) {
- PlayParticleSpawned();
- _lastParticleCount = ps.particleCount;
- }
- }
- private void PlayParticleSpawned() {
- if (particleSpawnSource != null) particleSpawnSource.PlayOneShot(particleSpawnClip);
- }
- private void OnParticleTrigger() {
- // Obtiene la referencia al sistema de partículas (si no se ha definido).
- if (ps == null) ps = GetComponent<ParticleSystem>();
- // Retornar si se está en modo edición.
- if (!Application.isPlaying) return;
- // Obtiene una lista de partículas en estado de entrar en colisión.
- List<ParticleSystem.Particle> enterList = new List<ParticleSystem.Particle>();
- int particlesAmount = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enterList);
- // Iterar por cada partícula que entró en colisión en este frame.
- for (int i = 0; i < particlesAmount; i++) {
- // Instanciar un sistema de partículas en cada colisión.
- ParticleSystem spawnedPS = Instantiate<ParticleSystem>(explosionParticlesPrefab, enterList[i].position, Quaternion.identity);
- AudioSource audio = spawnedPS.GetComponent<AudioSource>();
- if (audio != null) audio.pitch += (UnityEngine.Random.Range(-pitchVariance, pitchVariance));
- Destroy(spawnedPS.gameObject, destroyDelay);
- // Reduce en 1 la cuenta actual de partículas, porque se creará una partícula nueva al haber colisión.
- _lastParticleCount -= 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement