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

How do i spawn objects randomly between 2 CFrames?

Asked by
CjayPlyz 643 Moderation Voter
6 years ago

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

1 answer

Log in to vote
0
Answered by
Link150 1355 Badge of Merit Moderation Voter
6 years ago
Edited 6 years ago

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.

0
thanks :) CjayPlyz 643 — 6y
Ad

Answer this question