Skip to content

Instantly share code, notes, and snippets.

@equalent
Last active April 22, 2020 21:42
Show Gist options
  • Save equalent/9eeb582e2dde057f92032619eab5ef97 to your computer and use it in GitHub Desktop.
Save equalent/9eeb582e2dde057f92032619eab5ef97 to your computer and use it in GitHub Desktop.
NPC Spawner Component for Unity

NPC Spawner

It spawns NPC and draws two Gizmos: red for spawn area and blue for player spawn distance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NpcSpawner : MonoBehaviour {
[Header("NPC Spawner Configuration")]
[Tooltip("A refab list of NPCs that can be spawned. Spawner will destroy itself is this list is empty.")]
public GameObject[] Spawnables;
[Tooltip("A spawn interval in seconds. (5 sec by default)")]
public float interval = 5f;
[Tooltip("A spawn radius (a distance in which all NPCs will be spawned")]
public float radius = 5f;
[Tooltip("Maximum number of spawned NPCs (0 means infinity)")]
public int maxNpcs = 0;
[Tooltip("Distance to player when spawning begins")]
public float playerDistance = 20f;
[Tooltip("Game player object for distance reference")]
public GameObject player;
private int currentSpawned = 0;
private bool canSpawn = true;
// Use this for initialization
void Start () {
if (Spawnables.Length == 0)
{
Destroy(this);
return;
}
StartCoroutine("spawnerJob");
}
// Update is called once per frame
void Update () {
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(this.transform.position, radius);
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(this.transform.position, playerDistance);
}
public IEnumerator spawnerJob()
{
while (canSpawn)
{
if (Vector3.Distance(player.transform.position, this.transform.position) <= playerDistance)
{
yield return new WaitForSeconds(interval);
GameObject currentNpc = Spawnables[Random.Range(0, Spawnables.Length - 1)];
Instantiate(currentNpc, this.transform.position + Random.insideUnitSphere * radius, currentNpc.transform.rotation).name = "Spawned NPC";
currentSpawned++;
canSpawn = (currentSpawned <= maxNpcs) || (maxNpcs == 0);
}
}
}
}
@RealDrWhite
Copy link

I put it in and pressed play and now my Unity is crashing.
Rip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment