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

Finding the rotation between 2 points?

Asked by 9 years ago

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

1 answer

Log in to vote
4
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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.

1
Thanks :) Prohibetur 70 — 9y
1
Using lerp is pretty opaque in this case, why not just take the arithmetic average like you mean? BlueTaslem 18071 — 9y
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. adark 5487 — 9y
Ad

Answer this question