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.

Stopping navmesh AI from getting too close

Discussion in 'Scripting' started by Sajid, May 1, 2013.

  1. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Hey,

    I've got a sort of zombie game, with the enemy AI using navmesh as seen here:
    Code (csharp):
    1. var Target : Transform;
    2. var distance : int;
    3. var Health : int = 3;
    4. var tarmoney : int;
    5. var moneySpawn : GameObject;
    6. var player : GameObject;
    7. var playerDistance : int;
    8. var CPU : GameObject;
    9. var isTargetPlayer : boolean;
    10. var attacking : boolean;
    11. var moveSpeed : int;
    12. var rotationSpeed : int;
    13. var myTransform : Transform;
    14. public var following = boolean;
    15. var exploder : boolean;
    16. var explosion : GameObject;
    17. var splitter : boolean;
    18. var split : GameObject;
    19. private var NavComponent: NavMeshAgent;
    20. function Start()
    21. {  
    22.     NavComponent = this.transform.GetComponent(NavMeshAgent);
    23.    
    24.     player = GameObject.Find("Player");
    25.     CPU = GameObject.Find("CPU");
    26.     Target = CPU.transform;
    27.     attacking = false;
    28.     myTransform = transform;
    29.     following == true;
    30.    
    31. }
    32.  
    33. function Update()
    34. {
    35.     if(following)
    36.     {
    37.         NavComponent.SetDestination(Target.position);
    38.     }
    39.  
    40.     transform.LookAt(Vector3(Target.position.x, transform.position.y, Target.position.z));
    41.     if(playerDistance <=4)
    42.     {
    43.         following == false;
    44.        
    45.     }
    46.     else
    47.     {
    48.         following == true;
    49.     }
    50.     // determine target
    51.     if(playerDistance < 10)
    52.         {
    53.         Target = player.transform;
    54.         isTargetPlayer = true;
    55.         }
    56.     else
    57.     {
    58.         Target = CPU.transform;
    59.         isTargetPlayer = false;
    60.     }
    61.     // determine target
    62.    
    63.         //determine distance from player
    64.     playerDistance = Vector3.Distance(player.transform.position, transform.position);
    65.     distance = Vector3.Distance(Target.transform.position, transform.position);
    66.     //determine distance from player
    67.    
    68.     //determine when to attack
    69.     if(distance <= 2)
    70.     {
    71.         if(exploder)
    72.         {
    73.             Explode();
    74.         }
    75.         else
    76.         if(!attacking)
    77.         {
    78.                 Invoke("SubtractHealth",1);
    79.                 attacking = true;
    80.         }
    81.     }
    82.     //determine when to attack
    83.    
    84.     //determine when to die
    85.  
    86.     if(Health > 3)
    87.     {
    88.         Health = 3;
    89.     }
    90.     if(Health < 0)
    91.     {
    92.         Health = 0;
    93.     }
    94.     if(Health == 0)
    95.     {
    96.     Instantiate(moneySpawn, transform.position, transform.rotation);
    97.     Destroy(gameObject);
    98.     }
    99.     //determine when to die
    100.    
    101.     if(isTargetPlayer)
    102.     {
    103.     tarmoney = Target.GetComponent(PlayerAttributes).Money;
    104.     }
    105.    
    106.     //look at target
    107.     if(following)
    108.     {
    109.    
    110.     }
    111.     else
    112.     {  
    113.         transform.LookAt(Vector3(Target.position.x, transform.position.y, Target.position.z));
    114.     }
    115.     //look at target
    116. }
    117.  
    118.  
    119.  
    120. function SubtractHealth()
    121. {
    122.     if(isTargetPlayer == false)
    123.     {
    124.         CPU.GetComponent(CPUAttributes).ApplyDamage(2);
    125.     }
    126.     else
    127.     {
    128.         player.GetComponent(PlayerAttributes).ApplyDamage(3);
    129.     }
    130.      attacking = false;
    131. }
    132.  
    133. function OnTriggerEnter(other: Collider)
    134. {
    135.     if(other.tag == "Shot")
    136.     {
    137.         if(splitter == false)
    138.         {
    139.         Health --;
    140.         }
    141.         else
    142.         {
    143.         Split();
    144.         }
    145.     }
    146.  
    147. }
    148.  
    149. function Explode()
    150. {  
    151.     yield WaitForSeconds(1);
    152.     player.GetComponent(PlayerAttributes).ApplyDamage(10);
    153.     Destroy(gameObject);
    154.     Instantiate (explosion, transform.position, transform.rotation);
    155. }
    156.  
    157. function Split()
    158. {  
    159.     Destroy(gameObject);
    160.     var size = 1;
    161.  
    162.             for (var i = 0; i < 4; i++)
    163.             {
    164.                 var angle = 36 * i;
    165.                 var rad = angle * Mathf.PI / 180.0F;
    166.                 var x = Mathf.Cos(rad);
    167.                 var z = Mathf.Sin(rad);
    168.                 Instantiate(split, transform.position + Vector3(x * size, 0, z * size), Quaternion.identity);
    169.             }
    170. }
    The part that isnt working is here:
    Code (csharp):
    1. function Update()
    2. {
    3.     if(following)
    4.     {
    5.         NavComponent.SetDestination(Target.position);
    6.     }
    7.  
    8.     transform.LookAt(Vector3(Target.position.x, transform.position.y, Target.position.z));
    9.     if(playerDistance <=4)
    10.     {
    11.         following == false;
    12.        
    13.     }
    14.     else
    15.     {
    16.         following == true;
    17.     }

    Basically I need the enemies to stop walking towards the player when they get close to him, because if they dont they clip and end up spinning the character model around. Not to mention it looks wrong.

    Thing is, that doesnt work. When they get that close the navmesh agent continues to function and they continue to move forward.

    How can I accomplish this?
     
  2. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Nevermind I figured it out myself.

    You have to use NavComponent.enabled = false;


    As so:
    Code (csharp):
    1. function Update()
    2. {
    3.     transform.LookAt(Vector3(Target.position.x, transform.position.y, Target.position.z));
    4.     if(playerDistance <=4)
    5.     {
    6.         following == false;
    7.         NavComponent.enabled = false;
    8.        
    9.     }
    10.     else
    11.     {
    12.         following == true;
    13.         NavComponent.SetDestination(Target.position);
    14.         NavComponent.enabled = true;
    15.     }
     
    ielgohary likes this.
unityunity