using UnityEngine;
using System.Collections;
[System.Serializable]
public class SpawnArea
{
public float xMin, xMax, zMin, zMax;
public float rotaionMin, rotationMax;
}
[System.Serializable]
public class SpawnTimes
{
public float TimeFromStart, Rate, otherRate;
}
public class Spawn : MonoBehaviour
{
public SpawnArea area;
public SpawnTimes time;
public GameObject ball;
public int MaxEnemies = 5;
public int enemy;
float timestamp = 0.0f;
void Update()
{
enemy = FindObjectsOfType<DestroyByTime> ().Length;
if (enemy < MaxEnemies && Time.time >= timestamp)
{
WaitSpawn ();
timestamp = Time.time + time.otherRate;
}
}
void WaitSpawn()
{
float randomX = Random.Range (area.xMin, area.xMax);
float randomZ = Random.Range (area.zMin, area.zMax);
float randomRotation = Random.Range (area.rotaionMin, area.rotationMax);
Instantiate (ball, new Vector3 (randomX, 5.0f, randomZ), Quaternion.Euler (0.0f, randomRotation, 0.0f));
}
}
Did you assign values to your min & max in the inspector?
yes… i was wondering if there is a way to make it so that the places that the enemies are in will not be an area that they can spawn
The easiest way to do this is to look if there’s already something there with raycasts or something, and try again if the space is taken.
If your enemies are balls, a simple SphereCast from above where you’re trying to spawn to the ground should do it. Do a SphereCast, check if you hit something that’s not the ground, spawn if you hit something.
You can either keep trying to spawn until you find a random spot where there’s room, or just not spawn and wait for the next cooldown. That’ll make the spawn rate slow down as there’s more enemies, which may be nice.