CharacterFollowTargetAction.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 CharacterFollowTargetAction : IAction
  12. {
  13. CharacterController character;
  14. Transform target;
  15. float speed = 1f;
  16. float animationSpeed = 1f;
  17. float targetDistance;
  18. bool done = false;
  19. // Constructor
  20. public CharacterFollowTargetAction(CharacterController character, Transform target, float targetDistance, float speed, float animationSpeed = 1f)
  21. {
  22. this.character = character;
  23. this.target = target;
  24. this.targetDistance = targetDistance;
  25. this.speed = speed;
  26. this.animationSpeed = animationSpeed;
  27. }
  28. public void Start()
  29. {
  30. character.navMeshAgent.destination = target.position;
  31. character.navMeshAgent.updatePosition = true;
  32. character.navMeshAgent.updateRotation = false;
  33. character.navMeshAgent.isStopped = false;
  34. character.navMeshAgent.speed = speed;
  35. }
  36. public void Update()
  37. {
  38. if(Vector3.Distance(character.navMeshAgent.destination, target.position) > targetDistance)
  39. {
  40. character.navMeshAgent.destination = target.position;
  41. }
  42. if (character.navMeshAgent == null) return;
  43. if (character.navMeshAgent.pathPending)
  44. {
  45. //Debug.Log("Calculating path");
  46. return;
  47. } else
  48. {
  49. //Debug.Log($"Following path with {character.navMeshAgent.path.corners.Length} corners ({character.unique_name})");
  50. }
  51. if (character.navMeshAgent.path == null) {
  52. //Debug.Log("No path");
  53. return;
  54. }
  55. if (character.navMeshAgent.path.corners.Length == 0)
  56. {
  57. //End();
  58. return;
  59. }
  60. if (character.navMeshAgent.isStopped) return;
  61. //Debug.Log($"Remaining distance: {character.navMeshAgent.remainingDistance} ({character.unique_name})");
  62. if (character.navMeshAgent.remainingDistance < character.navMeshAgent.stoppingDistance)
  63. {
  64. character.navMeshAgent.isStopped = true;
  65. character.navMeshAgent.path.ClearCorners();
  66. //End();
  67. return;
  68. }
  69. TurnToPoint(character.navMeshAgent.steeringTarget);
  70. Vector3 heading = character.navMeshAgent.steeringTarget - character.transform.position;
  71. float distance = heading.magnitude;
  72. Vector3 direction = heading / distance;
  73. character.animator.SetFloat("horizontal", 0);
  74. character.animator.SetFloat("vertical", animationSpeed);
  75. }
  76. private void TurnToPoint(Vector3 point)
  77. {
  78. Quaternion targetRotation = Quaternion.LookRotation (point - character.transform.position);
  79. character.animator.transform.rotation = Quaternion.Slerp (character.animator.transform.rotation, targetRotation, 100f * Time.deltaTime);
  80. }
  81. public void End()
  82. {
  83. character.animator.SetFloat("horizontal", 0);
  84. character.animator.SetFloat("vertical", 0);
  85. character.navMeshAgent.isStopped = true;
  86. character.navMeshAgent.speed = 0;
  87. done = true;
  88. }
  89. public bool IsDone()
  90. {
  91. return done;
  92. }
  93. }
  94. }