- Home /
Object Spawner acts really badly,
I've seen this tutorial (https://www.youtube.com/watch?v=E7gmylDS1C4&feature=emb_logo) But for me, the objects should be spawning up- not sideways, but no matter what, I just didn't figure it out how to modify the code. Here is generator.cs (which generates the objects):
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class generator: 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.width, Screen.height, Camera.main.transform.position.z));
StartCoroutine(asteroidWave());
}
private void spawnEnemy(){
GameObject a = Instantiate(asteroidPrefab) as GameObject;
a.transform.position = new Vector2(screenBounds.x * -2, Random.Range(-screenBounds.y, screenBounds.y));
}
IEnumerator asteroidWave(){
while(true){
yield return new WaitForSeconds(respawnTime);
spawnEnemy();
}
}
}
And here is deployer.cs, which just distributes the objects around the screen:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class deployer : MonoBehaviour { public float speed = 10.0f; private Rigidbody rb; private Vector2 screenBounds;
// Use this for initialization
void Start () {
rb = this.GetComponent<Rigidbody>();
rb.velocity = new Vector2(-speed, 0);
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
// Update is called once per frame
void Update () {
if(transform.position.x < screenBounds.x * 2){
Destroy(this.gameObject);
}
}
}
I have a game in portrait mode, that's why I need the objects to be spawned on the upper y-axis. But any modification just doesn't change anything.,