I have gameobjects spawning at a random locations within the Y axis but having difficulty restricting the top and bottom values.
public GameObject gameObject;
public Vector3 spawnValues;
private float spawntime;
public float spawnstart;
public float spawnwait;
void Start()
{
spawntime = spawnstart;
}
void Update()
{
Vector3 spawnPos = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width * 1.3f, Random.Range(-spawnValues.y + 2, Screen.height - 2), 1));
if (spawntime <= 0)
{
spawntime = spawnwait;
Random rnd = new Random();
int num = Random.Range(1, 10);
for (var i=0; i <num; i++)
{
Vector3 pos = Camera.main.ScreenToWorldPoint (new Vector3(Screen.width * 1.3f, Random.Range(-spawnValues.y + 2, Screen.height - 2), 1));
Instantiate(gameObject, pos, Quaternion.identity);
}
}
else
{
spawntime -= Time.deltaTime;
}
}