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.
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.
--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
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”
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))
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