Help with trigonometry script!

I am trying to create a method that takes as parameters the red reference vector, the green target point, and the blue/orange (A/B) point.

The goal is to end up with a final point, that lines up on the perpendicular green line between target point and reference vector, how far along the perpendicular is determined by the original position of (A/B) and the direction of the original reference red vector.

I wanted something like

public Vector2 GetFinalPoint( Vector2 referenceVec, Vector2 TargetPoint, Vector2 selfPoint){
return finalpoint;
}

I hope my intentions are clear. all variables are dynamic and can change to any other values than what is specified in the image

for clarity, for a referenceVec of (0,1) and a target point of (3,4), and a self point of (25,45) - I would expect a final point of (25,4)

Thanks!

Describe the problem less abstractly. What actual practical problem do you have and what result are you trying to get?

point A represents self
target point represents target

reference vector represents camera.forward

you see the green line? it shows when the self gets past the position of the target. When the self gets past the position of the target, I want to get the position it would have, if it would be aligned with the target on the green line (and adjusted for current camera direction)

i think this explanation is pretty much the same as what i described, not sure if it changes much

My reason for asking is it sounds like an XY problem: XY problem - Wikipedia

It helps to just describe things in practical gameplay terms, rather than a bunch of abstract points. You’re still viewing it from a completely abstract lense. What kind of mechanic are you trying to implement? A camera system?

Because I don’t think the reference vector even matters here, and you are throwing yourself a red herring. It seems to just come down to positions.

it seems like something that can be resolved by math

the reference vector matters because if the reference vector is something simple like (0,1) or (1,0), the answer is very easy

pointA final is equal to (pointA.x, target.y)

super straight forward

but if the vector doesnt have a zero in one of its coordinates, now the result is not so straightforward

I still want to know in simple gameplay terms what you’re trying to accomplish, again.

Are you just trying to withdraw a camera so it’s inline with something?

Here is your answer - “when my observer moves past the target, the perspective looks bad, except if I assume the position as having not passed the target, now it looks good. I want the position as if the observer had not moved past the target”

I dont expect you to understand what I just said, this is just bogging down the thread, my current setup has thousands of lines of code and tons of moving parts, its not something that can be boiled down like this

I appreciate your attempt of XY’ing me since its standard procedure here but it wont lead anywhere, if you cant be bothered to help me with the math, thats OK, im not forcing you, but you wont find any way around it since I feel that I have outlined super well the specific problem I want to solve, and that it seems like you dont want to solve (and thats OK on your part)

It sounds like you just don’t want the camera to pass the target.

I think you can just use Unity’s existing Plane struct. It has a method, ClosestPointOnPlane: Unity - Scripting API: Plane.ClosestPointOnPlane

You make a plane at the point of the target, with the normal of the camera’s direction (or your reference direction), and using the camera’s position and that method, you have your ‘final point’.

To me it sounds like all you need is Vector3.ProjectOnPlane which does exactly what you want.

Just subtract your target point from your point A, project it with that method and add the target point back

Vector3 targetPoint;
Vector3 pointA;
Vector3 refVec;

Vector3 PointA_Final = Vector3.ProjectOnPlane(pointA - targetPoint, refVec) + targetPoint;