Jump to content
  • Advertisement

el1-0613

Member
  • Content Count

    2
  • Joined

  • Last visited

Community Reputation

1 Neutral

About el1-0613

  • Rank
    Newbie

Personal Information

  • Interests
    Art
    Audio
    Design
    Programming

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. el1-0613

    Need Help with Enemy AI

    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.
  2. 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
×

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!