Chicken.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. #if EPO_DOTWEEN
  6. using DG.Tweening;
  7. #endif
  8. namespace EPOOutline.Demo
  9. {
  10. public class Chicken : MonoBehaviour
  11. {
  12. [SerializeField]
  13. private bool alwaysActive = false;
  14. [SerializeField]
  15. private bool updateChicken = true;
  16. [SerializeField]
  17. private float searchRadius = 5.0f;
  18. private Outlinable outlinable;
  19. private NavMeshAgent agent;
  20. private Animator animator;
  21. private int enteredCount = 0;
  22. private static int priority = 0;
  23. private void Awake()
  24. {
  25. agent = GetComponent<NavMeshAgent>();
  26. outlinable = GetComponent<Outlinable>();
  27. animator = GetComponent<Animator>();
  28. if (!alwaysActive)
  29. {
  30. #if EPO_DOTWEEN
  31. outlinable.FrontParameters.DOFade(0.0f, 0.0f);
  32. outlinable.BackParameters.FillPass.DOFade("_PublicColor", 0.0f, 0.0f);
  33. #else
  34. outlinable.enabled = false;
  35. #endif
  36. }
  37. agent.avoidancePriority = priority++;
  38. if (updateChicken)
  39. StartCoroutine(UpdateChicken());
  40. }
  41. private void OnTriggerEnter(Collider other)
  42. {
  43. if (alwaysActive)
  44. return;
  45. if (!other.GetComponent<Character>())
  46. return;
  47. #if EPO_DOTWEEN
  48. enteredCount++;
  49. outlinable.FrontParameters.DOKill(true);
  50. outlinable.FrontParameters.DOFade(1.0f, 0.5f);
  51. outlinable.BackParameters.FillPass.DOFade("_PublicColor", 0.5f, 0.5f);
  52. #else
  53. outlinable.enabled = true;
  54. #endif
  55. }
  56. private void OnTriggerExit(Collider other)
  57. {
  58. if (alwaysActive)
  59. return;
  60. if (!other.GetComponent<Character>())
  61. return;
  62. if (--enteredCount != 0)
  63. return;
  64. #if EPO_DOTWEEN
  65. outlinable.FrontParameters.DOKill(true);
  66. outlinable.FrontParameters.DOFade(0.0f, 0.5f);
  67. outlinable.BackParameters.FillPass.DOFade("_PublicColor", 0.0f, 0.5f);
  68. #else
  69. outlinable.enabled = false;
  70. #endif
  71. }
  72. private IEnumerator UpdateChicken()
  73. {
  74. var path = new NavMeshPath();
  75. while (true)
  76. {
  77. animator.CrossFade("Walk In Place", 0.1f);
  78. var point = Random.insideUnitCircle;
  79. var shift = new Vector3(point.x, 0, point.y) * searchRadius;
  80. NavMeshHit hit;
  81. if (!NavMesh.SamplePosition(transform.position + shift, out hit, searchRadius, -1))
  82. {
  83. yield return null;
  84. continue;
  85. }
  86. Debug.DrawLine(transform.position, hit.position, Color.yellow, 3.0f);
  87. if (!NavMesh.CalculatePath(transform.position, hit.position, -1, path))
  88. {
  89. yield return null;
  90. continue;
  91. }
  92. agent.destination = hit.position;
  93. while (agent.pathStatus != NavMeshPathStatus.PathComplete)
  94. yield return null;
  95. var timeToWait = (agent.remainingDistance / agent.speed) * 1.5f;
  96. while (agent.remainingDistance > agent.stoppingDistance && timeToWait > 0.0f)
  97. {
  98. timeToWait -= Time.deltaTime;
  99. yield return null;
  100. }
  101. animator.CrossFade("Eat", 0.1f);
  102. yield return new WaitForSeconds(Random.Range(1.0f, 5.0f));
  103. yield return null;
  104. }
  105. }
  106. private void OnDrawGizmos()
  107. {
  108. Gizmos.color = new Color(1.0f, 0.0f, 0.0f, 0.2f);
  109. Gizmos.DrawSphere(transform.position, searchRadius);
  110. }
  111. }
  112. }