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

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…)

how bout checking distance and:

if(distance < stopRange){
stopMoving;
}

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

yeah but then there is a problem with which unit it is checking

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.

function Radar(){//--------RADAR - RETURNS CLOSEST OBJECT
	var wpnRangeC = wpnRange;
	var rdrRangeC = rdrRange;
	var enemies = GameObject.FindGameObjectsWithTag("Target"); 
	rdrTargetArray.Clear();
	wpnTargetArray.Clear();
	wpnTarget = null;
	rdrTarget = null;	
	
	for(var e in enemies){
		var dist = Vector3.Distance(e.transform.position, transform.position);
		
		if(dist < wpnRange){
			wpnTargetArray.Add(e);
			if(dist < wpnRangeC){
				wpnRangeC = dist;
				wpnTarget = e;
			}		
		}
		if(dist < rdrRange  dist > wpnRange){
			rdrTargetArray.Add(e);
			if(dist < rdrRangeC){
				rdrRangeC = dist;
				rdrTarget = e;
			}
		}
	}	
return;
}

can you explain how all this stuff works?

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…

how does it affect fps? im making an online game so hopefully this isnt too ram intensive.

can somebody convert this to c#? i give up lol

public void Radar(){

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

foreach(GameObject e in enemies){ //----------this loops through each obj in enemies and checks distance
float 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. --------------------*/

}
}

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

Check out my post here:
http://forum.unity3d.com/threads/99700-How-to-make-zombie-attack-other-gameobjects-via-tags

It sounds like the same basic issue.

You can just stop when something is in range. The method outlined is extremely light.

lol.

thanks guys