You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB

  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.Effects
  4. {
  5. public class WaterHoseParticles : MonoBehaviour
  6. {
  7. public static float lastSoundTime;
  8. public float force = 1;
  9. private ParticleCollisionEvent[] m_CollisionEvents = new ParticleCollisionEvent[16];
  10. private ParticleSystem m_ParticleSystem;
  11. private void Start()
  12. {
  13. m_ParticleSystem = GetComponent<ParticleSystem>();
  14. }
  15. private void OnParticleCollision(GameObject other)
  16. {
  17. int safeLength = m_ParticleSystem.GetSafeCollisionEventSize();
  18. if (m_CollisionEvents.Length < safeLength)
  19. {
  20. m_CollisionEvents = new ParticleCollisionEvent[safeLength];
  21. }
  22. int numCollisionEvents = m_ParticleSystem.GetCollisionEvents(other, m_CollisionEvents);
  23. int i = 0;
  24. while (i < numCollisionEvents)
  25. {
  26. if (Time.time > lastSoundTime + 0.2f)
  27. {
  28. lastSoundTime = Time.time;
  29. }
  30. var col = m_CollisionEvents[i].collider;
  31. if (col.attachedRigidbody != null)
  32. {
  33. Vector3 vel = m_CollisionEvents[i].velocity;
  34. col.attachedRigidbody.AddForce(vel*force, ForceMode.Impulse);
  35. }
  36. other.BroadcastMessage("Extinguish", SendMessageOptions.DontRequireReceiver);
  37. i++;
  38. }
  39. }
  40. }
  41. }