-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spawner.cs
45 lines (40 loc) · 1.17 KB
/
Spawner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Spawner : NetworkBehaviour
{
public List<GameObject> spawnObjects;//chooses a random one
public float timeBetweenSpawns;
public Transform spawnLocation;
public float maxDistance;//if item goes beyond this a new one will spawn
public Transform spawned;
[ServerCallback]
void OnEnable()
{
StopAllCoroutines();
StartCoroutine(Run());
}
[Server]
void Spawn()
{
int i = Random.Range(0, spawnObjects.Count);
GameObject obj = Instantiate(spawnObjects[i], spawnLocation.position, spawnLocation.rotation);
NetworkServer.Spawn(obj);
spawned = obj.transform;
}
IEnumerator Run()
{
while(true)
{
if(spawned==null)
Spawn();
while(spawned!=null && Vector2.Distance(spawned.position, spawnLocation.position) < maxDistance)
{
yield return new WaitForSeconds(0.1f);
}
spawned = null;
yield return new WaitForSeconds(timeBetweenSpawns);
}
}
}