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 ZombieMoveToPositionAction : IAction { CharacterController character; Vector3 targetPosition; int varient; float speed = 1f; float animationSpeed = 1f; bool done = false; // Constructor public ZombieMoveToPositionAction(CharacterController character, Vector3 targetPosition, int varient) { this.character = character; this.targetPosition = targetPosition; this.varient = varient; switch(varient) { case 1: speed = 1.2f; animationSpeed = 1f; break; case 2: speed = 1.2f; animationSpeed = 1f; break; case 3: speed = 1.2f; animationSpeed = 1f; break; case 4: speed = 1.2f; animationSpeed = 1f; break; case 5: speed = 1.2f; animationSpeed = 1f; break; case 6: speed = 1.2f; animationSpeed = 1f; break; case 7: speed = 1.2f; animationSpeed = 1f; break; case 8: speed = 2f; animationSpeed = 2f; break; case 9: speed = 2.2f; animationSpeed = 2f; break; default: speed = 1f; animationSpeed = 1f; break; } } public void Start() { character.navMeshAgent.destination =targetPosition; character.navMeshAgent.updatePosition = false; character.navMeshAgent.updateRotation = false; character.navMeshAgent.isStopped = true; character.navMeshAgent.speed = speed; character.navMeshAgent.stoppingDistance = 0.5f; } public void Update() { character.animator.SetFloat("ZombieMoveVarient", varient); 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 (point - 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; } } }