UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"create an enemy spawner"
using UnityEngine; public class EnemySpawner : MonoBehaviour { public GameObject enemyPrefab; public float spawnInterval = 5f; public float spawnRadius = 10f; private float timeSinceLastSpawn; // Start is called before the first frame update void Start() { timeSinceLastSpawn = 0f; } // Update is called once per frame void Update() { timeSinceLastSpawn += Time.deltaTime; if (timeSinceLastSpawn >= spawnInterval) { SpawnEnemy(); timeSinceLastSpawn = 0f; } } void SpawnEnemy() { Vector3 spawnPosition = transform.position + Random.insideUnitSphere * spawnRadius; spawnPosition.y = transform.position.y; GameObject newEnemy = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity); } }