Search Unity

getting units to stop when an enemy is in front of them?

Discussion in 'Scripting' started by Feyhu, Aug 9, 2011.

  1. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    How do I make my units stop when the enemy is within a certain distance?

    The game has two sides sending enemies from their respective bases (one at left and one at right along z axis)
    I can send units at the base on the right but when they get close to enemy units i want them to stop.

    (preferably without collision since i dont have anything set up for colliders...)
     
  2. baronsamedi

    baronsamedi

    Joined:
    May 31, 2011
    Posts:
    29
    how bout checking distance and:

    if(distance < stopRange){
    stopMoving;
    }

    or inverse
    if(stoprange < distance){
    moveToTarg;
    }
     
  3. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    yeah but then there is a problem with which unit it is checking
     
  4. baronsamedi

    baronsamedi

    Joined:
    May 31, 2011
    Posts:
    29
    which unit do you want? the closest? heres somethin im workin on, so not nearly perfected yet, but it gets units tagged as enemy, then for each gets distance, if dist < weaponRange it adds to weaponTargetArray, then it gets the closest and sets as currentTarget.

    Code (csharp):
    1.  
    2. function Radar(){//--------RADAR - RETURNS CLOSEST OBJECT
    3.     var wpnRangeC = wpnRange;
    4.     var rdrRangeC = rdrRange;
    5.     var enemies = GameObject.FindGameObjectsWithTag("Target");
    6.     rdrTargetArray.Clear();
    7.     wpnTargetArray.Clear();
    8.     wpnTarget = null;
    9.     rdrTarget = null;  
    10.    
    11.     for(var e in enemies){
    12.         var dist = Vector3.Distance(e.transform.position, transform.position);
    13.        
    14.         if(dist < wpnRange){
    15.             wpnTargetArray.Add(e);
    16.             if(dist < wpnRangeC){
    17.                 wpnRangeC = dist;
    18.                 wpnTarget = e;
    19.             }      
    20.         }
    21.         if(dist < rdrRange  dist > wpnRange){
    22.             rdrTargetArray.Add(e);
    23.             if(dist < rdrRangeC){
    24.                 rdrRangeC = dist;
    25.                 rdrTarget = e;
    26.             }
    27.         }
    28.     }  
    29. return;
    30. }
    31.  
     
  5. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    can you explain how all this stuff works?
     
  6. baronsamedi

    baronsamedi

    Joined:
    May 31, 2011
    Posts:
    29
    well my code does a lot of stuff that even im not using atm, basically this is all you need:

    function Radar(){

    var closestTarg : GameObject;
    var maxDetectionRange = 300.0;
    var enemies = GameObject.FindGameObjectsWithTag("Target"); //---this assigns all objects with "tag" to the variable 'enemies'

    for(var e in enemies){ //----------this loops through each obj in enemies and checks distance
    var dist = Vector3.Distance(e.transform.position, transform.position);

    if(dist < maxDetectionRange){
    maxDetectionRange = dist;
    closestTarget = e;
    }
    /*-----------as it loops through it sets closest target as object with least distance, and sets that distance to be the one checked for following objects, this way as it goes through list you will end up with the absolute closest object. --------------------*/

    }
    }


    hopefully that makes sense...
     
    Last edited: Aug 10, 2011
  7. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    how does it affect fps? im making an online game so hopefully this isnt too ram intensive.
     
  8. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    can somebody convert this to c#? i give up lol
     
  9. Eiznek

    Eiznek

    Joined:
    Jun 9, 2011
    Posts:
    374
    Code (csharp):
    1. public void Radar(){
    2.  
    3. GameObject closestTarg;
    4. float maxDetectionRange = 300.0f;
    5. GameObject[] enemies = GameObject.FindGameObjectsWithTag("Target"); //---this assigns all objects with "tag" to the variable 'enemies'
    6.  
    7. foreach(GameObject e in enemies){ //----------this loops through each obj in enemies and checks distance
    8. float dist = Vector3.Distance(e.transform.position, transform.position);
    9.  
    10. if(dist < maxDetectionRange){
    11. maxDetectionRange = dist;
    12. closestTarget = e;
    13. }
    14. /*-----------as it loops through it sets closest target as object with least distance, and sets that distance to be the one checked for following objects, this way as it goes through list you will end up with the absolute closest object. --------------------*/
    15.  
    16. }
    17. }

    Not pretty just copy and pasted his code and threw it in Notepad
     
  10. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
  11. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    thanks guys