ZombieFollowTargetAction.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using KairoEngine.Core;
  6. namespace KairoEngine.CharacterSystem
  7. {
  8. /// <summary>
  9. /// Action to make a Zombie follow a target transform using one of many animation varients.
  10. /// </summary>
  11. public class ZombieFollowTargetAction : IAction
  12. {
  13. CharacterController character;
  14. Transform target;
  15. float targetDistance;
  16. int varient;
  17. float speed;
  18. float animationSpeed;
  19. bool done = false;
  20. /// <summary>
  21. /// Action that will make a character follow a target transform playing a zombie walk or run animation varient.
  22. /// </summary>
  23. /// <param name="character">The CharacterController performing the action</param>
  24. /// <param name="target">The target Transform to be followed</param>
  25. /// <param name="targetDistance">Stop following when this distance is reached</param>
  26. /// <param name="varient">Type of zombie animation to play. 1 to 7 are walk animations and 8 to 9 are run animations</param>
  27. public ZombieFollowTargetAction(CharacterController character, Transform target, float targetDistance, int varient)
  28. {
  29. if(target == null) Debug.LogError("target Transform cannot be null in ZombieFollowTargetAction");
  30. if(character == null) Debug.LogError("character CharacterController cannot be null in ZombieFollowTargetAction");
  31. this.character = character;
  32. this.target = target;
  33. this.targetDistance = targetDistance;
  34. this.varient = varient;
  35. switch(varient)
  36. {
  37. case 1:
  38. speed = 1.2f;
  39. animationSpeed = 1f;
  40. break;
  41. case 2:
  42. speed = 1.2f;
  43. animationSpeed = 1f;
  44. break;
  45. case 3:
  46. speed = 1.2f;
  47. animationSpeed = 1f;
  48. break;
  49. case 4:
  50. speed = 1.2f;
  51. animationSpeed = 1f;
  52. break;
  53. case 5:
  54. speed = 1.2f;
  55. animationSpeed = 1f;
  56. break;
  57. case 6:
  58. speed = 1.2f;
  59. animationSpeed = 1f;
  60. break;
  61. case 7:
  62. speed = 1.2f;
  63. animationSpeed = 1f;
  64. break;
  65. case 8:
  66. speed = 2f;
  67. animationSpeed = 2f;
  68. break;
  69. case 9:
  70. speed = 2.2f;
  71. animationSpeed = 2f;
  72. break;
  73. default:
  74. speed = 1f;
  75. animationSpeed = 1f;
  76. break;
  77. }
  78. }
  79. public void Start()
  80. {
  81. character.navMeshAgent.destination = target.position;
  82. character.navMeshAgent.updatePosition = true;
  83. character.navMeshAgent.updateRotation = false;
  84. character.navMeshAgent.isStopped = false;
  85. character.navMeshAgent.speed = speed;
  86. character.navMeshAgent.stoppingDistance = targetDistance;
  87. }
  88. public void Update()
  89. {
  90. character.animator.SetFloat("ZombieMoveVarient", varient);
  91. if(Vector3.Distance(character.transform.position, target.position) > targetDistance)
  92. {
  93. character.navMeshAgent.destination = target.position;
  94. character.navMeshAgent.isStopped = false;
  95. character.navMeshAgent.updatePosition = true;
  96. }
  97. if (character.navMeshAgent == null) return;
  98. if (character.navMeshAgent.pathPending)
  99. {
  100. //Debug.Log("Calculating path");
  101. return;
  102. } else
  103. {
  104. //Debug.Log($"Following path with {character.navMeshAgent.path.corners.Length} corners ({character.unique_name})");
  105. }
  106. if (character.navMeshAgent.path == null) {
  107. //Debug.Log("No path");
  108. return;
  109. }
  110. if (character.navMeshAgent.path.corners.Length == 0)
  111. {
  112. return;
  113. }
  114. if (character.navMeshAgent.isStopped) return;
  115. //Debug.Log($"Remaining distance: {character.navMeshAgent.remainingDistance} ({character.unique_name})");
  116. if (character.navMeshAgent.remainingDistance < character.navMeshAgent.stoppingDistance)
  117. {
  118. character.navMeshAgent.isStopped = true;
  119. character.navMeshAgent.path.ClearCorners();
  120. Stop();
  121. return;
  122. }
  123. TurnToPoint(character.navMeshAgent.steeringTarget);
  124. Vector3 heading = character.navMeshAgent.steeringTarget - character.transform.position;
  125. float distance = heading.magnitude;
  126. Vector3 direction = heading / distance;
  127. character.animator.SetFloat("horizontal", 0);
  128. character.animator.SetFloat("vertical", animationSpeed);
  129. }
  130. private void TurnToPoint(Vector3 point)
  131. {
  132. Quaternion targetRotation = Quaternion.LookRotation (point - character.transform.position);
  133. character.animator.transform.rotation = Quaternion.Slerp (character.animator.transform.rotation, targetRotation, 100f * Time.deltaTime);
  134. }
  135. private void Stop()
  136. {
  137. character.animator.SetFloat("horizontal", 0);
  138. character.animator.SetFloat("vertical", 0);
  139. character.navMeshAgent.isStopped = true;
  140. character.navMeshAgent.updatePosition = false;
  141. character.navMeshAgent.speed = 0;
  142. character.navMeshAgent.stoppingDistance = 0;
  143. character.navMeshAgent.destination = character.transform.position;
  144. }
  145. public void End()
  146. {
  147. Stop();
  148. done = true;
  149. }
  150. public bool IsDone()
  151. {
  152. return done;
  153. }
  154. }
  155. }