Basic game completed
This commit is contained in:
47
3D FPS/Assets/Scripts/Enemy/RandomSpawn.cs
Normal file
47
3D FPS/Assets/Scripts/Enemy/RandomSpawn.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user