How would I find a position in between two vector positions?

So let’s say I have:

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:


I preferably do not wish to use ways but it looks like the only option I have.

5 Likes

So the midpoint of those two vectors is just simply

local pos3 = (pos1 + pos2)/2

https://gyazo.com/3733cc887c425a7f3cd5276f1e61806b

17 Likes

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.

1 Like

In theory, you can just find the unit direction between pos1 and pos2, then multiply that direction by 20 studs, add pos1:

local newPos = (pos2 - pos1).Unit * 20 + pos1

I will note that this new position can move beyond pos2, and if you don’t want that, you can take a math.min using the magnitude between the two:

local newPos = (pos2 - pos1).Unit * math.min(20, (pos2 - pos1).Magnitude) + pos1
23 Likes

Oh nice, I will try this out and will make this the solution if it works! Thank you!

Sorry I didn’t read the full question :3

1 Like

Thanks for a possible solution however! Much appreciated :smiley:

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)
14 Likes