Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-2

How would I randomly place an object inside of a grid that has to be a certain distance away?

Asked by 8 years ago

So I've made a 100x100 part and I'd like to be able to randomly place a part inside of there, I've divided the grid up into 5x5 parts inside of that and I've iterated through the model in workspace that the grid is contained in but whenever I put the parts inside of that grid they're all right next to to each other. I'd like to be able to have them each a certain distance away from each other. How would I achieve this?

1
I'm confused, what do you mean by `they are all right next to each other` and `have them a certain distance away from one another`? BlackJPI 2658 — 8y
2
First of all, You already asked a question almost the same as this. Second, we need code and a better explanation to understand what you're saying. User#11440 120 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

Lets use some simple math to make sure they're spread apart from each other.

Our grid is 100x100, so lets assume its in the centre of the map. Our parts are 5x5 each. 100/5 is 20. That means we have 20 possible places for each axis.

The centre of a 5x5 part is 2.5 by 2.5, however the boundaries of the grid are 50,50,-50,-50. This means that the position of the 5x5 part must begin at 47.5, and iterate by 5.

local positionX = math.random(20) --generating a random number from 1 to 20
local positionY = math.random(20) --doing the same for both axis

positionX = (positionX-10.5)*5 --converting the random number from 1~20 to -47.5~47.5
positionY = (positionY-10.5)*5 --doing the same for both axis

positionVector = Vector3.new(positionX,0,positionY) --creating the vector.

Alternatively this code can be condensed into

positionVector = Vector3.new((math.random(20) -10.5)*5,0,(math.random(20) -10.5)*5)

Hope this is what you were looking for, or at least can build off of what I taught you.

Ad

Answer this question