Open-sourced some code
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ProximitySpawner : MonoBehaviour {
|
||||
//public access members
|
||||
public GameObject prefab;
|
||||
public string targetTag;
|
||||
public float minDistance = 4f;
|
||||
public float maxDistance = 10f;
|
||||
public bool destroyIfTooFar = false;
|
||||
|
||||
//private members
|
||||
Transform targetTransform;
|
||||
GameObject spawnedObject;
|
||||
|
||||
void Start() {
|
||||
targetTransform = GameObject.FindWithTag(targetTag).transform;
|
||||
}
|
||||
|
||||
void Update() {
|
||||
//get the distance
|
||||
float distance = Vector3.Distance(transform.position, targetTransform.position);
|
||||
|
||||
//destroy if the player is too far away
|
||||
if (destroyIfTooFar && distance > maxDistance) {
|
||||
Destroy(spawnedObject);
|
||||
return; //skip out on the next check
|
||||
}
|
||||
|
||||
//check the distance, check the spawned object
|
||||
if (distance > minDistance && distance < maxDistance && spawnedObject == null) {
|
||||
spawnedObject = Instantiate(prefab);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TimedSpawner : MonoBehaviour {
|
||||
//inspector access elements
|
||||
public GameObject prefab;
|
||||
public float spawnDelay = 10f;
|
||||
public int maxSpawns = 3;
|
||||
|
||||
//private members
|
||||
float lastSpawnTime = float.NegativeInfinity;
|
||||
List<GameObject> spawnList = new List<GameObject>();
|
||||
|
||||
//TODO: maybe all spawners should shut off (and monsters become inactive) when the player is too far away.
|
||||
void Update() {
|
||||
//prune the list
|
||||
spawnList.RemoveAll(spawn => spawn == null);
|
||||
|
||||
//spawn if enough time has passed
|
||||
if (Time.time - lastSpawnTime > spawnDelay && spawnList.Count < maxSpawns) {
|
||||
lastSpawnTime = Time.time;
|
||||
|
||||
spawnList.Add(Instantiate(prefab, transform.position, Quaternion.identity, transform));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user