Enemy only moves towards Player when sqrMagnitude is greater or equal to...

Hi guys,

I’m trying to get an enemy to move towards my player tagged player within the radius defined. However, no matter the number defined the enemy will only move if if(heading.sqrMagnitude >= radius * radius).

Below you can see the relevant code:

function Update () {

	target = GameObject.FindWithTag("Player").transform;
	var heading = target.position - myTransform.position;
	
	var distance = heading.magnitude;
	var direction = heading / distance;
	heading.y = 0;

	if(target.tag == "Player")    {
		if(heading.sqrMagnitude <= radius * radius){
			
	    //rotate to look at the player 
			myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
			
			//move towards the player
			myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
		}
		
	}

}

what you can do is add a boolean and a collider. if your enemy collides with the player you change the boolean from true to false(however you wan). this way you can make the enemy follow you till he has the distance you want on the collider