Search Unity

Feedback on my code

Discussion in 'Getting Started' started by PajamaJohn, Feb 25, 2019.

  1. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    Hello,

    I been playing around with my space shooter and been trying to learn how to setup enemy spawn positions like they do in Hawk: Freedom Squadron. I added a YouTube link, for more details of what I’m talking about. At the moment I have it set to random positions along the x-axis. Here is my code, any feedback on improving would be appreciated.



    Code (CSharp):
    1.     public GameObject enemyOne;
    2.     public GameObject enemyTwo;
    3.     public GameObject enemyThree;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.  
    8.         StartCoroutine(SpawnEnemies());  
    9.     }
    10.  
    11.     IEnumerator SpawnEnemies()
    12.     {
    13.         yield return new WaitForSeconds(5);
    14.         EnemyOne();
    15.         yield return new WaitForSeconds(1);
    16.         EnemyTwo();
    17.         yield return new WaitForSeconds(.5f);
    18.         EnemyOne();
    19.         yield return new WaitForSeconds(2);
    20.         EnemyOne();
    21.         EnemyOne();
    22.         yield return new WaitForSeconds(1);
    23.         EnemyOne();
    24.         yield return new WaitForSeconds(1);
    25.         EnemyThree();
    26.         yield return new WaitForSeconds(2);
    27.         EnemyOne();
    28.         EnemyOne();
    29.         yield return new WaitForSeconds(1);
    30.         EnemyThree();
    31.         yield return new WaitForSeconds(1);
    32.         EnemyOne();
    33.         EnemyOne();
    34.         yield return new WaitForSeconds(.5f);
    35.         EnemyTwo();
    36.     }
    37.  
    38.     void EnemyOne()
    39.     {
    40.         Vector3 spawnPosition = new Vector3(Random.Range(-6, 6), 0, 16);
    41.         Quaternion spawnRotation = Quaternion.identity;    
    42.         Instantiate(enemyOne, spawnPosition, spawnRotation);
    43.     }
    44.  
    45.     void EnemyTwo()
    46.     {
    47.         Vector3 spawnPosition = new Vector3(Random.Range(-6, 6), 0, 16);
    48.         Quaternion spawnRotation = Quaternion.identity;    
    49.         Instantiate(enemyTwo, spawnPosition, spawnRotation);  
    50.     }
    51.  
    52.     void EnemyThree()
    53.     {
    54.         Vector3 spawnPosition = new Vector3(Random.Range(-6, 6), 0, 16);
    55.         Quaternion spawnRotation = Quaternion.identity;    
    56.         Instantiate(enemythree, spawnPosition, spawnRotation);  
    57.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your coroutine seems a little silly. I'd just put it in a loop. Also, duplicating your code in your spawn methods isn't the right way to go. Just make a single spawn method and pass in the prefab you want to spawn.
     
  3. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    Hello again,
    First, I would like to thank you. Second, I feel like a dummy for duplicating my code in spawn methods. I learned how to consistency spawn enemies with a for loop, but I was trying to figure out how to have more control over when they spawn. And I’m assuming if I wanted more control over their spawn position, I can just pass an argument to spawn method, right? Another question, would it be good practice if I put my enemy prefabs in an array and then pass the index to spawn method? I fixed up my code a bit, not exactly the way I wanted it but I just wanted to make sure I'm on the right track.


    Code (CSharp):
    1.     public GameObject enemyOne;
    2.     public GameObject enemyTwo;
    3.     public GameObject enemyThree;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.  
    8.         StartCoroutine(SpawnEnemies());  
    9.     }
    10.  
    11.     IEnumerator SpawnEnemies()
    12.     {
    13.         yield return new WaitForSeconds(5);
    14.         for (int i = 0; i < 10; i++)
    15.         {
    16.             spawn(enemyOne);
    17.             yield return new WaitForSeconds(1);
    18.             spawn(enemyTwo);
    19.             yield return new WaitForSeconds(1);
    20.  
    21.             if (i ==3)
    22.             {
    23.                 yield return new WaitForSeconds(.5f);
    24.                 spawn(enemyThree);
    25.             }
    26.         }
    27.     }
    28.  
    29.     void spawn(GameObject enemy)
    30.     {
    31.         Vector3 spawnPosition = new Vector3(Random.Range(-6, 6), 0, 16);
    32.         Quaternion spawnRotation = Quaternion.identity;    
    33.         Instantiate(enemy, spawnPosition, spawnRotation);  
    34.     }
    35. }
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Looks pretty good to me.

    Yes. You can also have an array or list of spawn positions, and grab them randomly or in order, or pass in the index of the spawn position you want to your spawn method.

    I generally prefer to create an enum, and then pass that to the method rather than the index itself (by default an enum is numbered starting at 0, so can work nicely in place of using index numbers). But there's lots of different ways to do it, including just passing the index.

    Code (csharp):
    1. public GameObject[] EnemyPrefabs;
    2.  
    3. public enum EnemyName { WeakEnemy, RedEnemy, GoodEnemy };
    4.  
    5. public SimpleSpawnWithEnumExample()
    6. {
    7.     GameObject newEnemyObject = Spawn(EnemyName.RedEnemy);  //Spawn the prefab at index 1
    8. }
    9.  
    10. public GameObject Spawn (EnemyName enemy)
    11. {
    12.     GameObject returnObject = Instantiate(EnemyPrefabs[(int)enemy])
    13.     return returnObject;
    14. }
     
  5. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    Once again thank you so much! I got so much to learn and there so many ways to do something just make my head explode! And I’m going to look more into enum. :p
     
    Ryiah and Joe-Censored like this.
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I like threads like this because it shows how easy it is to improve. You just have to be able to think critically about your work and be open to evaluation and feedback.

    Keep at it!
     
    PajamaJohn, Ryiah and Joe-Censored like this.
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Don't get too worried about doing things the right or wrong way though. It is more important that your game works as intended than if its code passes a forum code review ;)
     
    PajamaJohn likes this.
  8. PajamaJohn

    PajamaJohn

    Joined:
    Dec 2, 2017
    Posts:
    20
    Hello again!

    I apologize for the delay. I believe few of my issues when learning is overthinking or fear of getting into bad habits, but I’m working on it. :)