# How to make goalKeeper move within post but not move towards to the player?

Hello so basically I make a 2d top-down soccer game but I have to trouble with GK AI, I want to GK is followed the ball but not move toward on it

let say (Enemy = GK)  so when the player moves to the left or right the enemy will follow to the right or left not The Yaxis but in X-axis with guarding the post or Y=0

my default enemy movement is like this :

if (gameObject.CompareTag("GK"))
{

anim.SetBool("GKRun", false);
float distancePlayer = Vector2.Distance(target.position, transform.position);

if (gkballInRange)
{
if (distancePlayer <= gkballRange)
{
//Animation
anim.SetBool("GKRun", true);
transform.Translate(0, -speed * Time.deltaTime, 0);
}
}

}


in that scrip, when the ball is on the range the GK is moving to the ball just that. maybe anyone can explain to me how to do it?

• Looks like you just need to change the last line to transform.Translate(speed * Time.deltaTime, 0, 0); The parameters to it are (x, y, z), and currently, only the y parameter is set to a non-zero value (speed * time gives you distance, and I'm guessing it's negative because the y-axis points down). Just copy that value, then place a zero there, and paste it as the x parameter. Also, change the sign (if you leave the minus sign, I think the GK will avoid the ball). – Filip Milovanović Nov 1 '20 at 7:29
• @FilipMilovanović yes like that with max area but its always to the right also what I want is like Vector3.Movetoward but y=0 – YouCanCallMe Syarif Nov 1 '20 at 8:10
• Oh, I get it - it only moves one way. See my answer below. – Filip Milovanović Nov 1 '20 at 9:43

OK, this is the situation you have: You can obtain the GK-to-Player vector by doing target.Position - transform.Position, and you can use that vector both to determine how the goalkeeper should move and to check if the player is within the goalkeeper's range. I'll refer to this vector as relativePlayerPosition.

Currently, though, you're just calculating the distance to the player, and loosing any information about directions:

float distancePlayer = Vector2.Distance(target.position, transform.position);


If you look at the docs for Vector2.Distance, you'll see that it's calculated as
(target.position - transform.position).magnitude.

So, you can do the same thing "manually", but keep the intermediate result. Calculate the relative position vector and store it into a variable, then use magnitude to get the distance:

Vector2 relativePlayerPosition = target.position - transform.position;
float distancePlayer = relativePlayerPosition.magnitude;


If the player is to the left of the goalie, the x component of this vector will be negative. If the player is to the right, it will be positive. You can use this fact to change the direction towards which the goalkeeper moves.

To obtain the sign of the x component, use the Mathf.Sign function (returns -1 for negative values, and 1 otherwise).

float xDirection = Mathf.Sign(relativePlayerPosition.x);


Finally, use this to modify the direction (sign) of the x parameter for the Translate method:

transform.Translate(xDirection * speed * Time.deltaTime, 0, 0);

• Thank you but its same i fix it by adding not transform.Translate(target.transform.position.x * speed * Time.deltaTime, 0, 0); and limited the movement in x var pos = transform.position; pos.x = Mathf.Clamp(transform.position.x, -1.3f, 1f); transform.position = pos; – YouCanCallMe Syarif Nov 6 '20 at 8:27