So I’m trying to make this basic 2d game where a bunch of bubble like prefabs float upwards and are destroyed on touch or if they hit the top. As time goes by the bubble objects are supposed to get faster. And I need clarity for the following:
- Should i use a separate script to control the movement of the objects and another just for spawning?
- then would i use one main script for controlling it all?
- how would i instantiate a clone of a prefab from an array and control its velocity
- would a couroutine help me out here? if so how could i control and destroy the clones being spawned?
- this is just a test i’ve tried a few different things before and i dont know which direction to take
Thank you for your time and help
using UnityEngine;
using System.Collections;
public class GM : MonoBehaviour {
public Transform pos;
public GameObject bub;
public float delay;
public GameObject[] bubbles;
public Rigidbody2D body2d;
public Vector2 vel;
// Use this for initialization
void Awake () {
delay = 1.5f;
}
void Start() {
StartCoroutine ("Spawn");
vel = new Vector2 (0, 5f);
Collider2D other = GetComponent<BoxCollider2D> ();
}
// Update is called once per frame
void Update () {
body2d = bub.GetComponent<Rigidbody2D> ();
body2d.velocity = vel;
}
IEnumerator Spawn() {
while (true) {
Instantiate (bub, transform.position, Quaternion.identity);
yield return new WaitForSeconds (1f);
}
}
void OnTriggerEnter2D (Collider2D other){
Destroy (bub.gameObject);
}
}