private float attackCounter; // Counter to count when the next attack comes void Awake() { behaviours = new List <GenericAIBehaviour> (); anim = GetComponent <Animator> (); agent = GetComponent <UnityEngine.AI.NavMeshAgent> (); stats = GetComponent <GeneralStats> (); currentCommand = stats.GetAIStats.GetCommand; speedFloat = Animator.StringToHash("Speed"); attackTrigger = Animator.StringToHash("Attack"); }
void Update() { foreach (var activeBehaviour in behaviours) { if (activeBehaviour.GetBehaviourCode() == currentBehaviour) { // Pass the AIStats to the active, current behaviour currentCommand = activeBehaviour.SetCommand(stats); // call LocalUpdate for the active, current behaviour activeBehaviour.LocalUpdate(); // Set the AITag to the active, current behaviour currentTag = activeBehaviour.localTag; } } PerformCommand(currentCommand, currentAttkTarget, currentMoveDest); }
// ------------------------------------------------------------------------------------------ // AI commands are low level commands that the BasicAIBehaviour can translate to animation // The basic AI behaviour will call this every update // Other behaviours must modify AIStats.Command based on their behaviour and attack + move dest // ------------------------------------------------------------------------------------------ void PerformCommand(AIStats.Command command, GameObject attackingTarget, Vector3 moveDest) { // To use command Run and Sprint, set moveDestination to the position. if (currentCommand == AIStats.Command.SprintingToDest) { agent.destination = moveDest; anim.SetFloat(speedFloat, stats.GetCharacterStats.GetSprintingSpeed); } if (currentCommand == AIStats.Command.RunningToDest) { agent.destination = moveDest; anim.SetFloat(speedFloat, stats.GetCharacterStats.GetRunningSpeed); } if (stats.GetAIStats.GetCommand == AIStats.Command.WalkingToDest) { agent.destination = moveDest; anim.SetFloat(speedFloat, stats.GetCharacterStats.GetWalkingSpeed); } // Idle command sits the AI in place. if (stats.GetAIStats.GetCommand == AIStats.Command.Idle) { agent.destination = moveDest; anim.SetFloat(speedFloat, 0.0f); } // To use command AttackingDest, set attackingTarget to the target. // The AttackAIBehaviour will take care of extra work such as laserLine and animation functions if (currentCommand == AIStats.Command.AttackingDest) { if (attackingTarget == null) { Debug.Log("Target for attack is null"); return; } // When attacking, run faster than normal if (agent.remainingDistance <= stats.GetPrimaryWStats.GetRange) { anim.SetFloat(speedFloat, 0f); } else { anim.SetFloat(speedFloat, stats.GetCharacterStats.GetAttackMoveSpeed); } if (attackCounter > stats.GetPrimaryWStats.GetAttackDelay && (agent.remainingDistance <= stats.GetPrimaryWStats.GetRange)) { anim.SetTrigger(attackTrigger); attackCounter = 0f; } // Turn the AI ourselves using transform.RotateTowards or LookAt agent.angularSpeed = 0.0f; transform.LookAt(new Vector3(attackingTarget.transform.position.x, transform.position.y, attackingTarget.transform.position.z)); Quaternion direction = Quaternion.LookRotation(attackingTarget.transform.position - transform.position); transform.rotation = Quaternion.RotateTowards(transform.rotation, direction, stats.GetAIStats.GetAimspeed); moveDest = attackingTarget.transform.position; } else { agent.angularSpeed = stats.GetAIStats.GetTurnspeed; } // Attack cooldown ticks regardless of action attackCounter += Time.deltaTime; }