Question by
syarifabdr22 · Nov 01, 2020 at 05:18 AM ·
unity 2daimovement scripttopdown
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);
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?
Comment
Answer by AltaNox · Nov 02, 2020 at 05:33 PM
The problem is that you are Translating in the Y-axis instead of the X-axis. The translate method adds values in this order (x , y , z)
So the issue is here:
//Problematic line
transform.Translate(0,-speed*Time.deltaTime,0)
//You should be doing
transform.Translate(-speed*Time.deltaTime,0,0);