Get position at same y level given 2 unit vectors

I am facing a bit of a math problem here, I was wondering if anybody can help me fix my problem.
The problem: Given 2 Unit vectors (green and blue) I want the second red point (Vector3) to be in line with the first one in the Y axis (where the black point is). I have no idea on what formula this needs.

image

So you want one vector to have the same y as another?
If so then just set the y to the other vector’s y.

That wont work since I basically want the blue line to stretch forwards so that it’s end point matches the red point on the green line’s Y position.

Like this:
image

Oh i see what you mean. I think I can help you.

First get the y of the 1st one, then the Y of the second one. Divide the 1st y by the 2nd y.
Multiply this value by your 2nd vector.

local Y1 = vector1.Y
local Y2 = vector2.Y

local Multiplier = Y1/Y2

local FinalVector = vector2 * Multiplier


Thanks! That works perfectly, can’t believe I didn’t think about this. It seems to more complex when thinking about it.

Oh, I just realised it doesn’t work when the vectors are rotated. Do you have any idea on how I could fix this?

Yeah I started to think of that after i said it lmao.
What does it print?

I don’t have it printing anything, I set up a visual example of it. And it’s just weird when the vectors are rotated

Show me what it is in that case.

Nvm, this puts it on the same y level but it has a slight offset that makes it so that the blue vector doesn’t line up with it.

The visualization looks like this currently:
image

I think the solution has something to do with the dot product, but I’m not too sure

I don’t rlly understand the problem, do you have three pre-defined points? Are you trying to find the length between points A and B? In that case, couldn’t you use the Pythagorean theorem?

No, this might look confusing but C and B are just generated Vector3 based on the calculations I am trying to achieve. In this case C is in the right place but B is suppossed to be aligned with the LookVector of the blue part in the image.

I basically just made a visualization to make it easier to see when I have the right formulas.

I’ll clear it up and show the real problem I have isntead.

I have a raycast that gets fired like in this image.
I have a ball that I want to place like shown in the image below; where the position of the ball is on the “Expected Position”.
I don’t want the ball to stick in to the wall.
I can place it on the Orange point and it’d be easier but that is unaccurate from the raycast as shown.

All in all, I basically want to get the “Expected Position” using the “Inaccurate Position”. And place the ball there instead.
image

Try something like this:

--I dont feel like scripting a lot, pretend the RayHit is the position at which the raycast ended

local RayHit1 = CFrame.new(RayHit)

Ball.Position = (RayHit1 * CFrame.new(Vector(0, 0, Ball.Size.Y/2))).Position
--Z is forward/backwards relative to the ball's front

Haven’t tested it tho

Sorry but this is kinda what I am trying not to do, this will return the “Inaccurate Postion” in the image above. I am trying to get the “Expected Position”

local RaycastHit = Vector3.new()
local RaycastStart = Vector3.new()

Ball.Position = RaycastHit + (RaycastHit-RaycastStart).Unit * Ball.Size.Y/2

I just realized this will sometimes still intersect with the wall depending on the angle, This should work better

local Normal = Vector3.new()
local Origin = Vector3.new()
local Hit = Vector3.new()

local Ball
local HitToOrigin = (Origin-Hit).Unit
local NewPosition = Hit + HitToOrigin * ((Ball.Size.Y/2)/Normal:Dot(HitToOrigin))

This is basically a linear interpolation problem.

The way you can imagine this is that you want to determine how many times you have to take a step (in this case, the unit vector is your step) to go a certain distance.

The distance you have to go can be imagined as (DesiredY - CurrentY), and then to determine how many steps you have to take to cover that distance you simply divide by the UnitVector.Y value.

You then multiply your UnitVector (your step value) by the amount of steps you have to take and you’ll achieve a position at your desired Y level while still having moved in the same direction as the unit-vector.

The code looks like this:

local function GetPointAtY(origin: Vector3, direction: Vector3, targetY: number): Vector3
	local steps = ((targetY - origin.Y) / direction.Y)
	return (origin + (direction * steps))
end

The logic for this can apply to any axis.