private IEnumerator UpdateDestination() { while (!isDead) { //Calculate and visualize the target position. Vector3 targetPosition = AI_Utilities.GetTargetNextFramePosition(transform.position, target.position, motor.data.controller.velocity); Debug.DrawLine(transform.position, targetPosition, Color.red, 0.15f); //Calculate a path to the player's next frame position. NavMeshPath path = new NavMeshPath(); NavMesh.CalculatePath(transform.position, targetPosition, NavMesh.AllAreas, path); //Update the destination. if (path.status == NavMeshPathStatus.PathComplete) { agent.SetPath(path); } else { agent.SetDestination(target.position); } //Calculate the distance to the target and check if the enemy is in attack range. float distanceToTarget = Vector3.Distance(transform.position, target.position); isInAttackRange = (distanceToTarget < attackDistance); agent.updateRotation = !isInAttackRange; //Match the animation speed to the movement. float moveMagnitude = new Vector2(agent.velocity.x, agent.velocity.z).magnitude / agent.speed; animator.SetFloat("MoveMagnitude", Mathf.Clamp01(moveMagnitude)); //Delay the next update. yield return(new WaitForSeconds(0.15f)); } }
private IEnumerator UpdateRotation() { agent.updateRotation = false; while (!isDead) { Quaternion targetRotation = AI_Utilities.GetRotationToTarget(transform.position, target.position); transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * 360); yield return(null); } }