48 lines
1.1 KiB
C#
48 lines
1.1 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public class RandomSpawn : MonoBehaviour
|
||
|
|
{
|
||
|
|
public GameObject enemyPrefab;
|
||
|
|
public float spawnInterval = 5f;
|
||
|
|
|
||
|
|
public Vector3 spawnAreaCenter;
|
||
|
|
public Vector3 spawnAreaSize;
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
StartCoroutine(SpawnEnemies());
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
IEnumerator SpawnEnemies()
|
||
|
|
{
|
||
|
|
while (true)
|
||
|
|
{
|
||
|
|
yield return new WaitForSeconds(spawnInterval);
|
||
|
|
SpawnEnemy();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void SpawnEnemy()
|
||
|
|
{
|
||
|
|
Vector3 randomPosition = GetRandomPosition();
|
||
|
|
Instantiate(enemyPrefab, randomPosition, Quaternion.identity);
|
||
|
|
}
|
||
|
|
|
||
|
|
Vector3 GetRandomPosition()
|
||
|
|
{
|
||
|
|
float x = Random.Range(spawnAreaCenter.x - spawnAreaSize.x / 2, spawnAreaCenter.x + spawnAreaSize.x / 2);
|
||
|
|
float y = spawnAreaCenter.y;
|
||
|
|
float z = Random.Range(spawnAreaCenter.z - spawnAreaSize.z / 2, spawnAreaCenter.z + spawnAreaSize.z / 2);
|
||
|
|
|
||
|
|
return new Vector3(x, y, z);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnDrawGizmosSelected()
|
||
|
|
{
|
||
|
|
Gizmos.color = Color.green;
|
||
|
|
Gizmos.DrawWireCube(spawnAreaCenter, spawnAreaSize);
|
||
|
|
}
|
||
|
|
}
|