Explorar o código

spawn enemies every 2 seconds at spawners during night

DasGewehr %!s(int64=9) %!d(string=hai) anos
pai
achega
6b579e0aac

+ 5 - 2
game/Assets/Prefabs/SolarBotSpawner.prefab

@@ -123,8 +123,8 @@ Transform:
   m_PrefabParentObject: {fileID: 0}
   m_PrefabInternal: {fileID: 100100000}
   m_GameObject: {fileID: 1400659369070184}
-  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: 0, y: 2, z: 0}
+  m_LocalRotation: {x: 0, y: -0.6040429, z: 0, w: 0.79695183}
+  m_LocalPosition: {x: -35.2, y: 2, z: -0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children:
   - {fileID: 4257224600887088}
@@ -466,3 +466,6 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: d546e31cd9a499a44a89424a0b8259e6, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
+  EnemyPrefab: {fileID: 1196849215294664, guid: 6a04fac6f7a8dea419c1a2e19e3bb8c8,
+    type: 2}
+  cooldownForSpawning: 2

+ 28 - 5
game/Assets/Scripts/Enemy/EnemySpawner.cs

@@ -2,15 +2,38 @@
 using System.Collections.Generic;
 using UnityEngine;
 
-public class EnemySpawner : MonoBehaviour {
+public class EnemySpawner : MonoBehaviour 
+{
+    [SerializeField]
+    private GameObject EnemyPrefab;
+    [SerializeField]
+    private float cooldownForSpawning = 2.0f;
+    private float timer;
+    private DayTimeManager dayTimeManager;
 
 	// Use this for initialization
-	void Start () {
-		
+	void Start () 
+    {
+        dayTimeManager = FindObjectOfType<DayTimeManager>();
 	}
 	
 	// Update is called once per frame
-	void Update () {
-		
+	void Update () 
+    {
+        if (dayTimeManager.dayTimeLabel == DayTimeLabel.Night)
+        {
+            timer += Time.deltaTime;
+            if (timer > cooldownForSpawning)
+            {
+                SpawnEnemy();
+                timer = 0.0f;
+            }
+        }
 	}
+
+    private void SpawnEnemy()
+    {
+        GameObject enemy  = GameObject.Instantiate(EnemyPrefab);
+        enemy.transform.position = this.transform.position;
+    }
 }