- Home /
Question by
AshwinTheGammer · Apr 06, 2020 at 06:00 AM ·
instantiatevector3spawning problemsenemy spawn
[VECTOR 3]Spawning enemy at random position
How would I spawn enemy randomly at vector3 position? Here's code: 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;
//public int maxEnemy = 40;
//public int currentEnemy = 0;
void Start ()
{
//spawnRandom ();
InvokeRepeating ("spawnRandom", spawnTime,
8.7f); //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)) {
//int spawnIndex = Random.Range (0,
spawnPoints.Count);
Instantiate (enemy, newSpawnPoint,
Quaternion.identity);
return newSpawnPoint;
spawnPoints.Add (newSpawnPoint);
} else {
return Vector3.zero;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
I need help with rotation 2 Answers
Enemy prefab wont work with the enemy movement script 3 Answers
Instantiate object in C# 1 Answer