Enemy Movement

Hi, I’ve written a code that should behave like Space Invader’s enemy I.A.
Enemies move right, they reach the right boundary, go down, then move left, they reach the left boundary… and so on.
Problem’s when they reach the right boundary: script doesn’t work correctly because they can’t go left, so they fall down continuously. Can someone help me?

Code

public class enemyController : MonoBehaviour {

    private Rigidbody2D rb2d;

    private GameObject rightEnemy;
    private GameObject leftEnemy;

    public float speed;
    private float rightEdge = 15f;
    private float leftEdge = -15f;

    private bool s;

    void Start () {
        rightEnemy = GameObject.Find ("Enemy1 (10)");
        leftEnemy = GameObject.Find ("Enemy1");
        rb2d = GetComponent<Rigidbody2D> ();
    }
   

    void Update () {
        s = true;
        if (rightEnemy.transform.position.x < rightEdge && leftEnemy.transform.position.x > leftEdge && s == true)
            transform.Translate (2f * Time.deltaTime, 0, 0);
        else if (rightEnemy.transform.position.x >= rightEdge) {
            transform.Translate (-0.5f, -0.5f, 0);
            s = false;
        }
        if (rightEnemy.transform.position.x < rightEdge && leftEnemy.transform.position.x > leftEdge && s == false)
            transform.Translate (-2f * Time.deltaTime, 0, 0);
        else if (leftEnemy.transform.position.x <= leftEdge) {
            transform.Translate (0.5f, -0.5f, 0);
            s = true;
        }
    }
}

Enemy(10) is at th extreme right, Enemy 1 the first.

Anyone could help me, please? Perhaps I’m writing in the wrong section and I can’t ask about coding?

Couldn’t you do it with animation?

It’s about programming, I don’t mind animation. I wrote here to find a solution to the enemy movement’s problem. Thanks anyway, I should simply find a solution myself :slight_smile: