W
W
WasTabon2020-12-07 16:37:34
Unity
WasTabon, 2020-12-07 16:37:34

How to make objects not spawn too close (2d)?

public GameObject spawnObj;
    public Vector3 spawnPos;

    void Start()
    {
        StartCoroutine("spawn");
    }

    IEnumerator spawn()
    {
        for (int i = 0; i < 2; i++)
        {
            spawnPos.x = Random.Range(-4.3f, 4.4f);
            spawnPos.y = Random.Range(-2.75f, 2.8f);
            int wait = Random.Range(0, 1);
            yield return new WaitForSeconds(wait);
            Instantiate(spawnObj, spawnPos, Quaternion.identity);
        }
    }
}


Here is the code, it will spawn 2 platforms, how can I check that the 2 platforms don't spawn too close to each other?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mopsicus, 2020-12-07
@mopsicus

What is the problem? Where there are coordinates, you can do while and check by some constant whether it satisfies the condition (by distance or height) - that's all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question