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

Finding the rotation between 2 points?

If you wanted to create a line (made of a part) between 2 points how would you find the needed rotation?

1 answer

✓ Accepted answer

If you want just a straight line, ROBLOX already has a CFrame constructor for you to use, as well as the nifty Lerp method of Vector3s.

local p1 = workspace.Part1.Position
local p2 = workspace.Part2.Position --These are your two given 'points' Both are Vector3s

local part = Instance.new("Part", workspace)
part.FormFactor = Enum.FormFactor.Custom
part.Size = Vector3.new((p2 - p1).magnitude, .2, .2) --IIRC 'x' is the lookVector axis. The other two define the thickness of the line.

local midP = p1:Lerp(p2, .5) --math
part.CFrame = CFrame.new(midP, p2) --more math. Isn't this fun?

If the part you're creating is an arrow mesh or some other thing where the point it's actually facing is important, you can change the p2 in that last line to p1 to point it at that position instead.

3 comments

1

Thanks :)

1

Using lerp is pretty opaque in this case, why not just take the arithmetic average like you mean?

0

All I care about is the midpoint between p1 and p2, which is where the center of the created line (it's Position) has to be. I actually don't understand how taking the average of the two points helps, unless I'm totally misunderstanding you.

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