// Update is called once per frame void Update() { if (moving) { if (destination == null || !destination.Exists()) { ReachedDestination(); } else { Vector3 closestToDest = GetComponent <Collider>().ClosestPoint(destination.Position); Vector3 closestToThis = destination.ClosestPoint(transform.position); NavMeshAgent agent = GetComponent <NavMeshAgent>(); if ((agent.destination - closestToThis).magnitude > UpdateDestinationRange) { agent.destination = closestToThis; } closestToDest.y = 0; closestToThis.y = 0; float targetRange = attackMovement ? GetComponent <AttackUnit>().GetWeapon().GetRange() : InteractionRange; if ((closestToThis - closestToDest).magnitude < targetRange) { ReachedDestination(); } } } }
public bool DestinationReachable(Destination dest) { NavMeshAgent agent = GetComponent <NavMeshAgent>(); Vector3 destinationPoint = dest.ClosestPoint(transform.position); NavMeshPath path = new NavMeshPath(); return(agent.CalculatePath(destinationPoint, path) && path.status == NavMeshPathStatus.PathComplete); }
public void MoveTo(Destination dest, System.Action action, bool isAttacking = false) { // Don't do anything if there's no path to the destination if (DestinationReachable(dest)) { moving = true; attackMovement = isAttacking; MoveAnimation(); NavMeshAgent agent = GetComponent <NavMeshAgent>(); agent.destination = dest.ClosestPoint(transform.position); agent.isStopped = false; destination = dest; actionOnArrival = action; } }