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
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);
gkballInRange = Physics2D.OverlapCircle(transform.position, gkballRange, playerMask);
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?
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). \$\endgroup\$Vector3.Movetoward
but y=0 \$\endgroup\$