local pos1 = workspace.Part1.Position
local pos2 = workspace.Part2.Position
In this example they would be roughly be 100 studs away from each other and their vector positions are completely random. How would I find the position of let’s say 20 studs of the distance between the starting position (pos1) and end position (pos2) regardless if they parts are further or closer than 20 studs? Very sorry the way I worded it but I am struggling to do so. Use the diagram below:
Thanks for replying! But it would appear that this is not what I want exactly, for example, the mid point isn’t always going to be 20 studs between pos1 and the mid point.
Edit: This is more applicable to the original question:
The easiest way to do this is to simply use the normalized vector (i.e. unit vector) and multiply it by the desired distance:
local pos1 = workspace.Part1.Position
local pos2 = workspace.Part2.Position
local direction = (pos2 - pos1).Unit
local distance = 20
local position = pos1 + (direction * distance)
My original answer:
This is called “lerping” (no, not larping). It’s short for “interpolating.” There’s actually a built-in function to do it!
local pos1 = workspace.Part1.Position
local pos2 = workspace.Part2.Position
local center = pos1:lerp(pos2, 0.5)
The “0.5” indicates that you want to “lerp” 50% from pos1 to pos2.
The actual math behind it is pretty simple. To “lerp” a single number, the math looks like this:
local function Lerp(n1, n2, percent)
return n1 + ((n2 - n1) * percent)
end
You’re simply adding the difference of n2 and n1 multiplied by the percent to the original n1 position. A Vector3 lerp would simply do the same math on all X, Y, & Z axes.
In order to then find an exact distance on the line (such as 20 studs), you could manipulate the lerping function. You have to solve for the “percent” which can be done simply by dividing the actual distance by the desired distance:
local pos1 = workspace.Part1.Position
local pos2 = workspace.Part2.Position
local distance = (pos1 - pos2).Magnitude
local desiredDistance = 20
local center = pos1:lerp(pos2, desiredDistance / distance)