Jump to content
  • Advertisement
el1-0613

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!