public override void Update() { // check if AI agent has arrived to a waypoint if (agent.remainingDistance < 1) { // check if the npc has finished walking through all waypoints if (currentIndex >= GameEnvironment.Singleton.Checkpoints.Count - 1) { currentIndex = 0; // Reset the patrol path } else { currentIndex++; // set next waypoint as destination } // move the npc agent to waypoint at the current index count agent.SetDestination(GameEnvironment.Singleton.Checkpoints[currentIndex].transform.position); } // if AI saw the player, then exit patrol state and start chasing if (CanSeePlayer()) { nextState = new StatePursue(npc, agent, anim, player); stage = EVENT.EXIT; } }
public override void Update() { // if AI saw the player, then exit idle state and start chasing if (CanSeePlayer()) { nextState = new StatePursue(npc, agent, anim, player); stage = EVENT.EXIT; } // Set a check to exit the Update stage and go to next one (Exit), otherwise will be stuck in Update forever // This condition creates 10% chance to exit Update stage else if (Random.Range(0, 100) < 10) { nextState = new StatePatrol(npc, agent, anim, player); stage = EVENT.EXIT; } }
public override void Update() { // get the directional vector between AI and player Vector3 direction = player.position - npc.transform.position; // remove vertical component value to avoid tilting AI character direction.y = 0; // rotate AI character to face player while shooting npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); // if player is out of AI's attack range but AI can still see them, then move to chasing state if (!CanAttackPlayer() && CanSeePlayer()) { nextState = new StatePursue(npc, agent, anim, player); stage = EVENT.EXIT; } // if AI lost sight of the player, then exit state and go idle else if (!CanSeePlayer()) { nextState = new StateIdle(npc, agent, anim, player); stage = EVENT.EXIT; } }