Skip to content

Instantly share code, notes, and snippets.

@kistaaa
Created September 5, 2023 20:08
Show Gist options
  • Save kistaaa/80f0e64f28b2ea1fd5b35bf0566141a8 to your computer and use it in GitHub Desktop.
Save kistaaa/80f0e64f28b2ea1fd5b35bf0566141a8 to your computer and use it in GitHub Desktop.
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