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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

AI issue

Discussion in 'Scripting' started by oldcollins, Dec 13, 2016.

  1. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    i'm attempting to get the AI unit within my game to fist find any flags within the scene and collect them and then once all flags are collected move to exit its NavMeshAgent based AI

    here's my script so far any help would be great

    Code (JavaScript):
    1.  private var agent : NavMeshAgent;
    2. private var GameObject;
    3. var flag:Transform;
    4. var exit:Transform;
    5. var flagcount = 1;
    6. private var gos : GameObject[];
    7. private   var ext : GameObject[];
    8.      function Start(){
    9.        
    10.      //var gos : GameObject[];
    11.     // var ext : GameObject[];
    12.      agent = GetComponent.<NavMeshAgent>();
    13.      //eninmeGameObject = GameObject.FindWithTag ( "eventtile" );
    14.        
    15.      gos = gameObject.FindGameObjectsWithTag("flag");
    16.      ext = gameObject.FindGameObjectsWithTag("exit");
    17.      if (gos.length == 0) {
    18.          Debug.Log("No game objects are tagged with feg");
    19.    
    20.      }
    21.   if (ext.length == 0) {
    22.          Debug.Log("No game objects are tagged with feg");
    23.    
    24.      }
    25.      }
    26.    
    27.    
    28.      function Update(){
    29.      //agent.SetDestination(goal.transform.position);
    30.    
    31.      if(flagcount >= 0){
    32.      
    33.       if (gos.length >=0 ) {
    34.         agent.destination = flag.position;
    35.    
    36.            }  
    37.          }
    38.    
    39.     else
    40.        
    41.      if(flagcount == 0){
    42.        
    43.          if (gos.length == 0) {
    44.          
    45.             if (ext.length >= 0) {
    46.             agent.destination = exit.position;
    47.                  }
    48.            }
    49.          }
    50.    
    51.      }
    52.    
    53.    
    54.      function OnTriggerEnter (other : Collider) {
    55.      if (other.gameObject.tag == "flag"){
    56.         other.gameObject.active = false;
    57.         flagcount-=1;
    58. }
    59.      }
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    line 43, you state, gos.length == 0, remove that if from your logic. gos, which is a predetermined list of objects will never be equal to 0. Since you never remove things from that list, it's count will never change.

    For that matter:
    right after line 15, set the flagcount to gos.Length
    remove the if at line 33 as it is not necessary.
     
  3. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    i've made the changes ass suggested and it still only goes for one flag and does not move to exit
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Untested, but this is a cleaner representation of what you want.

    Code (csharp):
    1. private var agent : NavMeshAgent;
    2. private var GameObject;
    3. var flags : GameObject[];
    4. var target : GameObject;
    5. var exit : GameObject;
    6. private index : int = 0;
    7. private var lastTarget : GameObject;
    8. function Start() {
    9.     // get the current agent
    10.     agent = GetComponent. < NavMeshAgent > ();
    11.     // store the flags
    12.     flags = gameObject.FindGameObjectsWithTag("flag");
    13.     // if we dont have enough, just quit
    14.     if (flags.length == 0) {
    15.         Debug.Log("No game objects are tagged with feg");
    16.     }
    17.    
    18.     // find the first exit and store it
    19.     var exits = gameObject.FindGameObjectsWithTag("exit");
    20.     if (exits.length == 0) {
    21.         Debug.Log("No game objects are tagged with feg");
    22.     } else {
    23.         exit = exits[0];
    24.     }
    25.     // set the first target
    26.     SetTarget();
    27. }
    28.  
    29. function Update() {
    30.     // if we have no target for any reason, quit
    31.     if(target == null){ return; }
    32.     // set the target to the next point we need to go to.
    33.     SetTarget();
    34.    
    35.     // if that point is not the same as the last one, we need to update the destination.
    36.     if(lastTarget != target){
    37.         agent.destination = target.transform.position;
    38.     }
    39.     // store the curren target, so we see if we have changed frame to frame
    40.     lastTarget = target;
    41.    
    42.     // if we are within 3 units of a flag, or the exit, get the next point to go.
    43.     if((target.transform.position - transform.position).sqrMagnitude < 9){
    44.         index++;
    45.     }
    46. }
    47.  
    48. // this will get the target from the current index
    49. function SetTarget(){
    50.     // set the target to null
    51.     target = null;
    52.     // if we are above the count + the exit, just return to stop more from happening
    53.     if(index > flags.Length) return;
    54.     // if we are equal to the number of flags, exit
    55.     if(index == flags.Length){
    56.         target = exit;
    57.     } else {
    58.         // if not, just pick that flag
    59.         target = flags[index];
    60.     }
    61. }
     
  5. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    well its working a lot better then the way i was going but its not picking up the flags it gets near them and then goes to exit