I made a simple top-down schematic view of the world from the game that I’m currently working on:
The yellow circles are islands, the blue part is the ocean, the red circle is the island that currently Player is on, and the grid shows all my neighbour terrains(I have more than 6 islands and 8 terrains). As you can see some islands are at the edge of 2 terrains. I need a way to spawn some enemies, that are searching for player, but spawn only on the island that currently the Player is sitting on, even if that island is spreading on 2 terrains. The attacks should be constant. Maybe 1 per day or another period of time. I have the AI to search for, kill, etc. but I need a way to make the spawning of them and I have no idea what to do.
It sounds like currently your enemy spawning script is “terrain” based, when really it should be “island” based.
I don’t have even a Terrain based spawning script
I tried this Coroutine:
public IEnumerator SpawnEnemies()
{
Vector3 RandomPos;
for(int i = 0; i < enemiesCount; i += 0)
{
RandomPos = Player.transform.position + Random.insideUnitSphere * 60;
if(RandomPos.y < 61) //under sea level
{
yield return null;
}
if(Vector3.Distance(RandomPos, Player.transform.position) < 30) //Is near player, so not valid
{
yield return null;
}
Instantiate(EnemyPrefab, RandomPos, Quaternion.identity);
i++; //increase only if valid position
yield return new WaitForEndOfFrame();
}
}
at this line RandomPos = Player.transform.position + Random.insideUnitSphere * 60; I think I don’t need to change the y to be exactly on the terrain because it has a NavMeshAgent component, that makes the object stick to the ground.
It is working now. The code:
public IEnumerator SpawnEnemies()
{
Vector3 RandomPos;
for (int i = 0; i < enemiesCount; i += 0)
{
RandomPos = Player.transform.position + Random.insideUnitSphere * 60;
RandomPos = new Vector3(RandomPos.x, terrain.SampleHeight(RandomPos), RandomPos.z);
if (RandomPos.y > 61 && Vector3.Distance(RandomPos, Player.transform.position) > 30) //under sea level
{
GameObject EnemyInstance = Instantiate(EnemyPrefab, RandomPos, Quaternion.identity);
EnemyInstance.GetComponent<NavMeshAgent>().Warp(RandomPos);
i++; //increase only if valid position
yield return new WaitForEndOfFrame();
}
else
{
yield return null;
}
}
}
Also I changed in the Enemy AI script from Start() to Awake()