69 lines
1.9 KiB
C#
69 lines
1.9 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ParticleCollision : MonoBehaviour
|
|
{
|
|
public List<int> subEmmiterIds = new List<int>();
|
|
|
|
private ParticleSystem _particleSystem;
|
|
|
|
private List<ParticleCollisionEvent> _collisionEvents = new List<ParticleCollisionEvent>();
|
|
|
|
private ParticleSystem.Particle[] _particles;
|
|
|
|
private float _lifetime;
|
|
|
|
public float particleDistanceCheck = 0.5f;
|
|
|
|
public Vector3 offset = new Vector3(0f, 0.3f, 0f);
|
|
|
|
public bool debug;
|
|
|
|
private void Start()
|
|
{
|
|
_particleSystem = GetComponent<ParticleSystem>();
|
|
_lifetime = _particleSystem.main.startLifetime.constant;
|
|
_particles = new ParticleSystem.Particle[_particleSystem.main.maxParticles];
|
|
}
|
|
|
|
private void OnParticleCollision(GameObject other)
|
|
{
|
|
_particleSystem.GetCollisionEvents(other, _collisionEvents);
|
|
foreach (ParticleCollisionEvent collisionEvent in _collisionEvents)
|
|
{
|
|
if (!(collisionEvent.intersection != Vector3.zero))
|
|
{
|
|
continue;
|
|
}
|
|
int particles = _particleSystem.GetParticles(_particles);
|
|
for (int i = 0; i < particles; i++)
|
|
{
|
|
if (debug)
|
|
{
|
|
Debug.Log($"{_particles[i].startLifetime} {_lifetime} {Mathf.Abs(_particles[i].startLifetime - _lifetime) < 0.0001f}");
|
|
}
|
|
if (!(Mathf.Abs(_particles[i].startLifetime - _lifetime) < 0.0001f) || !(Vector3.Magnitude(base.transform.TransformPoint(_particles[i].position) - collisionEvent.intersection) < particleDistanceCheck))
|
|
{
|
|
continue;
|
|
}
|
|
if (debug)
|
|
{
|
|
Debug.Log("collision particle " + i + " " + _particles[i].startLifetime);
|
|
}
|
|
_particles[i].startLifetime -= 0.01f;
|
|
_particles[i].position += offset;
|
|
foreach (int subEmmiterId in subEmmiterIds)
|
|
{
|
|
if (debug)
|
|
{
|
|
Debug.Log($"Emitter triggered {subEmmiterId}");
|
|
}
|
|
_particleSystem.TriggerSubEmitter(subEmmiterId, ref _particles[i]);
|
|
}
|
|
break;
|
|
}
|
|
_particleSystem.SetParticles(_particles, particles);
|
|
}
|
|
}
|
|
}
|