Advertisement
johnnygoodguy2000

EnemySpawner.cs

Jun 15th, 2024 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class EnemySpawner : MonoBehaviour
  6. {
  7.     [SerializeField] private GameObject enemyPrefab;
  8.     [SerializeField] private float minimumSpawnTime;
  9.     [SerializeField] private float maximumSpawnTime;
  10.  
  11.     private float timeUntilSpawn;
  12.  
  13.  
  14.     // Start is called before the first frame update
  15.     void Awake()
  16.     {
  17.         SetTimeUntilSpawn();
  18.        
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.         timeUntilSpawn -= Time.deltaTime;
  25.  
  26.          // Check if player is within detection radius
  27.            
  28.         if (timeUntilSpawn <= 0)
  29.         {
  30.            Instantiate(enemyPrefab, transform.position, Quaternion.identity);
  31.            SetTimeUntilSpawn();
  32.         }
  33.        
  34.     }
  35.  
  36.     private void SetTimeUntilSpawn()
  37.     {
  38.         timeUntilSpawn = Random.Range(minimumSpawnTime, maximumSpawnTime);
  39.     }
  40. }
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement