npc wouldn´t stay where it is, when it enters a range

hey guys, i´ve got a little problem with a spaceship that should move to an astroid, after entering a distance the spacecraft should stop (later it should start orbit the the astroid)

the script checks the distance between spacecraft and the astroid(enemy station), if distance < range → move to astroid. if enter the orbitrange → stop (later it should orbit around the astroid), but i wouldnt stop

if(goHome) → fly to station
if(!goHome) → stop

so here we go:

var isChasing:boolean;
var seeDistance : float = 75;
var seeEnemyStation:float= 100000;
var EnemyStationOrbit:float = 180;

function Update () 
{
/////////////////////
////FIND PLAYER IN LVL
    var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("Player");
//assumes only one player!!!
    var thePlayer:GameObject = gos[0];
    var distToPlayer = Vector3.Distance(thePlayer.transform.position,transform.position);
	
	if(distToPlayer<=seeDistance){
        isChasing= true;
		//print(isChasing);
    } if(distToPlayer>seeDistance) {
        isChasing=false;
		//print(isChasing);
    }
	
//FIND ENEMY STATION
	var EnemyStation : GameObject[];
	EnemyStation = GameObject.FindGameObjectsWithTag("EnemyStation");
	var EStation :GameObject = EnemyStation[0];
	
	var distToEnemyStation= Vector3.Distance(EStation.transform.position,transform.position);

	 if((distToEnemyStation<=seeEnemyStation)  (EnemyStationOrbit <= distToEnemyStation))
        goHome= true;
	else
		goHome=false; /////HERE !!!!!!!!!!! it switches to false, but the ship is still moving
		transform.Translate(0,0,0);
// enemy moving

//transform.RotateAround (target.position, Vector3.up, degrees * Time.deltaTime);

//NO TAREGT? GO HOME!!!
	if(!isChasing  goHome)
	var rotate = Quaternion.LookRotation(EStation.transform.position - transform.position);
	transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime /5);
	transform.Translate(0,0,Time.deltaTime);
//THERE IS A TARGET IN RANGE - move to target, shoot
	if(isChasing  goHome)
	 { 
		if(seeDistance >=20)
			{
				var rotateToPlayer = Quaternion.LookRotation(thePlayer.transform.position - transform.position);
				transform.rotation = Quaternion.Slerp(transform.rotation, rotateToPlayer, Time.deltaTime /5);
				transform.Translate(0,0,Time.deltaTime);
			}
		else if (seeDistance < 20) // 20 units distance to player, orbit player, shoot
				transform.RotateAround (thePlayer.transform.position, Vector3.up, 10 * Time.deltaTime);
	}
	print(distToEnemyStation);
	print(goHome);
}

Hey thank you! This looks like what I was looking for. DO I add this to the PlayerController or the cube? I guess I could add it to a EmptyObject?

to your cube/ememy, what ever…but you have to tweak it;)

…got it…i just say “brackets”…:wink: oh my…
if(condition)
{ ALWAYS USE THESE F*CKING BRACKETS to do something}

I’ll try using it after work. Thanks again for the help.