- Home /
Need Help Spawning Cubes Constantly In Front Of Player
So in my game there is a first person controller that falls inside 4 walls that lock it in(as if you were falling inside an empty skyscraper) and i want there to be cubes that generate at random x and z positions but staying inside the walls, before the player reaches them(so generating at a specific y aswell) i would like this to be constant and for the cubes to be destroyed after the controller has passed them.
i have kind of tried this already so but all i managed was a bunch of cubes generating every second in the same position, here is my code:
using UnityEngine; using System.Collections;
public class Generator : MonoBehaviour {
public GameObject cube;
public int numberOfCubes;
public int min, max;
public float Time = 3f;
// Use this for initialization
void Start () {
InvokeRepeating ("PlaceCubes", Time, Time);
}
// Update is called once per frame
void PlaceCubes () {
for (int i = 0; i < numberOfCubes; i++) {
Instantiate (cube,GeneratedPosition(),Quaternion.identity);
}
}
Vector3 GeneratedPosition()
{
int x,y,z;
x = Random.Range(min,max);
y = Random.Range(min,max);
z = Random.Range(min,max);
return new Vector3(x,y,z);
}
}
i would also like to mention that i am a bit of a noob!
so if you could help me and tell me what i need to , be it creating a new script that would be great thanks!
30 -30 but the only difference it makes is the size of the ware where the cubes are allowed to spawn
Answer by LT23Live · Jan 12, 2015 at 08:07 AM
I would like a little bit more details. How quickly do you want the cubes to spawn.
A small tip The cubes you want should be prefabs with a destroyer script set to them. The script should have OnTriggerEnter used. Then destroy the gameobject.
Ok i would like to cubes to spawn every 5 seconds say, similar to a game like temple run, and i would like the cubes to be destroyed once the first person controller has passed them. I presume the rest you understand, thank you