- Home /
Spawning enemy at random spawn points without Overlapping!
Hi Unity Answers,
I want to spawn Enemies at random spawn points without overlapping. So, I'm checking that if the spawn point is used then don't use that spawn point, if it's not then continue... I've an empty gameObject which contains all spawnPoints for enemy. I've assigned the script to that empty gameobejct.
I tried to use Transform as a List for Spawn points but unfortunately, it didn't work. If you can convert this from Vector3 to Transform then, it'll be good. I tried to convert this in Transform but I got so many error. I did try, but, it didn't work as expected.
You can check that script here: Transform Error Script Errors in that script is here: Image Transform error
Now, The script which I'm working on is as follow:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemySpawns : MonoBehaviour {
public GameObject enemy; //my enemy
public float spawnTime = 3f; //spawn after 3 sec
public List<Vector3> spawnPoints = new List<Vector3> (); //Vector3 positions of spawnpoints.
public float distance; //distance from the spawnPoints
void Start ()
{
//spawnRandom ();
InvokeRepeating ("spawnRandom", spawnTime, spawnTime); //Call this fucntion after 3sec.
}
public Vector3 spawnRandom(){
Vector3 newSpawnPoint; //new spawn point
Vector3 random = UnityEngine.Random.insideUnitSphere * distance;
newSpawnPoint = new Vector3 (random.x, 0, random.z);
newSpawnPoint += transform.position;
if (!spawnPoints.Contains (newSpawnPoint)) {
return newSpawnPoint;
spawnPoints.Add (newSpawnPoint);
int spawnIndex = Random.Range (0, spawnPoints.Count);
Instantiate (enemy, spawnPoints [spawnIndex].normalized, Quaternion.identity); //Spawn randomly
} else {
return Vector3.zero;
Deug.Log("Enemy is overlapping!");
}
}
}
The problem is: Enemy is not spawning at all! Thanks!
Answer by krispastas · Apr 06, 2020 at 01:44 PM
Just check if the enemy is colliding with another enemy.
Answer by Sawyer1424 · Apr 08, 2020 at 03:57 PM
I would have the empty game object go to a random area with a Navmesh that is twice as large as your largest enemy, have it always find a new valid area and go to it, give it a lot of speed and have it instantiate an enemy every three seconds. Give it a rigidbody along with everything else, but give it a lot smaller weight so it can't push your enemies, make these on a separate collision layer so your player can't interact with it.
code for the random movement
using UnityEngine.AI;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemySpawns : MonoBehaviour {
public GameObject enemy;
public float distance = 10;
public float spawnTime = 3;
void Start ()
{
//spawnenemy
InvokeRepeating ("spawn", spawnTime, spawnTime); //Call this fucntion after 3sec.
}
void FixedUpdate ()
{
Vector3 randomDirection = Random.insideUnitSphere * distance;
randomDirection += transform.position;
NavMeshHit hit;
NavMesh.SamplePosition(randomDirection, out hit,distance, 1);
Vector3 fleePosition = hit.position;
agent.ResetPath();
agent.destination = fleePosition;
}
public void Spawn ()
{
Instantiate (enemy, transform.position);
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
problem in minimax algorithm 1 Answer
Unity random card select system with persentage 1 Answer