How would I go about forming a part using a script which connects two points together? Is there a roblox event for this? The two points have completely different x, y and a values. No x, y or z value are the same. My current problem would be how to orientate the part to connect between the two points.
That constructor creates a CFrame from 2 vectors, positioned at the first and angled at the second. From there you take the magnitude between them, and move away from the first location by its lookVector times half the magnitude.
You should do that with a part that has Z size equal to the distance between the 2 points.
I did that before but it didn’t work properly. I’ve changed the code and resized the part and it works now. I think rotating my part directly using CFrame messed up with it because once I removed those lines, the code works just fine. Thanks for sharing with me the concept, now I know the right steps to do this.
This could’ve also been done with a good old point slope formula if you want to use multiple parts
I found this thread and I am facing the same problem as you did back then. Unfortunately there are only solution hints and no concrete solution. Maybe you can help me.
My initial situation: I have two parts in the workspace which should be connected by one part. The parts can have different positions on X,Y and Z.
My results so far unfortunately look like this:
With a hack that only works in exceptional situations:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local model = script.Parent local startPart = model.Start -- Left part local endPart = model.End -- right part local path = ReplicatedStorage:WaitForChild("3DModels"):WaitForChild("Path"):Clone() -- The dotted line path.Parent = model path.CFrame = CFrame.new(startPart.Position, endPart.Position) --local rotatedCFrame = CFrame.Angles(0,math.rad(90),0) -- my "hack" --path.CFrame = path.CFrame:ToWorldSpace(rotatedCFrame) local magnitude = (startPart.Position - endPart.Position).Magnitude path.Position = Vector3.new(path.Position.X+(magnitude/2), path.Position.Y, path.Position.Z) path.Size = Vector3.new(magnitude, path.Size.Y, path.Size.Z)
I have already read “Understanding CFrames” and only came across my “hack”. Unfortunately I am bad in geometry and physics. What am I doing wrong? In the best case I would like to use existing functions of CFrame. Otherwise I do not know how to calculate the angles of a Vec3.
I think that’s happening because you are applying the size on the x-axis try using the Z axis instead:
path.Size = Vector3.new(path.Size.X, path.Size.Y, magnitude)
Or rotating it 90 degrees might work? Like:
path.CFrame = CFrame.new(startPart.Position, endPart.Position) *CFrame.Angles(0,math.rad(90),0)
Also, you might want to create a new topic, If your issue can’t be relatively quickly solved. (I.e as to prevent a large addition of posts to this already 2 year old topic)
thanks for the very quick response. In fact, the problem seems to be solved - but its not. Because I have now extended the side length Z, the parts are actually connected. I want a rotation, because I don’t want this part to be wider. I just want it to be automatically longer and to align itself at the right angles on the X,Y,Z plane. The result of your suggestion looks like this:
According to your seccond suggestion: It cause the the same results as Image 2 and 3 in my initial post.
Only works if the part’s orientation are not opposites, you can try using the part’s look vector:
local model = script.Parent local startPart = model.Start local endPart = model.End local FirstConnection = startPart.CFrame.LookVector.Unit * startPart.Size.Z/2 local SecondConnection = -endPart.CFrame.LookVector.Unit * startPart.Size.Z/2
This solution will make the part’s star(red) and endpoint(blue) at the edge of the parts:
Then you would find the magnitude of the parts, including the look Vector. The position is also calculated, reference the image above:
local Mag = (FirstConnection - SecondConnection).Magnitude local LV = CFrame.new(FirstConnection, SecondConnection) local Pos = FirstConnection * LV * Magnitude / 2 --[[ Basically first I take the start point(red), multiplying it by the look vector(black line), then multiplying half the magnitude so that it results in the middle of the two magnitudes(roughly where you are standing) --]]
The end code would look something like this:
local model = script.Parent local startPart = model.Start local endPart = model.End local FirstConnection = startPart.CFrame.LookVector.Unit * startPart.Size.Z/2 local SecondConnection = -endPart.CFrame.LookVector.Unit * startPart.Size.Z/2 local Mag = (FirstConnection - SecondConnection).Magnitude local LV = CFrame.new(FirstConnection, SecondConnection) local Pos = FirstConnection * LV * Magnitude / 2 path.Position = Pos path.Size = Vector3.new(path.Size.X, path.Size.Y, Mag)
thank you for all the help you’ve given me so far. So that we all have a uniform example, I have now set up a new project. A clock hand should show the correct time in the project. The clock hand should touch the center of the Clock and the part of the time. In the presented solution of @OnlyJaycbee only 9 o’clock works.
Thanks for your help!
ConnectingParts.rbxl (19.5 KB)
I wrote and use this function to position and size a part connecting two Vector3 points.
function setTwoEndPoints(part, point1, point2) local magnitude = (point1 - point2).magnitude part.Size = Vector3.new(part.Size.X, part.Size.Y, magnitude) part.CFrame = CFrame.new( point1:Lerp(point2, 0.5), -- center halfway between points point2 --look at second point ) return part end
In your example place file, I swapped the X and Z size values of the pointer part, then used this function to set the size and position of the pointer part using startPart.Position and endPart.Position as arguments to the function.
ConnectingParts.rbxl (19.8 KB)
thank you so much! It worked.
Thank you so much for this I had a problem similar to this and i just couldn’t get the math right