Page 1 of 1
[SOLVED]Random Enemy Spawn
Posted: Tue Oct 13, 2020 8:46 am
by mschoenhals
Hi,
I would like to have random enemy spawns as a player enters a particular scene (one that the player previously visited). I've created a script to attach to enemies that will detect the player level and set the spawn level accordingly.
How can I best implement random spawns? If you could point me to the simplest method first, that would help with implementation.
Also, is it possible to have the random spawns tied to a procedural quest system from a particular quest giver? I have the Pixel Crushers Dialogue System included in my game.
Thanks very much in advance.
Re: Random Enemy Spawn
Posted: Tue Oct 13, 2020 9:48 am
by Tony Li
Hi
mschoenhals wrote: ↑Tue Oct 13, 2020 8:46 amHow can I best implement random spawns?
You could set up Spawner components in the scene, one for each level range. Assign random enemy prefabs for that level range. Set Min and Max to 1, and UNtick Auto Start.
In your script, detect the player's level and call the corresponding Spawner's StartSpawning() method.
mschoenhals wrote: ↑Tue Oct 13, 2020 8:46 amAlso, is it possible to have the random spawns tied to a procedural quest system from a particular quest giver? I have the Pixel Crushers Dialogue System included in my game.
Yes. Add a Quest Entity component to the prefab. When it's spawned in, it will be added to whatever quest domains it ends up in. If a quest-generating NPC watches that quest domain, it might make a quest about the spawn.
Re: Random Enemy Spawn
Posted: Wed Oct 14, 2020 9:47 am
by mschoenhals
Hi Tony,
Thank you for the reply. Please excuse the crude coding as I'm still very much learning the language (any suggestions would be greatly appreciated). This is what I came up with:
Code: Select all
using UnityEngine;
using RPG.Stats;
using PixelCrushers.QuestMachine;
namespace RPG.Combat
{
public class SceneSpawnManager : MonoBehaviour
{
[SerializeField] GameObject spawnFirst = null;
[SerializeField] GameObject spawnSecond = null;
[SerializeField] GameObject spawnThird = null;
[SerializeField] GameObject spawnFourth = null;
[SerializeField] GameObject spawnFifth = null;
[SerializeField] GameObject spawnSixth = null;
GameObject player;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindWithTag("Player");
int playerLevel = player.GetComponent<BaseStats>().GetLevel();
SpawnEnemies(playerLevel);
}
private void SpawnEnemies(int playerLevel)
{
if (playerLevel > 2 && playerLevel < 6)
{
Debug.Log("Activate first spawner");
spawnFirst.SetActive(true);
spawnFirst.GetComponent<Spawner>().StartSpawning();
}
if (playerLevel >= 6 && playerLevel < 9)
{
Debug.Log("Activate second spawner");
spawnSecond.SetActive(true);
spawnSecond.GetComponent<Spawner>().StartSpawning();
}
if (playerLevel >= 9 && playerLevel < 11)
{
Debug.Log("Activate third spawner");
spawnThird.SetActive(true);
spawnThird.GetComponent<Spawner>().StartSpawning();
}
if (playerLevel >= 11 && playerLevel < 14)
{
Debug.Log("Activate fourth spawner");
spawnFourth.SetActive(true);
spawnFourth.GetComponent<Spawner>().StartSpawning();
}
if (playerLevel >= 14 && playerLevel < 17)
{
Debug.Log("Activate fifth spawner");
spawnFifth.SetActive(true);
spawnFifth.GetComponent<Spawner>().StartSpawning();
}
if (playerLevel > 16)
{
Debug.Log("Activate sixth spawner");
spawnSixth.SetActive(true);
spawnSixth.GetComponent<Spawner>().StartSpawning();
}
}
}
}
I have a few questions: On the spawner, is Weight used for randomness? ie: if I set it to .5, will there be a 50% chance of spawning this prefab? When would you use an entity as apposed to a prefab? Can a prefab be a disabled prefab in the scene hierarchy and then enabled when spawning? Also, what is a spawned entity as apposed to a spawned prefab?
Thanks very much in advance.
Re: Random Enemy Spawn
Posted: Wed Oct 14, 2020 11:48 am
by mschoenhals
Also,
When using the spawner script, only 2 of 3 spawns instantiate. Here are my parameters for spawning:
https://www.dropbox.com/s/hkkk7ipo29l7t ... e.jpg?dl=0
Any ideas on why only 2 show up?
Re: Random Enemy Spawn
Posted: Wed Oct 14, 2020 12:15 pm
by Tony Li
mschoenhals wrote: ↑Wed Oct 14, 2020 9:47 am
I have a few questions: On the spawner, is Weight used for randomness? ie: if I set it to .5, will there be a 50% chance of spawning this prefab? When would you use an entity as apposed to a prefab? Can a prefab be a disabled prefab in the scene hierarchy and then enabled when spawning? Also, what is a spawned entity as apposed to a spawned prefab?
It's a weighted distribution. If prefab A is set to 0.5 and prefab B is set to 1.0, then prefab B will be twice as likely to occur.
If the prefab or instance is disabled, then the spawner will instantiate a disabled copy. It won't automatically activate it.
A spawned entity is managed by a SpawnedObjectManager component in the scene. The SpawnedObjectManager remembers the existence and position of all spawned entities in saved games so they can be restored. A spawner's prefab can also have a SpawnedEntity component to make it a spawned entity. (The prefab should also be assigned to the SpawnedObjectManager's list.) See Plugins / Pixel Crushers / Common / Documentation / Save_System.pdf for more details.
There are only 3 spawnpoints. The spawner will only spawn one prefab at each spawnpoint to prevent them from overlapping each other. If you want to spawn multiple prefabs in an area, you can change the Position Info to a different option.
Re: Random Enemy Spawn
Posted: Wed Oct 14, 2020 6:57 pm
by mschoenhals
Hi Tony,
Thanks very much for your help. I've worked out a system now and it appears to be going well.
All the best to you and thanks again.
Re: [SOLVED]Random Enemy Spawn
Posted: Wed Oct 14, 2020 7:48 pm
by Tony Li
Happy to help! I'm glad to hear your system is working now.