UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"Write me a script for an enemy spawner"
using UnityEngine; public class EnemySpawner : MonoBehaviour { public GameObject enemyPrefab; public int enemyCount; public float spawnRadius; public float spawnInterval; private float spawnTimer; // Start is called before the first frame update void Start() { spawnTimer = spawnInterval; } // Update is called once per frame void Update() { spawnTimer -= Time.deltaTime; if (spawnTimer <= 0f) { SpawnEnemies(); spawnTimer = spawnInterval; } } void SpawnEnemies() { for (int i = 0; i < enemyCount; i++) { Vector3 spawnPosition = Random.insideUnitSphere * spawnRadius; spawnPosition.y = 0f; spawnPosition += transform.position; GameObject enemyInstance = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity); enemyInstance.transform.SetParent(transform); } } }