# Finding position on ray from y position

hi I’m trying to do something like this.

the cyan line is the ray and I’m trying to get the position of the red dot without having to place a part on the y position and cast another ray to find out.

Anyone know how to do this?

1 Like

You can find it by rearrange the vector line equation

``````local rayOrigin
local rayDirection --Components of a ray
--Vector equation of a line, pointOnALine and positionAlongLine  and DirectionOfTheLine  are all Vector 3.
positionAlongLine = pointOnALine + DirectionOfTheLine * X -- X is a scalar parameter
--Solving for only the y component to find when Y = 2
y = rayOrigin.Y + rayDirection.Y* X -- y is a y position on the line, X is a scalar parameter
--To find when y = 2
2 - rayOrigin.Y = rayDirection.Y*X
--therefore
X = (2 - rayOrigin.Y ) / rayDirection.Y
--Once you found X you can find the end result position by subsituting the X scalar parameter into the vector line equation.
--Notice the equation fails if the ray direction Y is equal to zero
local redDotPosition = rayOrigin +rayDirection*X
--Or combining the equations
local redDotPosition = rayOrigin +rayDirection*(2 - rayOrigin.Y ) / rayDirection.Y
``````

Also keep in mind this equation is a line going both directoins and not a ray in one direction, For a ray the X scalar parameter is limited to X>0 meaning it c an only go along the rayDirection.

2 Likes