protected override bool ExecuteBehaviour(NewTaskCallback onTaskChangeCallback) { // Chase target and attack in melee if (aiController.CombatTarget != null && aiController.CombatTarget.GetComponent <CombatStats>().IsAlive) { float distance = Vector3.Distance(owner.transform.position, aiController.CombatTarget.transform.position); if (distance > aiController.SearchRadius) { aiController.IsAttackingMelee = false; // Later, make this event based. All animations should be handled within AIController. characterAnimator.SetBool("IsAttackingMelee", aiController.IsAttackingMelee); aiController.CombatTarget = null; navAgent.isStopped = false; //StartCoroutine(LookForTargetTask()); onTaskChangeCallback.Invoke(this, taskMngr.GetTaskOfType <LookForTarget>()); return(false); } else if (distance > (aiController.MeleeDistance - 1f)) { aiController.IsAttackingMelee = false; characterAnimator.SetBool("IsAttackingMelee", aiController.IsAttackingMelee); if (navAgent.destination != aiController.CombatTarget.transform.position) { navAgent.destination = aiController.CombatTarget.transform.position; } navAgent.isStopped = false; } else { if (!aiController.IsAttackingMelee) { // Set melee enabled should probably also be some kind of event. aiController.SetMeleeEnabled(true); characterAnimator.SetFloat("MeleeAttackSpeed", combatStats.MeleeAttackSpeed); // Need some kind of way to stop this once the the task is completed. taskMngr.StartCoroutine(aiController.RotateTowardsTarget()); } } } else { aiController.CombatTarget = null; aiController.SetMeleeEnabled(false); //StartCoroutine(LookForTargetTask()); onTaskChangeCallback.Invoke(this, taskMngr.GetTaskOfType <LookForTarget>()); return(false); } return(true); }
protected override bool ExecuteBehaviour(NewTaskCallback onTaskChangeCallback) { if (!aiController.SearchForTarget()) { if (Vector3.Distance(owner.transform.position, navAgent.destination) < 1f) { onTaskChangeCallback.Invoke(this, taskMngr.GetTaskOfType <Guard>()); return(false); } } else { taskMngr.DetermineAttackTask(this); return(false); } return(true); }
protected override bool ExecuteBehaviour(NewTaskCallback onTaskChangeCallback) { // Look around for target for a bit by walking to random points within the AI entity's search radius. bool foundTarget = false; if (searchTimer >= searchTime) { searchTimer = 0f; // Set to return to start position. onTaskChangeCallback.Invoke(this, taskMngr.GetTaskOfType <ReturnToOrigin>()); return(true); } else { foundTarget = aiController.SearchForTarget(); if (!foundTarget) { if (Vector3.Distance(owner.transform.position, navAgent.destination) > 1f) { // Do nothing? //Debug.Log("Distance more than 1f"); } else { //Debug.Log("Trying to find new destination..."); Vector3 newDestination; if (aiController.GetRandomVectorInSearchRadius(out newDestination)) { //Debug.Log("Found new destination! At vector: " + newDestination); navAgent.destination = newDestination; } } } } searchTimer += AIManager.TASK_UPDATE_RATE; //Debug.Log("Running: " + GetType() + ", on " + owner.name + ", foundTarget " + foundTarget + " , at " + Time.time + ", SearchTimer at " + searchTimer); if (foundTarget) { taskMngr.DetermineAttackTask(this); } return(!foundTarget); }