[Solved] Weird behavior when making EF move down on direction change

My goal is to move the enemy formation down each time they “bounce” off the side, i.e. change direction. When testing, the formation does move down, but sometimes they jump down several steps instead of a single step. I am using a bool “drop” to flag if the formation changes direction, and an integer “bounces” to count the number of direction changes.

I am seeing weird things in the console messages as well. I am logging a message whenever I set drop as true and whenever bounces is incremented. The later is only executed in an if statement that checks if drop is true, so they should always be logged in pairs. However, in the console I only get the message for drop twice (followed by the bounces increment message), but after that I continue to get additional messages that bounce is being incremented. And sometimes it will increment bounces 2 or 3 times on direction change.

Here’s the code:

void Update() {
    bool drop = false;
    //Flip direction if enemy ship is near edge
    foreach (Transform child in transform) {

        if (child.transform.position.x > xmax - padding) {   //statements execute if enemy postion is inside padding on rights side
            if (direction == Vector3.right) {   //additonally check if we are currently moving right
                drop = true; Debug.Log("Drop");
            } 
            direction = Vector3.left;
            break;
        }
        else if (child.transform.position.x < xmin + padding) {
            if (direction == Vector3.left) { drop = true; Debug.Log("Drop"); }
            direction = Vector3.right;
            drop = true;
            break;
        }
    }
    //Move the enemy formation horizontally each frame
    transform.position += direction * speed * Time.deltaTime;
    //Move the enemy formation down after direction change
    if (drop) {
        Debug.Log(bounces);
        bounces++;
        transform.position = new Vector3 (transform.position.x, transform.position.y -(bounces * dropDistance), transform.position.z);
        drop = false;
    }

Here is what the console looks like. The “5” and “6” occurred simultaneously, otherwise all of the drop increments happened individually when the formation changed direction. I am really puzzled as to why the “Drop” message stops after the 2nd time. This happens consistently.

Capture

Well after posting this, I decided to comment out all of my drop functionality and move on. Then I got an error and realized that I had forgotten to delete a “bool = true;” statement that was leftover from before I moved that functionality to its own if statement.

I think this has corrected the overall functionality, however I am still only getting 2 drop messages! So if anyone knows why this is happening, please let me know.

Aaaaand, I just realized the drop messages were being collapsed. Derp. Okay, well this is solved. If anyone is interested in adding this to their project, here is the corrected code:

void Update() {
    bool drop = false;
    //Flip direction if enemy ship is near edge
    foreach (Transform child in transform) {

        if (child.transform.position.x > xmax - padding) {   //statements execute if enemy postion is inside padding on rights side
            if (direction == Vector3.right) {   //additonally check if we are currently moving right
                drop = true; Debug.Log("Drop");
            } 
            direction = Vector3.left;
            break;
        }
        else if (child.transform.position.x < xmin + padding) {
            if (direction == Vector3.left) { drop = true; Debug.Log("Drop"); }
            direction = Vector3.right;
            break;
        }
    }
    //Move the enemy formation horizontally each frame
    transform.position += direction * speed * Time.deltaTime;
    //Move the enemy formation down after direction change
    if (drop) {
        Debug.Log(bounces);
        bounces++;
        transform.position = new Vector3 (transform.position.x, transform.position.y - dropDistance, transform.position.z);
        
    }

edit: fixed the reset of transform to move down. Was multiplying bounces * drop distance. Only the drop distance is needed.

Furthermore, you will probably want to reset the Y position when you spawn a new group by changing this part of the update function

    if (AllMembersDead()) {
        Debug.Log("End Y Position");
        Debug.Log(transform.position.y);
        transform.position = new Vector3(transform.position.x, transform.position.y + (bounces * dropDistance), transform.position.z);
        bounces = 0;
        Debug.Log("resetting Y position and bounces");
        Debug.Log(transform.position);
        Debug.Log(bounces);
        SpawnUntilFull();
        speed *= 1.15f;
    }

Privacy & Terms