- Home /
How do I spawn objects randomly near an object then delete them?
Hi I am making a top down 3d endless runner type game and I am struggling to figure out how to make "x" number of obstacles spawn in front of the player in random positions on the x Axis then disappear after a small amount of time.
So, if the camera is looking straight down the x axis goes from left to right, so I assume that the player runs from left to right as well.
public int obstacles_$$anonymous$$ = 1;
public int obstacles_max = 4;
public float obstacles_range_$$anonymous$$ = 4;
public int obstacles_range_max = 10;
public Transform playerTransform;
public GameObject obstaclePrefab;
public float obstacleLifetime = 4f;
private void SpawnObstacles() {
int randomNumber = Random.Range(obstacles_$$anonymous$$, obstacles_max );
for(int i = 0; i < randomNumber; i++) {
Vector3 randomPos = new Vector3(playerTransform.x + Random.Range(obstacles_range_$$anonymous$$ , obstacles_range_max), playerTransform.y, playerTransform.z);
GameObject obstacle = Instantiate(obstaclePrefab, randomPos , Quaternion.identity) as GameObject;
Destroy(obstacle, obstacleLifetime );
}
}
The camera is top down and the player moves forward on the z axis with the ability to move left and right on the x axis.
Then you use this ins$$anonymous$$d
public int obstacles_$$anonymous$$ = 1;
public int obstacles_max = 4;
public float obstacles_range = 10;
public float obstacles_deviation_$$anonymous$$ = -3;
public float obstacles_deviation_max = 3;
public Transform playerTransform;
public GameObject obstaclePrefab;
public float obstacleLifetime = 4f;
private void SpawnObstacles() {
int randomNumber = Random.Range(obstacles_$$anonymous$$, obstacles_max );
for(int i = 0; i < randomNumber; i++) {
Vector3 randomPos = new Vector3(playerTransform.x + Random.Range(obstacles_deviation_$$anonymous$$, obstacles_deviation_max), playerTransform.y, playerTransform.z + obstacles_range);
GameObject obstacle = Instantiate(obstaclePrefab, randomPos , Quaternion.identity) as GameObject;
Destroy(obstacle, obstacleLifetime );
}
}
Answer by 334499p · Jun 30, 2017 at 02:17 AM
Are the objects being placed all at once or within a certain time frame? If all at once you can use a for statement:
for(int i = 0; i < amount; i++){
Instantiate(prefab, origin + Vector3.right * Random.Range(-1,1), Quaternion.identity);
}
If within a time frame for better performance:
StartCoroutine(createPrefabs());
IEnumerator createPrefabs(){
int i = 0;
While (i < amount){
I++;
Instantiate(prefab, origin + Vector3.right * Random.Range(-1,1), Quaternion.identity);
yield return new waitForEndOfFrame();
}
}
For the prefab, attach a script to it that destroys itself after waiting some time through an invoke():
void Start(){
Invoke("kill", seconds)
}
void kill{
Destroy(gameObject);
}
Your answer
Follow this Question
Related Questions
I want to teleport my player to a random(from these 4) spot on the x axis, but it doesn't work. 3 Answers
Instatiating a prefab in a random position 0 Answers
Spawn objects at random position ON a 2D object. 1 Answer
moving random objects to random positions 1 Answer
instantiate prefabs y+1 from last instintiated prefab 1 Answer