- Home /
Choosing a random location out of a list for an object to spawn at.
I've got a basic spawning script for enemies but I'm trying to make it so the enemies will spawn at a randomly chosen location out of a set that I input.
public GameObject[] spawnObject;
public GameObject[] spawnPosition;
public float radius = 1.0f;
public float minSpawnTime = 1.0f;
public float maxSpawnTime = 10.0f;
public bool constantSpawn = false;
//public Transform spawnPosition;
//Vector3 randomLocation;
public float SpawnCount = 0f;
public float LookAtDistance = 10.0f;
public float Distance;
public Transform Target;
public float Damping = 5.0f;
private Quaternion rotation;
public bool deathspawn = true;
void Start()
{
Target = GameObject.FindGameObjectWithTag ("Player").transform;
}
void Update(){
Distance = Vector3.Distance (Target.position + Target.transform.forward, transform.position);
if (Distance < LookAtDistance)
{
LookAt();
}
}
void OnTriggerEnter(Collider Player)
{
Invoke("SpawnEnemy", Random.Range(minSpawnTime,maxSpawnTime));
}
void OnTriggerExit(Collider Player)
{
CancelInvoke ("SpawnEnemy");
deathspawn = false;
}
void SpawnEnemy ()
{
//randomLocation = Random.insideUnitSphere * radius;
//float spawnRadius = radius;
int spawnObjectIndex = Random.Range(0,spawnObject.Length);
int spawnPositionIndex = Random.Range (0, spawnPosition);
//transform.position = Random.insideUnitSphere * spawnRadius;
Instantiate(spawnObject[spawnObjectIndex],spawnPosition[spawnPositionIndex].position + randomLocation,spawnObject[spawnObjectIndex].transform.rotation);
SpawnCount += 1f;
if (constantSpawn == true && SpawnCount <= 5)
{
Invoke ("SpawnEnemy", Random.Range (minSpawnTime,maxSpawnTime));
}
}
void LookAt(){
rotation = Quaternion.LookRotation (Target.localPosition - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
public void SpawnDown (float reduction){
SpawnCount -= reduction;
if (deathspawn == true) {
Invoke ("SpawnEnemy", Random.Range (minSpawnTime, maxSpawnTime));
}
}
}
I used a random selection for the enemies themselves but to be completely honest I got that section of the code from a tutorial and I am not really sure how everything is implemented into the code. The spawnObject allows me to assign multiple objects to be spawned and I'm asssuming the spawnObjectIndex is what chooses from a random object in that list so I copies that over for the spawnPosition but I'm not sure how to put it into the instantiate function. What I've got now leads to 5 errors. I'm assuming I did something wrong with the spawnPositionIndex () bit and I know I messed up the Instantiate.
Any assistance would be appreciated and if this is unclear please let me know and I'll try to explain it better, it's difficult to describe problems concerning stuff you don't get very well
Answer by gajdot · Nov 18, 2013 at 07:22 PM
It's a bit chaotic what you did there. So if you don't mind I wont' go trough your script but tell the basic idea how to do it. So you have the random locations in the spawnPosition array, so what you need to do is, just give that to the instantiate function, like this:
int spawnPositionIndex = Random.Range (0, spawnPosition.Length); // or Count can't remember right, anyway the length of the array list
Instantiate(spawnObject[spawnObjectIndex],spawnPosition[spawnPositionIndex].transform.position,spawnPosition[spawnPositionIndex].transform.rotation);
edited it so others could have the good code too.
Yeah, my coding is definitely not the pretty right now. I'm currently just trying to get the job done, I'll work on refining stuff once this semester ends.
I put in what you suggested but I'm still getting 4 errors, all within the instantiate function.
Assets/Test Scripts/Leech_$$anonymous$$other_AI_Test.cs(73,93): error CS1061: Type UnityEngine.GameObject' does not contain a definition for
position' and no extension method position' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Assets/Test Scripts/Leech_$$anonymous$$other_AI_Test.cs(73,136): error CS1061: Type UnityEngine.GameObject' does not contain a definition for
rotation' and no extension method rotation' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Assets/Test Scripts/Leech_$$anonymous$$other_AI_Test.cs(73,17): error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments Assets/Test Scripts/Leech_$$anonymous$$other_AI_Test.cs(73,17): error CS1503: Argument
#2' cannot convert object' expression to type
UnityEngine.Vector3'
Ohh yeah, the gameobject doesn't have position, nor rotation, you need to write spawnPosition[spawnPositionIndex].transform.position and spawnPosition[spawnPositionIndex].transform.rotation
OR you need to change the spawnPosition array to Transform[] your call
That worked perfectly.
I probably should have realized the solution to that problem myself but again, still learning all the syntax.
I'm glad I could help. Even the experienced coders make silly mistakes and spend hours to figure out that you wrote = in an if statement ins$$anonymous$$d of == :D or something similar :)
Just keep up the good work :) a good programmer learns till he dies :)