Spawn Random Objects

Spawn Random Objects only by a horizontal line

Unity 2D Spawn Random Objects only by a horizontal line for desktop or mobile

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeteoriteMove : MonoBehaviour
{
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
rb.velocity = new Vector2(rb.velocity.x, -0.9f);
}

// Update is called once per frame
void Update()
{
transform.Rotate(Vector3.back * 20f * Time.deltaTime);
rb.velocity = new Vector2(rb.velocity.x, -0.9f);

}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "ResetTag")
{
Destroy(gameObject);
}
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnMeteorite : MonoBehaviour
{
public GameObject[] enemy;
[SerializeField] public float interval = 10f;
private float timer = 0;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if(timer >= interval)
{
int randomEnemy = Random.Range(0, enemy.Length);
var pos = new Vector3(Random.Range(-4, 4), Random.Range(8,8), Random.Range(10,10));
Instantiate(enemy[randomEnemy], pos, Quaternion.identity);
timer = 0;
}
}
}

Is this Articolo useful? 0 of 0 people say this Articolo is useful.
- / 5
Grazie per aver votato!