Creating a Part between 2 Positions

I would like to create a part between two Vector3 positions. I using this to create some kind of gun, and I didn’t want the laser to go through wall, so I am taking the position of the barrel and the position of where the ray hit and combining them. This is what I have so far:

local function addVectors (Vect1, Vect2)
	return (((Vect1.X + Vect2.X)/2) + ((Vect1.Y + Vect2.Y)/2) + ((Vect1.Z + Vect2.Z)/2)) / 3
end

local laserPart = Instance.new("Part", workspace)
laserPart.Name = "Laser"
laserPart.Anchored = true
laserPart.CanCollide = false
laserPart.CFrame = CFrame.new((Tool.Handle.Barrel.WorldPosition + showRay.Position) / 2, Tool.Handle.Barrel.WorldPosition)
laserPart.Size = Vector3.new(0.1, 0.1, addVectors(Tool.Handle.Barrel.WorldPosition, showRay.Position))
laserPart.Material = Enum.Material.Neon
laserPart.BrickColor = BrickColor.new("Bright green")

The main problem I am having is finding how to accuratly make the size of the part correct. I have tried various methods on what to do, but none have worked. Thank you to everyone in advance!

What methods have you tried so far?

I have tried adding each of the values of each axis together and divided it mostly.

Have you tried (Vector3 - Vector3).magnitude ?

4 Likes

Sounds like a good idea, I completely forgot about .magnitude.

This question has been asked on many sites before.

Please search before you ask a question
Here is code to make a part go between two points:

local Distance = (p0-p1).Magnitude
Part.CFrame = CFrame.new(p0,p1) * CFrame.new(0,0,-Distance/2)
Part.Size = Vector3.new(1,1,Distance)
6 Likes

Okay, thank you. Sorry for reposting, I couldn’t find a good reference…

1 Like
part.CFrame = part1.CFrame:Lerp(part2.CFrame, .5)

I think its like this

1 Like