How would I go with generating a part with a variable scale and orientation? Let’s say that I want a part to start from a specific point, and finish to another one.
How would I calculate the space between point A and B, and the orientation which in this case is 0?
You would want to place the connecting part midway between A and B, and then size it to the magnitude of the distance between both points. If the points can have different orientations, you can use CFrames
I used this sample code to have a part connect two points:
local direction = pointA - pointB -- pointA and pointB are both Vector3's
part.CFrame = CFrame.new(pointB + direction/2, pointB)
part.Size = Vector3.new(1, 1, direction.Magnitude)
Position isn’t changed by setting the size of a part. Position is measured by the center point of the part, and changing the size will keep that center point. It doesn’t really matter which operation is done first.
I tried all of them, but they didn’t work. Here is the script I used (your’s with some manipulations):
local pointA = workspace.Part1.Position
local pointB = workspace.Part2.Position
local direction = pointA - pointB
local part = Instance.new("Part")
part.CFrame = CFrame.new(pointB + direction/2, pointB)
part.Size = Vector3.new(1, 1, direction.Magnitude)
Oh, maybe that’s new behavior. I could have sworn that changing the size or position of a part would make it jump to the top of any intersecting parts.
Here is the thing, I see no error. It says that it works fine!
What I mean by “would this work with UI” is that could you calculate the magnitude for GUI objects?
Magnitude would be ( ObjectB.AbsolutePosition - ObjectA.AbsolutePosition ).Magnitude
Orientation would require trigonometry. If you wanted angle from the X axis, you would do math.atan2( ObjectB.AbsolutePosition.Y - ObjectA.AbsolutePosition.Y, ObjectB.AbsolutePosition.X - ObjectA.AbsolutePosition.X )
I can’t remember which way round all the notations end up, but if your rotation ends up out by 90 degrees, swap the X’s and Y’s. And if the rotation ends up being the wrong direction (clockwise instead of anticlockwise or vice versa) then just swap the sign of the atan result as I think atan is counter clockwise and UI rotation is clockwise.
You can use Vector3.new, such as if you want it to tp, then you can do from “this position” use vector3.new and write the new position in Vector3.new(“x,y,z”) or you can do multiple Vector3.new such as
Vector3.new("x,y,z")
wait(3)
Vector3.new("x,y,z")
wait(3)
and so on...
Possibly due to misspelling PlayerGui, and not having a ScreenGui inside of it (GuiObjects only show inside of a ScreenGui, SurfaceGui or BillboardGui).
If you look at the output you’d notice it warning you of a potential infinite wait. Analysing the output window can be very helpful to identifying mistakes like that.
I tested myself and fixed a few issues regarding AnchorPoint (so that Position is measured from the centre of the object) and converting the rotation to degrees (as Rotation is in degrees, the math functions work in radians).
I’ve updated the code accordingly - put your ObjectA and ObjectB inside of a ScreenGui in StarterGui and your LocalScript somewhere like StarterPlayerScripts and hit Play.