BubbleSpot.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #pragma warning disable CS0649
  5. #if EPO_DOTWEEN
  6. using DG.Tweening;
  7. #endif
  8. namespace EPOOutline.Demo
  9. {
  10. public class BubbleSpot : MonoBehaviour
  11. {
  12. [SerializeField]
  13. private Transform trackPosition;
  14. [SerializeField]
  15. private Vector3 trackShift;
  16. [SerializeField]
  17. private Camera targetCamera;
  18. [SerializeField]
  19. private Transform bubble;
  20. [SerializeField]
  21. private bool visibleFromBegining = false;
  22. [SerializeField]
  23. private float showDelay = 0.0f;
  24. [SerializeField]
  25. private float showDuration = 5.0f;
  26. [SerializeField]
  27. private bool once;
  28. private bool wasShown = false;
  29. private int playersInside = 0;
  30. private IEnumerator Start()
  31. {
  32. Hide(0.0f);
  33. if (!visibleFromBegining)
  34. yield break;
  35. yield return new WaitForSeconds(showDelay);
  36. Show();
  37. yield return new WaitForSeconds(showDuration);
  38. Hide();
  39. }
  40. private void Reset()
  41. {
  42. targetCamera = FindObjectOfType<Camera>();
  43. }
  44. private void OnTriggerEnter(Collider other)
  45. {
  46. if (!other.GetComponent<Character>())
  47. return;
  48. if (playersInside++ == 0)
  49. Show();
  50. }
  51. private void OnTriggerExit(Collider other)
  52. {
  53. if (!other.GetComponent<Character>())
  54. return;
  55. if (--playersInside == 0)
  56. Hide();
  57. }
  58. private void Show()
  59. {
  60. if (wasShown && once)
  61. return;
  62. wasShown = true;
  63. Show(0.5f);
  64. }
  65. private void Hide()
  66. {
  67. Hide(0.15f);
  68. }
  69. private void Hide(float duration)
  70. {
  71. #if EPO_DOTWEEN
  72. bubble.transform.DOKill(true);
  73. bubble.transform.DOScale(Vector3.zero, duration);
  74. #else
  75. bubble.gameObject.SetActive(false);
  76. #endif
  77. }
  78. private void Show(float duration)
  79. {
  80. #if EPO_DOTWEEN
  81. bubble.transform.DOKill(true);
  82. bubble.transform.DOScale(Vector3.one, duration).SetEase(Ease.OutElastic, 0.00001f);
  83. #else
  84. bubble.gameObject.SetActive(true);
  85. #endif
  86. }
  87. private void Update()
  88. {
  89. if (trackPosition)
  90. transform.position = trackPosition.position + trackShift;
  91. bubble.transform.position = targetCamera.WorldToScreenPoint(transform.position);
  92. }
  93. }
  94. }