# Need Help with Enemy AI

## Recommended Posts

Hello. I am currently trying to program an enemy's movement across a platform. When it reaches the end of the platform, it supposed to turn around and walk to the other side of the platform. But when I test the game, the enemy only turns around the first time. When it starts walking and approaches the right side, it switches and starts moving to the left, but when it approaches the left side it just walks off.

Here's my code:

public float speed;
public bool movingLeft;
public bool grounded = false;
public Transform groundedEnd;

Rigidbody2D enemyBody;

// Use this for initialization
void Start () {
enemyBody = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () {
Raycasting ();
flipEnemy ();
enemyMovement ();
}

//check if enemy is grounded
void Raycasting(){
Debug.DrawLine (this.transform.position, groundedEnd.position, Color.green);
grounded = Physics2D.Linecast (this.transform.position, groundedEnd.position, 1 << LayerMask.NameToLayer("Ground"));
}

//flip enemy before falling off the edge
void flipEnemy(){
if(!grounded && movingLeft){
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
movingLeft = false;
}
if(!grounded && !movingLeft){
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
movingLeft = true;
}
}

//what direction the enemy is facing/walking
void enemyMovement(){
if (movingLeft) {
enemyBody.velocity = new Vector2 (-speed, enemyBody.velocity.y);
}
if (!movingLeft) {
enemyBody.velocity = new Vector2 (speed, enemyBody.velocity.y);
}
}
}

##### Share on other sites
Posted (edited)

Try changing the flipEnemy() function to this:

	void flipEnemy(){
if(!grounded && movingLeft){
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
movingLeft = false;
}else if(!grounded && !movingLeft){
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
movingLeft = true;
}
}

You need to test using "else if" because after checking if the player is grounded and movingLeft, you assign the boolean to false, and then you do the if statement AGAIN checking if movingLeft is false, which will always be true because you just assigned it to be false in the first if statement.

Other way of fixing this issue would be to put empty return statement inside of each if statement's end, this way you wouldn't have to use else if. But it depends on your preference.

Edited by EddieK

##### Share on other sites
14 minutes ago, EddieK said:

You need to test using "else if" because after checking if the player is grounded and movingLeft, you assign the boolean to false, and then you do the if statement AGAIN checking if movingLeft is false, which will always be true because you just assigned it to be false in the first if statement.

Such a simple fix. I feel pretty dumb now.

Thanks, man. I've been stuck on this for a bit. Maybe I just need to stop overthinking everything when I code.

## Create an account

Register a new account

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633686
• Total Posts
3013318
×