I want to spawn 3 or 4 different type of enemies at same location? i have written a script which only spawns two type of enemies at same location i am new to unity and c# please helo me here is my spawn script...
public GameObject enemy;
public GameObject enemy1;
public GameObject boss;
public float speed = 5f;
public bool ismoveright = true;
public float min = -4f;
public float max = 4f;
public float padding = 3.5f;
public float spawndelay = 3f;
// Start is called before the first frame update
void Start()
{
SpawnUntill();
}
//for adjusting enmies speed/movement
void Update()
{
if (ismoveright == true)
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (transform.position.x >= max)
{
ismoveright = false;
}
if (transform.position.x <= min)
{
ismoveright = true;
}
if (ismoveright == false)
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
ScreenSizer();
if (CheckDestroyedEnemy())
{
SpawnUntill1();
if (CheckDestroyedEnemy())
{
SpawnUntill2();
}
}
}
//for generating enemies one by one not together
void SpawnUntill()
{
Transform position = ChildPosition();
if (position)
{
GameObject Enemyprefab = Instantiate(enemy, position.transform.position, Quaternion.identity) as GameObject;
Enemyprefab.transform.parent = position;
}
if (ChildPosition())
{
Invoke("SpawnUntill", spawndelay);
}
}
void SpawnUntill1()
{
Transform position = ChildPosition();
if (position)
{
GameObject Enemyprefab = Instantiate(enemy1, position.transform.position, Quaternion.identity) as GameObject;
Enemyprefab.transform.parent = position;
}
if (ChildPosition())
{
Invoke("SpawnUntill1", spawndelay);
}
}
void SpawnUntill2()
{
Transform position = ChildPosition();
if (position)
{
GameObject Enemyprefab = Instantiate(boss, position.transform.position, Quaternion.identity) as GameObject;
Enemyprefab.transform.parent = position;
}
if (ChildPosition())
{
Invoke("SpawnUntill2", spawndelay);
}
}
//for moving enemies accordng to secreen size
public void ScreenSizer()
{
float distancetoscreen = transform.position.z - Camera.main.transform.position.z;
Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distancetoscreen));
Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distancetoscreen));
min = leftmost.x + padding;
max = rightmost.x - padding;
}
//for checking all enemies are destroyed or not so we can produce new enemies
bool CheckDestroyedEnemy()
{
foreach (Transform childobj in transform)
{
if (childobj.childCount > 0)
{
return false;
}
}
return true;
}
//for generating enemies on positon in which it appers first time
Transform ChildPosition()
{
foreach (Transform childobj in transform)
{
if (childobj.childCount == 0)
{
return childobj;
}
}
return null;
}
↧