Detect if vector3 is between 2 vector3

Hello there! I want to detect if vector3 is inside region of 2 vector3s,but the script i got also returns true if vector3 is on a line of cube:

local a = Vector3.new(10,10,10)
local b =  Vector3.new(5,5,5)
local v =  Vector3.new(5,6,5)
local range = (a-b).unit:Dot(v-b) / (a-b).magnitude
local isBetween = range > 0 and range < 1
print(isBetween)
--true
local a = Vector3.new(0.3, 0.6, 25)

local b = Vector3.new(0.3, 0.6, -25)

local c = Vector3.new(0.3, 0.6, -20)

local newPos = (a - b).Unit * math.min(20, (a - b).Magnitude) + b

local PartAVisual = Instance.new("Part")

PartAVisual.Anchored = true

PartAVisual.Parent = workspace

PartAVisual.Color = Color3.new(0, 1, 0.364706)

PartAVisual.Position = a

local PartBVisual = Instance.new("Part")

PartBVisual.Anchored = true

PartBVisual.Parent = workspace

PartBVisual.Color = Color3.new(1, 0, 0)

PartBVisual.Position = b

local PartCVisual = Instance.new("Part")

PartCVisual.Anchored = true

PartCVisual.Parent = workspace

PartCVisual.Color = Color3.new(0, 0, 0)

PartCVisual.Position = c

print((c - b).Unit * (c - b).Magnitude)

print((a - b).Unit * (a - b).Magnitude)

local PosCBetweenB = (c - b).Unit * (c - b).Magnitude

local PosCBetweenA = (c - a).Unit * (c - a).Magnitude

local PosBetween = (a - b).Unit * (a - b).Magnitude

if math.abs(PosCBetweenB.Z) <= math.abs(PosBetween.Z) and math.abs(PosCBetweenA.Z) <= math.abs(PosBetween.Z) then

print("Inside")

else

print("Outside")

end

This was a fun mind twister, with some trial and error. Unfortunately, I was only able to get one axis. Though, you can check multiple axis with this method by doing a few more “if” statements with the different axis like X and Y.

1 Like
local function isVectorInsideVectors(sVector : Vector3, eVector : Vector3, vector : Vector3) : boolean
	if (vector.X > sVector.X and vector.X < eVector.X) and (vector.Y > sVector.Y and vector.Y < eVector.Y) and (vector.Z > sVector.Z and vector.Z < eVector.Z) then
		return true
	else
		return false
	end
end

print(isVectorInsideVectors(Vector3.new(0, 0, 0), Vector3.new(10, 10, 10), Vector3.new(5, 5, 5))) --true
print(isVectorInsideVectors(Vector3.new(0, 0, 0), Vector3.new(10, 10, 10), Vector3.new(-1, 11, 5))) --false

Wouldn’t this be essentially as simple as this?