- Home /
 
               Question by 
               ihmoud622 · Jul 26, 2020 at 09:16 PM · 
                c#enemyspace shooterrandomspawningspawning-enemies  
              
 
              I need Help spawning game objects (enemys) from the top of my screen,How can i spawn my Game Objects on top of the sreec randomly
Hello, I'm programming a Spaceshooter game and I'm basically done but the only problem is that my gameobject (Asteroids) are not spawning randomly , they are spawning just in one line and i want them how i already said spawning randomly. This is the code im using thank you for helping<3 (Using C#).
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class deployAsteroids : MonoBehaviour
 {
     public GameObject asteroidPrefab;
     public float respawnTime = 1.0f;
     private Vector2 screenBounds;
 
     // Use this for initialization
     void Start()
     {
         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.height, Screen.width, Camera.main.transform.position.z));
         StartCoroutine(asteroidWave());
     }
     private void spawnEnemy()
     {
         GameObject a = Instantiate(asteroidPrefab) as GameObject;
         a.transform.position = new Vector2(screenBounds.y * -2, Random.Range(-screenBounds.x, screenBounds.x));
     }
     IEnumerator asteroidWave()
     {
         while (true)
         {
             yield return new WaitForSeconds(respawnTime);
             spawnEnemy();
         }
     }
 }
     
 ,Hello, I'm programming a Spaceshooter game and I'm basically done but the only problem is that my gameobject (Asteroids) are not spawning randomly , they are spawning just in one line and i want them how i already said spawning randomly. This is the code im using thank you for helping<3 (Using C#).
 
 
    `using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class deployAsteroids : MonoBehaviour
 {
     public GameObject asteroidPrefab;
     public float respawnTime = 1.0f;
     private Vector2 screenBounds;
 
     // Use this for initialization
     void Start()
     {
         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.height, Screen.width, Camera.main.transform.position.z));
         StartCoroutine(asteroidWave());
     }
     private void spawnEnemy()
     {
         GameObject a = Instantiate(asteroidPrefab) as GameObject;
         a.transform.position = new Vector2(screenBounds.y * -2, Random.Range(-screenBounds.x, screenBounds.x));
     }
     IEnumerator asteroidWave()
     {
         while (true)
         {
             yield return new WaitForSeconds(respawnTime);
             spawnEnemy();
         }
     }
 }`
 
 
               Comment
              
 
               
              Answer by Llama_w_2Ls · Jul 28, 2020 at 11:47 AM
This line here a.transform.position = new Vector2(screenBounds.y * -2, Random.Range(-screenBounds.x, screenBounds.x)); is the wrong way round. The only position that youre making random is the y position, not the x. If you want them to spawn at random x positions, then you would reorder the line by writing new Vector2(Random.Range(-screenBounds.x, screenBounds.x) , screenBounds.y * -2, );
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                