CharacterMoveToPositionAction.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 CharacterMoveToPositionAction : IAction
  12. {
  13. CharacterController character;
  14. Vector3 targetPosition;
  15. float speed = 1f;
  16. float animationSpeed = 1f;
  17. bool done = false;
  18. // Constructor
  19. public CharacterMoveToPositionAction(CharacterController character, Vector3 targetPosition, float speed, float animationSpeed = 1f, float stoppingDistance = 1f)
  20. {
  21. this.character = character;
  22. this.targetPosition = targetPosition;
  23. this.speed = speed;
  24. this.animationSpeed = animationSpeed;
  25. character.navMeshAgent.destination = targetPosition;
  26. character.navMeshAgent.stoppingDistance = stoppingDistance;
  27. character.navMeshAgent.isStopped = false;
  28. character.navMeshAgent.speed = speed;
  29. }
  30. public void Start()
  31. {
  32. //character.navMeshAgent.updatePosition = false;
  33. //character.navMeshAgent.updateRotation = false;
  34. character.navMeshAgent.isStopped = false;
  35. character.navMeshAgent.speed = speed;
  36. }
  37. public void Update()
  38. {
  39. if (character.navMeshAgent == null) return;
  40. if (character.navMeshAgent.pathPending)
  41. {
  42. //Debug.Log("Calculating path");
  43. return;
  44. } else
  45. {
  46. character.navMeshAgent.updatePosition = true;
  47. character.navMeshAgent.isStopped = false;
  48. //Debug.Log($"Following path with {character.navMeshAgent.path.corners.Length} corners ({character.unique_name})");
  49. }
  50. if (character.navMeshAgent.path == null) {
  51. //Debug.Log("No path");
  52. return;
  53. }
  54. if (character.navMeshAgent.path.corners.Length == 0)
  55. {
  56. GenericEvents.Trigger(character.unique_name + "-ArrivedInPositionAction");
  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. GenericEvents.Trigger(character.unique_name + "-ArrivedInPositionAction");
  67. End();
  68. return;
  69. }
  70. TurnToPoint(character.navMeshAgent.steeringTarget);
  71. Vector3 heading = character.navMeshAgent.steeringTarget - character.transform.position;
  72. float distance = heading.magnitude;
  73. Vector3 direction = heading / distance;
  74. Move(speed, direction);
  75. }
  76. private void Move(float speed, Vector3 direction)
  77. {
  78. //Debug.Log(direction);
  79. character.animator.SetFloat("horizontal", 0);
  80. character.animator.SetFloat("vertical", animationSpeed);
  81. //character.transform.Translate(new Vector3(direction.x * speed * Time.deltaTime, 0, direction.z * speed * Time.deltaTime));
  82. //if(character.navMeshAgent.isStopped == false) character.navMeshAgent.nextPosition = character.transform.position;
  83. }
  84. private void TurnToPoint(Vector3 point)
  85. {
  86. Quaternion targetRotation = Quaternion.LookRotation (new Vector3(point.x, character.transform.position.y, point.z) - character.transform.position);
  87. character.animator.transform.rotation = Quaternion.Slerp (character.animator.transform.rotation, targetRotation, 100f * Time.deltaTime);
  88. }
  89. public void End()
  90. {
  91. character.animator.SetFloat("horizontal", 0);
  92. character.animator.SetFloat("vertical", 0);
  93. character.navMeshAgent.isStopped = true;
  94. character.navMeshAgent.updatePosition = false;
  95. character.navMeshAgent.speed = 0;
  96. done = true;
  97. }
  98. public bool IsDone()
  99. {
  100. return done;
  101. }
  102. }
  103. }