using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using KairoEngine.Core; namespace KairoEngine.CharacterSystem { /// /// Action to move a character in a certain direction and speed. /// public class CharacterMoveToPositionAction : IAction { CharacterController character; Vector3 targetPosition; float speed = 1f; float animationSpeed = 1f; bool done = false; // Constructor public CharacterMoveToPositionAction(CharacterController character, Vector3 targetPosition, float speed, float animationSpeed = 1f, float stoppingDistance = 1f) { this.character = character; this.targetPosition = targetPosition; this.speed = speed; this.animationSpeed = animationSpeed; character.navMeshAgent.destination = targetPosition; character.navMeshAgent.stoppingDistance = stoppingDistance; character.navMeshAgent.isStopped = false; character.navMeshAgent.speed = speed; } public void Start() { //character.navMeshAgent.updatePosition = false; //character.navMeshAgent.updateRotation = false; character.navMeshAgent.isStopped = false; character.navMeshAgent.speed = speed; } public void Update() { if (character.navMeshAgent == null) return; if (character.navMeshAgent.pathPending) { //Debug.Log("Calculating path"); return; } else { character.navMeshAgent.updatePosition = true; character.navMeshAgent.isStopped = false; //Debug.Log($"Following path with {character.navMeshAgent.path.corners.Length} corners ({character.unique_name})"); } if (character.navMeshAgent.path == null) { //Debug.Log("No path"); return; } if (character.navMeshAgent.path.corners.Length == 0) { GenericEvents.Trigger(character.unique_name + "-ArrivedInPositionAction"); End(); return; } if (character.navMeshAgent.isStopped) return; //Debug.Log($"Remaining distance: {character.navMeshAgent.remainingDistance} ({character.unique_name})"); if (character.navMeshAgent.remainingDistance < character.navMeshAgent.stoppingDistance) { character.navMeshAgent.isStopped = true; character.navMeshAgent.path.ClearCorners(); GenericEvents.Trigger(character.unique_name + "-ArrivedInPositionAction"); End(); return; } TurnToPoint(character.navMeshAgent.steeringTarget); Vector3 heading = character.navMeshAgent.steeringTarget - character.transform.position; float distance = heading.magnitude; Vector3 direction = heading / distance; Move(speed, direction); } private void Move(float speed, Vector3 direction) { //Debug.Log(direction); character.animator.SetFloat("horizontal", 0); character.animator.SetFloat("vertical", animationSpeed); //character.transform.Translate(new Vector3(direction.x * speed * Time.deltaTime, 0, direction.z * speed * Time.deltaTime)); //if(character.navMeshAgent.isStopped == false) character.navMeshAgent.nextPosition = character.transform.position; } private void TurnToPoint(Vector3 point) { Quaternion targetRotation = Quaternion.LookRotation (new Vector3(point.x, character.transform.position.y, point.z) - character.transform.position); character.animator.transform.rotation = Quaternion.Slerp (character.animator.transform.rotation, targetRotation, 100f * Time.deltaTime); } public void End() { character.animator.SetFloat("horizontal", 0); character.animator.SetFloat("vertical", 0); character.navMeshAgent.isStopped = true; character.navMeshAgent.updatePosition = false; character.navMeshAgent.speed = 0; done = true; } public bool IsDone() { return done; } } }