Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i spawn objects randomly between 2 CFrames?

How do i randomly spawn objects between 2 CFrames or if there is a easier way?

1 answer

✓ Accepted answer

I assume you mean on the line segment between the two CFrames' position. In this case, it's as simple as:

local start = cf1.p
local finish = cf2.p
local displacement = finish - start

local point = start + displacement*math.random()

Try to visualize the math in your head. As game developers we think of vectors as arrows in space. They can also be thought of as points.

In this case,

  • start and finish are two points corresponding to your CFrames' position; and
  • displacement is an arrow that starts from one of the points and reaches the other.

Now, to get a new, random point between these two points we simply stretch that displacement vector by a random factor comprised between 0 and 1. We can think of this number as a percentage of the distance between our two points.

We then add this new stretched displacement vector back to the first point to get our new random point.

1 comment

0

thanks :)

This discussion is preserved as it was. New answers, comments, and votes are closed.