Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

2D Invaders - Simple problem with managing a prefab group

Discussion in '2D' started by wamballa, Jun 19, 2018.

  1. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    Hi

    In my 2D Invaders game I have created an empty and attached this script to it. I have also created a prefab called Alien.

    This script loads a component with prefabs, then moves them side to side. When the leftmost or rightmost column gets to the edge of the screen, the alien group changes direction.

    When I hit play in unity the aliens do scroll from side to side nicely. However if I shoot the left column of aliens the group of aliens don't stop at the edge of the screen anymore. Instead they continue a bit further until a whole column of aliens are off screen, then turn around.

    What am I doing wrong?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class alienController : MonoBehaviour
    6. {
    7.     private Transform enemyHolder;
    8.     private float speed;
    9.  
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.         speed = 0.01f;
    14.         enemyHolder = GetComponent<Transform>();
    15.  
    16.         Debug.Log(enemyHolder);
    17.  
    18.         for (int rows = 0; rows < 3; rows++)
    19.         {
    20.             float newPosY = (float)rows + 0.05f;
    21.             for (int cols = 0; cols < 5; cols++)
    22.             {
    23.                 float newPosX = (float)cols + 0.1f - 2.5f;
    24.                 Vector3 pos = new Vector3(newPosX, newPosY, 0f);
    25.                 Instantiate(enemyPrefab, pos, transform.rotation, enemyHolder);
    26.             }
    27.         }
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         enemyHolder.position += Vector3.right * speed;
    34.  
    35.         foreach (Transform enemy in enemyHolder)
    36.         {
    37.             if (enemy.position.x > 2.8f || enemy.position.x < -2.8f)
    38.             {
    39.                 speed = -speed;
    40.             }
    41.         }
    42.  
    43.         if (enemyHolder.childCount == 0)
    44.         {
    45.             Debug.Log("WELL DONE");
    46.         }
    47.      }
    48. }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,851
    You're reversing the speed for each alien that is out of bounds. That's great if there are an odd number of out-of-bound aliens, but not so great if there are an even number.

    Also, this could conceivably get stuck. Suppose you've somehow gotten into a state where an alien is out of bounds, but the speed has already been reversed, so they are headed back into bounds. But then the code above runs; it sees the out-of-bounds alien, and inverts the speed, which (on the next frame) sends them more out of bounds. Then on the frame after that, you reverse it again, but by now they're too far to get back into bounds, so you just keep reversing the speed on every frame.

    This is one of those bugs that will never occur if everything goes just right, but sometimes things go wrong, and you should code defensively. So change lines 35-40 to this:

    Code (CSharp):
    1.         foreach (Transform enemy in enemyHolder) {
    2.             if ((enemy.position.x > 2.8f && speed > 0)
    3.              || (enemy.position.x < -2.8f && speed < 0) {
    4.                 speed = -speed;
    5.                 break;
    6.             }
    7.         }
    8.  
    This reverses the speed only once, and it reverses it only if the speed is in the wrong direction for the violated bound.
     
  3. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    This fixed it for me perfectly!!!
    Thank you very much for taking the time to help me out. Your solution is so clean and simple.
    You're a star!!!
     
    JoeStrout likes this.