TraumaInducer.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using UnityEngine;
  2. using System.Collections;
  3. /* Example script to apply trauma to the camera or any game object */
  4. public class TraumaInducer : MonoBehaviour
  5. {
  6. [Tooltip("Seconds to wait before trigerring the explosion particles and the trauma effect")]
  7. public float Delay = 1;
  8. [Tooltip("Maximum stress the effect can inflict upon objects Range([0,1])")]
  9. public float MaximumStress = 0.6f;
  10. [Tooltip("Maximum distance in which objects are affected by this TraumaInducer")]
  11. public float Range = 45;
  12. private IEnumerator Start()
  13. {
  14. /* Wait for the specified delay */
  15. yield return new WaitForSeconds(Delay);
  16. /* Play all the particle system this object has */
  17. // PlayParticles();
  18. /* Find all gameobjects in the scene and loop through them until we find all the nearvy stress receivers */
  19. var targets = UnityEngine.Object.FindObjectsOfType<GameObject>();
  20. for(int i = 0; i < targets.Length; ++i)
  21. {
  22. var receiver = targets[i].GetComponent<StressReceiver>();
  23. if(receiver == null) continue;
  24. float distance = Vector3.Distance(transform.position, targets[i].transform.position);
  25. /* Apply stress to the object, adjusted for the distance */
  26. if(distance > Range) continue;
  27. float distance01 = Mathf.Clamp01(distance / Range);
  28. float stress = (1 - Mathf.Pow(distance01, 2)) * MaximumStress;
  29. receiver.InduceStress(stress);
  30. }
  31. }
  32. /* Search for all the particle system in the game objects children */
  33. private void PlayParticles()
  34. {
  35. var children = transform.GetComponentsInChildren<ParticleSystem>();
  36. for(var i = 0; i < children.Length; ++i)
  37. {
  38. children[i].Play();
  39. }
  40. var current = GetComponent<ParticleSystem>();
  41. if(current != null) current.Play();
  42. }
  43. }