Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to Pause and Resume NavMeshAgent on enemy AI script

Discussion in 'Navigation' started by Clintonsanto, Oct 29, 2015.

  1. Clintonsanto

    Clintonsanto

    Joined:
    Oct 14, 2015
    Posts:
    16
    Hi guys. i'm totally newb in scripting and unity so i got few newb question about enemy AI
    i'm about to mimic and modify the game from the unity tutorial ISOMETRIC SURVIVAL SHOOTER
    so the base scripts was mostly same
    i had the logic, just not the exact scripting for these improvement

    basically i want to make Enemy AI behavior is like:

    -Chasing endlessly to Player (already made the script and already working using Isometric survival shooter navmesh.agent tutorial guide)

    -When Player is close enough triggering enemy inRange collider i want to play the selected attack animation AND make the navmesh.agent function to PAUSE so the enemy is stop moving not sliding while playing attack animation

    -(optional because not related to the thread) if the player still inRange for brief moment after the animation played (when the enemy animation already swing the sword) that's when the damage is calculated so i don't want the player damaged exactly the moment its entering enemy collider.

    -If the player get out the enemy collider before the specified time and before damage is applied
    it will cancels the attack statement + (optional) i want a popup message or bubble "MISS" to be popped or played

    -Then if the attack was canceled i want the enemy to RESUME the agent (play the Run animation again) chasing player again and repeat from above

    well i just don't know what code shud i add/substitute/modify to the script in which scripts

    i know about the nav.Stop(); and nav.Resume(); function but don't know where should i put it with exact code

    HERE is current Scripts attached to my Enemy Object (there's also 2 scripts for my Player<PlayerHealth & PlayerMovement>

    EnemyAttack:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAttack : MonoBehaviour
    5. {
    6.     public float timeBetweenAttacks = 0.5f;
    7.     public int attackDamage = 10;
    8.  
    9.     Animator anim;
    10.     GameObject player;
    11.     PlayerHealth playerHealth;
    12.     bool playerInRange;
    13.     float timer;
    14.  
    15.     void Awake ()
    16.     {
    17.         player = GameObject.FindGameObjectWithTag ("Player");
    18.         playerHealth = player.GetComponent <PlayerHealth> ();
    19.         anim = GetComponent <Animator> ();
    20.     }
    21.  
    22.     void OnTriggerEnter (Collider other)
    23.     {
    24.         if(other.gameObject == player)
    25.         {
    26.             playerInRange = true;
    27.         }
    28.     }
    29.  
    30.     void OnTriggerExit (Collider other)
    31.     {
    32.         if(other.gameObject == player)
    33.         {
    34.             playerInRange = false;
    35.         }
    36.     }
    37.  
    38.  
    39.     void Update ()
    40.     {
    41.         timer += Time.deltaTime;
    42.  
    43.         if(timer >= timeBetweenAttacks && playerInRange/* && enemyHealth.currentHealth > 0*/)
    44.         {
    45.             Attack ();
    46.         }
    47.  
    48.         if(playerHealth.currentHealth <= 0)
    49.         {
    50.             anim.SetTrigger ("PlayerDead");
    51.         }
    52.     }
    53.  
    54.     void Attack ()
    55.     {
    56.         timer = 0f;
    57.  
    58.         if (playerHealth.currentHealth > 0)
    59.         {
    60.             anim.SetTrigger ("Attacking");//parameter
    61.                        //unfinished attack parameter
    62.             playerHealth.TakeDamage (attackDamage);
    63.         }
    64.     }
    65. }


    EnemyMovement:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyMovement : MonoBehaviour {
    5.  
    6.     Transform player;
    7.     NavMeshAgent nav;
    8.  
    9.     void Awake()
    10.     {
    11.         player = GameObject.FindGameObjectWithTag ("Player").transform;
    12.     }
    13.  
    14.  
    15.     void Update () {
    16.              nav.SetDestination (player.position);
    17.     }
    18. }
    19.  


    THANKS BEFORE! your help means so much to me!
    sorry for the english
     
    Last edited: Oct 29, 2015