I am doing a infinite ball rolling game and I have a script to instantiate obstacles for the player to avoid. Here it is:
using UnityEngine;
using System.Collections.Generic;
public class ColliderManager : MonoBehaviour {
public Transform prefab;
public int numberOfObjects;
public float recycleOffset;
public Vector3 minSize, maxSize, minGap, maxGap;
public float minx, maxx;
public Material[] materials;
public PhysicMaterial[] physicMaterials;
//public Booster booster;
private Vector3 nextPosition;
private Queue<Transform> objectQueue;
void Start () {
//GameEventManager.GameStart += GameStart;
//GameEventManager.GameOver += GameOver;
//GameStart();
//objectQueue = new Queue<Transform>(numberOfObjects);
//for(int i = 0; i < numberOfObjects; i++){
// objectQueue.Enqueue((Transform)Instantiate(prefab, new Vector3(0f, 0f, 0f), Quaternion.identity));
//}
//enabled = true;
objectQueue = new Queue<Transform>(numberOfObjects);
for(int i = 0; i < numberOfObjects; i++){
objectQueue.Enqueue((Transform)Instantiate(prefab));
}
nextPosition = transform.localPosition;
for(int i = 0; i < numberOfObjects; i++){
Recycle();
}
}
void Update () {
if(objectQueue.Peek().localPosition.z + recycleOffset < Runner.distanceTraveled){
Recycle();
}
}
private void Recycle () {
Vector3 scale = new Vector3(
Random.Range(minSize.x, maxSize.x),
Random.Range(minSize.y, maxSize.y),
Random.Range(minSize.z, maxSize.z));
Vector3 position = nextPosition;
position.z += scale.z * 0.5f;
position.y += scale.y * 0.5f;
//booster.SpawnIfAvailable(position);
Transform o = objectQueue.Dequeue();
o.localScale = scale;
o.localPosition = position;
int materialIndex = Random.Range(0, materials.Length);
o.renderer.material = materials[materialIndex];
o.collider.material = physicMaterials[materialIndex];
objectQueue.Enqueue(o);
nextPosition += new Vector3(
Random.Range(minGap.x, maxGap.x),
Random.Range(minGap.y, maxGap.y),
Random.Range(minGap.z, maxGap.z) + scale.z);
if(nextPosition.x < minx){
nextPosition.x = minx - minGap.x;
}
else if(nextPosition.x > maxx){
nextPosition.x = maxx - maxGap.x;
}
}
}
The problem is that sometimes when you start the game you run straight into on of the collider obstacles. So my question is if there is a way for me to delay the script for just a few seconds so the player can get going (it’s not exactly fun blowing up on start). Thanks in advance for any help.