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
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
– gajdotThat worked perfectly. I probably should have realized the solution to that problem myself but again, still learning all the syntax.
– ChappyI'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 instead of == :D or something similar :) Just keep up the good work :) a good programmer learns till he dies :)
– gajdot