Random.Range() ''Y value'' Instantiating ontop of each other? HELP!

Hi guys ;

Im trying to do instantiate 2 random objects at the same position with different Y values;

So i use Random.Range() for Y value in the code but sometimes or offenly objects instantiating ontop of each other.

How can i fix this problem?

Thank you for your help.

Here is my code;

for(int i = 0; i<2; i++)
        {

            GameObject newObstacle5 = Instantiate(powerDowns[Random.Range(0,powerDowns.Length)]);

            newObstacle5.transform.position = new Vector3(0,0,0);
            newObstacle5.transform.position += new Vector3(playerPos+270,Random.Range(-10,10),0);
         
        }

If you want it to not happen, you need to keep track of all your Y values and if they are +/- a certain amount of each other, then generate a new random number until you find one you are happy with.

So, you would need to generate a random number, check it against a list and calculate if it’s too close to existing Y positions. If not, place your object. If it’s too close, generate a new Y value.

2 Likes

Bro actually thank you for help but im not very good at coding and if i say i understand i would’ve lie to you, if you have any time to write can you make it with example?

Instead of one of us trying to laboriously hammer all that back into a tiny little box just for you, why not go look up any one of the millions of discussions and/or tutorials already on the internet?

Because i didin’t understand that so didin’t have the right question, nobody wants to wait ofcourse i did look but couldn’t find with my questions maybe because of its my english.

So im really grateful that you show me the right one now i can move on.

Thank you guys!