Hi guys hopefully someone can help me
im trying to make a game where balls come from the right of the screen moving left. i want them to spawn off screen and start moving left. (thats not the issue.) I need to make it so that the balls can spawn vertically up or down in certain locations, at random (IMPORTANT: It needs to be between (MAX) 3.4 and (MIN) -6.4 on the Y axis… so yeah, anywhere between those locations it can spawn. How would i start a C# script like this (so, to spawn AND be spawned in between those locations EVERY 2 seconds) :)))) hopefully someone can enlighten me
Hi,…
I can’t fully understand your view. But in case if you want to instantiate a gameobject(in your view ball), in some random locations try this.
Javascript code
var create: Vector3 = Vector3(Random.Range(-8, 8.0), Random.Range(5.5,2.5), -.7); //Vector3(x , y, z) values location.
Instantiate(prefab, create, Quaternion.identity);
yield WaitForSeconds(2);
NOTE : prefab is your gameobject or ball, pass this code in any loop for continuous spawning of objects.
hope this may help u .
Here you go, sir. Hope it helps!
public class Spawner : MonoBehaviour
{
// Prefab to spawn
public GameObject spawnee;
// Use this for initialization
void Start()
{
StartCoroutine(SpawnTimer());
}
IEnumerator SpawnTimer()
{
while (true)
{
Spawn();
yield return new WaitForSeconds(2f);
}
}
void Spawn()
{
float rightScreenBound = 999; // set value for this
float randomY = Random.Range(-6.4f, 3.4f);
Instantiate(spawnee, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
}
}