Created
September 5, 2023 20:08
-
-
Save kistaaa/80f0e64f28b2ea1fd5b35bf0566141a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class EnemySpawner : MonoBehaviour | |
{ | |
public GameObject enemyPrefab; | |
public Transform[] spawnPoints; | |
public float spawnInterval = 2.0f; | |
private float spawnTimer = 0.0f; | |
private void Update() | |
{ | |
spawnTimer += Time.deltaTime; | |
if (spawnTimer >= spawnInterval) | |
{ | |
SpawnEnemy(); | |
spawnTimer = 0.0f; | |
} | |
} | |
private void SpawnEnemy() | |
{ | |
if (spawnPoints.Length == 0 || enemyPrefab == null) | |
{ | |
Debug.LogError("Spawn point or enemy prefab not set!"); | |
return; | |
} | |
// Choose a random spawn point | |
Transform randomSpawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)]; | |
// Instantiate the enemy at the chosen spawn point | |
Instantiate(enemyPrefab, randomSpawnPoint.position, randomSpawnPoint.rotation); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment