- Home /
How to spawn objects in a specific range of random location
I have a tile based game. Each tile's position is a multiple of 1.25, (For Example: 7.5, 3.75) and I want to spawn walls at a random location but at a specific range. I tried Random.Range(7.5,6.25) but that spawns anywhere BETWEEN the 2 numbers instead of spawn in either of those 2 locations. Is there a way of spawning objects like that? Thanks! (Btw I would really appreciate it if you answer in javascript because I don't really know much about c#)
Answer by HarshadK · Nov 03, 2014 at 05:18 AM
If you want a random value between just two numbers then you can use
Random.Range(0,2);
This will give you result as either zero or one then you can have a value pre-defined for 0 i.e. 7.5 and for 1 it will be 6.25.
Or if you just want to generate a random number between all whole range of tiles you can use something like:
1.25 * Random.Range(0,N);
where N can be any integer based on the number of tiles.
So if the random generated number is 2 then you will get the position to place is as 2.5 since it is your second tile.
O$$anonymous$$G THAN$$anonymous$$S I NEVER THOUGHT OF $$anonymous$$ULTIPLYING THE RANDO$$anonymous$$ NU$$anonymous$$BER THAN$$anonymous$$S YOUR $$anonymous$$Y HERO!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Generate random number and set a GameObject active. 1 Answer
Calling random functions 4 Answers
Random spawn timer 1 Answer
How can i delete an item from an array after being used? (So, it won't be repeated) 2 Answers