// Behaviours // PATROL void Patrol() { if (patrolPath == null) { return; } timeSinceArrivedAtWaypoint += Time.deltaTime; Vector3 nextPosition = originalPosition; if (AtWaypoint()) { timeSinceArrivedAtWaypoint = 0f; CycleWaypoint(); } nextPosition = GetCurrentWaypoint(); if (timeSinceArrivedAtWaypoint > waypointDwellTime) { // MoveTo(nextPosition); AI_Helpers.MoveTo( nextPosition, maxSpeed, patrolSpeedFraction, navMeshAgent ); } return; }
// Components void HandleFacePlayer() { bool facePlayer = state == AGENT_STATE.CHASE || state == AGENT_STATE.FIGHTING; if (facePlayer) { AI_Helpers.FaceTarget( target, this.gameObject ); } }
// CHASE void Chase() { Debug.Log("target:::" + target); if (target == null) { return; } MakeTargetStrafe(); Vector3 destination = targetLastKnownPosition; float currentDistance = Vector3.Distance(target.transform.position, transform.position); // Has reached target if (currentDistance <= stoppingDistanceToPlayer) { state = AGENT_STATE.FIGHTING; return; } // Player escaped if (currentDistance > maxChasingDistance) { state = AGENT_STATE.PATROL; return; } // Has Stamina To Continue chase? if (stamina.HasStaminaAgainstCostAction(staminaChasingCost)) { AI_Helpers.MoveTo(destination, maxSpeed, patrolSpeedFraction, navMeshAgent); stamina.DecreaseStamina(staminaChasingCost); } else { // Stamina depleted return; } }
public override void Dispatch() { if (context.target == null) { context.SetState(AGENT_STATE.PATROL); return; } if (context.target.GetComponent <Health>().IsDead()) { context.SetState(AGENT_STATE.PATROL); return; } // If my health is low if (context.health.IsLowHealth()) { float chanceToFlee = UnityEngine.Random.Range(0f, 1f); if (chanceToFlee >= context.minimumChanceToFleeIfLowHealth) { context.SetState(AGENT_STATE.FLEE); return; } } // If my target is far away if (AI_Helpers.TargetIsFarAway( context.target, context.gameObject, context.gameObject.GetComponent <WeaponManager>() )) { context.SetState(AGENT_STATE.CHASE); return; } if (inProgress) { return; } // Target is attacking, let's receive the attack and decide later if we should dodge or defend if (context.target.GetComponent <Battler>().IsAttacking()) { float chanceToNotAttack = UnityEngine.Random.Range(0, 1f); if (chanceToNotAttack >= 0.1f) { return; } } float chanceToAttack = UnityEngine.Random.Range(0, 1f); if (chanceToAttack < context.minimumChanceToAttack) { Debug.Log("Hit twice"); context.SetState(AGENT_STATE.CHASE); return; } context.StartCoroutine(Attack()); }