Skip to content

Instantly share code, notes, and snippets.

@AmmarTee
Last active December 19, 2022 07:38
Show Gist options
  • Save AmmarTee/ff1d561c45cb6508db95e0d30b2e29bf to your computer and use it in GitHub Desktop.
Save AmmarTee/ff1d561c45cb6508db95e0d30b2e29bf to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleSpawner : MonoBehaviour
{
public GameObject red;
public GameObject blue;
[Range(1,10)]
public int level;
[SerializeField]
public float startOffSet;
[SerializeField]
public float incrementalDistanceZ,incrementalDistanceX;
[SerializeField]
private float distance;
[SerializeField]
private float density;
[SerializeField]
private Vector3 spawnLocation;
void Start()
{
StartCoroutine(spawnObstacle());
}
IEnumerator spawnObstacle()
{
spawnLocation = new Vector3(0,0.2f,startOffSet);
for(int i = 0;i<distance;i++)
{
startOffSet +=incrementalDistanceZ;
spawnLocation = new Vector3(0,2f,startOffSet);
float xlocation = -1.3f;
for(int j = 0;j<density;j++)
{
xlocation+=incrementalDistanceX;
if(xlocation>1.3f)
{
break;
}
spawnLocation = new Vector3(xlocation,2f,startOffSet);
int rnd = Random.Range(1,level);
if(rnd <= 1)
{
Instantiate(red,spawnLocation,Quaternion.identity,this.transform);
}
else if(rnd > 1)
{
Instantiate(blue,spawnLocation,Quaternion.identity,this.transform);
}
yield return new WaitForSeconds(0.01f);
}
}
StartCoroutine(spawnObstacle());
}
// Update is called once per frame
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment