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

How do you generate a part based on two vector3 values?

Hello there! For a while I've been making a game where you can select two points in the world and generate a brick from point a to point b.

Currently, you are able to generate two points, but the brick from a to b isn't exactly generating correctly.

The current solution is to make a bunch of 1x1x1 parts to fill in the gap, which can cause lag very quickly.

All parts are on a gridsize of 1.

What I'm trying to accomplish:

http://prntscr.com/si7yfp

Remember this is 3D, not just x and z.

My current solution (inefficient and laggy)

http://prntscr.com/si7znx

Any help is appreciated! Thank you!

1 answer

✓ Accepted answer

I would measure the difference between coordinates on all three axes and set the size of the part to those three differences:

local pointOne = Vector3.new(24, 15, 10)
local pointTwo = Vector3.new(-15, 6, -42)

local x = math.abs(pointOne.X - pointTwo.X)
local y = math.abs(pointOne.Y - pointTwo.Y)
local z = math.abs(pointOne.Z - pointTwo.Z)

local part = Instance.new("Part")
part.Size = Vector3.new(x, y, z)

local connector = Instance.new("Part")
local distance = (pointOne - pointTwo).Magnitude
connector.Size = Vector3.new(1, 1, distance)
connector.CFrame = CFrame.new(pointOne, pointTwo) * CFrame.new(0, 0, -distance/2)
connector:Destroy()

part.Position = connector.Position
part.Parent = game.Workspace

The points are random but it should work.

I subtract pointTwo's coordinate from pointOne's and just call math.abs on it which returns the positive version, if it's negative, because we can't give our part a negative size.

I create the part, and size it.

To get its position, I create a part that is the length of the distance between the two points, and physically goes from point one to point two. After that, I just set the newly created part's position to the position of the connector. And it works.

1 comment

1

Thanks! I'll try it

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