jeudi 13 décembre 2018

How to avoid a random with the same index

a little question. I have a class that randomly creates some objects on the stage, These objects appear at two random points. The problem is Random.Range has the probability of choosing a point several times in a row, that is, I pick the same point 5 or 6 times. I just want to limit myself somehow, that if Random.Range chose the same point at most 3 times, he would choose another point.

public ObjectSequence ObstacleSequence;               
public float WaitSpawnTime;            
public float DistanceFromObstacleIndex;
public Transform[] spawnPoints;         


void Start()
{
    InvokeRepeating("Spawn", WaitSpawnTime, DistanceFromObstacleIndex);
}

void Spawn()
{
    int spawnPointIndex = Random.Range(0, spawnPoints.Length);
    ObjectSequence obstacle = Instantiate(ObstacleSequence, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    obstacle.setRandomCurrentChildIndex();
}

I notice that I only have two points. Thanks in advance




Aucun commentaire:

Enregistrer un commentaire