Hi everyone, i want my objects to spawn at random positions without repeating the same position. For example there are 7 locations and and i basically want them to shuffle.
I tweaked the Spawner script that is used in the 2D Platformer demo to have random spawns. It is below. Any idea how can to avoid repeating?
using UnityEngine;
using System.Collections;
public class SpawnerMY : MonoBehaviour
{
public float spawnTime = 5f; // The amount of time between each spawn.
public float spawnDelay = 3f; // The amount of time before spawning starts.
public GameObject[] enemies; // Array of enemy prefabs.
public Transform[] teleport;
void Start ()
{
// Start calling the Spawn function repeatedly after a delay .
InvokeRepeating("Spawn", spawnDelay, spawnTime);
}
void Spawn ()
{
// Instantiate a random enemy.
int enemyIndex = Random.Range(0, enemies.Length);
int tele_num = Random.Range(0,7);
Instantiate(enemies[enemyIndex], teleport[tele_num].position, teleport[tele_num].rotation);
// Play the spawning effect from all of the particle systems.
foreach(ParticleSystem p in GetComponentsInChildren())
{
p.Play();
}
}
}
↧