🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Need Help with Enemy AI

Started by
2 comments, last by el1-0613 5 years, 10 months ago

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);
		}
	}
}

 

Advertisement

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.

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.

This topic is closed to new replies.

Advertisement