How to keep distances (guard)

I have 2 first person controllers.I use the one that moves forward and the other one is guarding me (he is a defender).I want him to be always between me and the goal,but not far away from the goeal , guarding me.Here is my code.

var attacker:Transform;
var speed:float = 1.0;
var mindist : int = 6;
var mindistfromGoal : int=3;

var BasketballRack:Transform;

function Update () {


var lineToAttacker = attacker.transform.position - transform.position; 
var distanceToAttacker = Vector3.Distance(attacker.position, transform.position);


var lineToRack = BasketballGoal.transform.position - transform.position;
var distanceToRack = Vector4.Distance(BasketballRack.position, transform.position);


if ((distanceToAttacker > mindist) && (distanceToRack < mindistfromRack)) {
transform.Translate(lineToAttacker * Time.deltaTime * speed);
}

}

I’m sure something is missing or it is wrong.
Anyone help please?

This sounds like a good job for a relaxation method. You basically create a “force” which is proportional to how bad each constraint is violated. Think of a spring between the defender and the goal, and another one between the defender and the line between you and the goal. The springs pull on the defender until they reach equilibrium and he stops moving. His position then is the “best” place for him in the sense that it is a least-squares minimum of the error energy represented by the stretched springs.

You can adjust the spring strengths and add dampening to prevent oscillation. You could actually add these as real forces to the defender, or just compute the final position yourself (this is probably better from a controllability standpoint).

Look for Jacobi, Gauss-Siedel, or Successive Over-relaxation (SOR).