ZombieMoveToPositionAction.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 move a character in a certain direction and speed.
  10. /// </summary>
  11. public class ZombieMoveToPositionAction : IAction
  12. {
  13. CharacterController character;
  14. Vector3 targetPosition;
  15. int varient;
  16. float speed = 1f;
  17. float animationSpeed = 1f;
  18. bool done = false;
  19. // Constructor
  20. public ZombieMoveToPositionAction(CharacterController character, Vector3 targetPosition, int varient)
  21. {
  22. this.character = character;
  23. this.targetPosition = targetPosition;
  24. this.varient = varient;
  25. switch(varient)
  26. {
  27. case 1:
  28. speed = 1.2f;
  29. animationSpeed = 1f;
  30. break;
  31. case 2:
  32. speed = 1.2f;
  33. animationSpeed = 1f;
  34. break;
  35. case 3:
  36. speed = 1.2f;
  37. animationSpeed = 1f;
  38. break;
  39. case 4:
  40. speed = 1.2f;
  41. animationSpeed = 1f;
  42. break;
  43. case 5:
  44. speed = 1.2f;
  45. animationSpeed = 1f;
  46. break;
  47. case 6:
  48. speed = 1.2f;
  49. animationSpeed = 1f;
  50. break;
  51. case 7:
  52. speed = 1.2f;
  53. animationSpeed = 1f;
  54. break;
  55. case 8:
  56. speed = 2f;
  57. animationSpeed = 2f;
  58. break;
  59. case 9:
  60. speed = 2.2f;
  61. animationSpeed = 2f;
  62. break;
  63. default:
  64. speed = 1f;
  65. animationSpeed = 1f;
  66. break;
  67. }
  68. }
  69. public void Start()
  70. {
  71. character.navMeshAgent.destination =targetPosition;
  72. character.navMeshAgent.updatePosition = false;
  73. character.navMeshAgent.updateRotation = false;
  74. character.navMeshAgent.isStopped = true;
  75. character.navMeshAgent.speed = speed;
  76. character.navMeshAgent.stoppingDistance = 0.5f;
  77. }
  78. public void Update()
  79. {
  80. character.animator.SetFloat("ZombieMoveVarient", varient);
  81. if (character.navMeshAgent == null) return;
  82. if (character.navMeshAgent.pathPending)
  83. {
  84. //Debug.Log("Calculating path");
  85. return;
  86. } else
  87. {
  88. character.navMeshAgent.updatePosition = true;
  89. character.navMeshAgent.isStopped = false;
  90. //Debug.Log($"Following path with {character.navMeshAgent.path.corners.Length} corners ({character.unique_name})");
  91. }
  92. if (character.navMeshAgent.path == null) {
  93. //Debug.Log("No path");
  94. return;
  95. }
  96. if (character.navMeshAgent.path.corners.Length == 0)
  97. {
  98. GenericEvents.Trigger(character.unique_name + "-ArrivedInPositionAction");
  99. End();
  100. return;
  101. }
  102. if (character.navMeshAgent.isStopped) return;
  103. //Debug.Log($"Remaining distance: {character.navMeshAgent.remainingDistance} ({character.unique_name})");
  104. if (character.navMeshAgent.remainingDistance < character.navMeshAgent.stoppingDistance)
  105. {
  106. character.navMeshAgent.isStopped = true;
  107. character.navMeshAgent.path.ClearCorners();
  108. GenericEvents.Trigger(character.unique_name + "-ArrivedInPositionAction");
  109. End();
  110. return;
  111. }
  112. TurnToPoint(character.navMeshAgent.steeringTarget);
  113. Vector3 heading = character.navMeshAgent.steeringTarget - character.transform.position;
  114. float distance = heading.magnitude;
  115. Vector3 direction = heading / distance;
  116. Move(speed, direction);
  117. }
  118. private void Move(float speed, Vector3 direction)
  119. {
  120. //Debug.Log(direction);
  121. character.animator.SetFloat("horizontal", 0);
  122. character.animator.SetFloat("vertical", animationSpeed);
  123. //character.transform.Translate(new Vector3(direction.x * speed * Time.deltaTime, 0, direction.z * speed * Time.deltaTime));
  124. //if(character.navMeshAgent.isStopped == false) character.navMeshAgent.nextPosition = character.transform.position;
  125. }
  126. private void TurnToPoint(Vector3 point)
  127. {
  128. Quaternion targetRotation = Quaternion.LookRotation (point - character.transform.position);
  129. character.animator.transform.rotation = Quaternion.Slerp (character.animator.transform.rotation, targetRotation, 100f * Time.deltaTime);
  130. }
  131. public void End()
  132. {
  133. character.animator.SetFloat("horizontal", 0);
  134. character.animator.SetFloat("vertical", 0);
  135. character.navMeshAgent.isStopped = true;
  136. character.navMeshAgent.updatePosition = false;
  137. character.navMeshAgent.speed = 0;
  138. done = true;
  139. }
  140. public bool IsDone()
  141. {
  142. return done;
  143. }
  144. }
  145. }