When I assign a spawning code to an object, once the code activates the object goes to the world origin while the objects spawn at the designated area. I was wondering if anyone could tell me how to keep the object where I place it. I could just assign the code to something else but I would rather have the code on that object.
Here’s the code I’m using.
public GameObject[] spawnObject;
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 = 0.0f;
void OnTriggerEnter(Collider Player)
{
Invoke("SpawnEnemy", Random.Range(minSpawnTime,maxSpawnTime));
}
void SpawnEnemy ()
{
randomLocation = Random.insideUnitSphere * radius;
float spawnRadius = radius;
int spawnObjectIndex = Random.Range(0,spawnObject.Length);
transform.position = Random.insideUnitSphere * spawnRadius;
Instantiate(spawnObject[spawnObjectIndex],spawnPosition.position + randomLocation,spawnObject[spawnObjectIndex].transform.rotation);
SpawnCount ++;
if (constantSpawn == true && SpawnCount <= 5)
{
Invoke ("SpawnEnemy", Random.Range (minSpawnTime,maxSpawnTime));
}
else
{
CancelInvoke("SpawnEnemy");
}
}
}