# Finding the interesection of a direction vector and a position vector!

I have a direction vector and a position vector, i need to calculate a new position where they intersect.

Basically, i need |A| cos from this diagram :

It works using Mathf.Cos in unity, but unfortunately Mathf.Cos only goes from -1 to +1. So my position gets clamped which is not desired. How to solve this?

Youâ€™re not being precise, so itâ€™s hard to know what you mean. (Vectors represent points in (in this case) 2D space; they canâ€™t â€śintersectâ€ť though they might happen to be equal.)

The link you pointed to is illustrating a dot product. If thatâ€™s what you need, just use Vector2.Dot. Mathf.Cos goes from -1 to +1 because that is what cosine, the mathematical function, really does. If you understand what cosine means, youâ€™ll see that itâ€™s impossible for it to have any other value. So I canâ€™t imagine what you want from it.

So! Why donâ€™t you back up and explain what it is youâ€™re trying to do?

I know what a vector is
In this case itâ€™s 3D, but whatever.
What I mean by intersection is if you have a direction vector, you make an imaginary perpendicular line from a position vector and end up with a new position vector. Thatâ€™s what I need, Iâ€™m not sure how to explain it any other way =/

The dot product gives me even more problems when itâ€™s limited to 0 to 1.
This is a fairly common thing to calculate so Iâ€™m probably just explaining it in a retarded way.

OK, so far we have: â€śyou have a direction vector, you make an imaginary perpendicular line from a position vector and end up with a new position vector.â€ť

This imaginary line: itâ€™s perpendicular to what? The position vector, or the direction vector? And then what exactly do you do with it to get a new position vector? There are obviously an infinite number of points on any lineâ€¦ which of them do you want?

I see two options: either put more effort into explaining what you want mathematically (which will mean being actually precise with your terms, and possibly drawing a picture); or back up and explain what it is youâ€™re trying to actually accomplish.

I guarantee you are not trying to accomplish picking a second position vector on some line thatâ€™s perpendicular to something in some way related to some other direction vector. What youâ€™re trying to do is fire bullets at an angle to the direction youâ€™re facing, or find where something dropped from your spaceship would hit the terrain, or something like that. And you believe this ill-defined mathematical operation will do it for you (and you might be right). But if you explain to us what it is youâ€™re trying to accomplish, weâ€™ll either be able to understand the math you canâ€™t explain, or suggest a completely different way to approach it.

You need two directions, not a position. If you must use the position then you need an origin point to get the direction to that position so you can get the dot product from two directions.

The direction can be any large value, itâ€™s an offset in spacial units from the origin (0,0,0 in math) and the magnitude of the vector is the length. Normally directions are normalized, eg locked to a total magnitude of 1, so all values of that direction value added up will total 1.

The point of the dot product is to basically say â€śif direction A is the total opposite of B, then iâ€™ll give you -1 back, if they are pointing the exact same direction, Iâ€™ll give you 1 back - but if say, A points UP and B points Right then Iâ€™ll give you back 0â€ť.

Itâ€™s basically a normalized percentage of how far along the A direction you have to go on it in order to get to the intersection on B.

I think you need to understand what the math is going to give you back, then youâ€™ll better understand how to get the information it needs.

Just had a thought: could it be youâ€™re trying to describe the vector projection of A onto B (where A and B are Vector2Ds)?

In that case, itâ€™s just Vector2.Dot(A, B) if B is normalized (has length 1). If itâ€™s not normalized, then use Vector2.Dot(A, B.normalized).

Thanks for the totally beginner trigonometry examples
You got it right in the end though, itâ€™s just a projection. I just didnâ€™t know what it was called, sorry about the terrible explanation ( although Cos did actually got me pretty close ).

Or you can use Vector3.Projection which does exactly what I want

Glad you got it figured out. I realize now you never even told us if you were working in 2D or 3Dâ€¦ I had assumed 2D, or yeah, I would have suggested Vector3.Project. (Of course Vector3.Project will work for 2D too, with appropriate typecastingâ€¦)

What version of Unity are you using? If itâ€™s the most current, you may need Unity Pro. Oh, wait, Iâ€™m seeing that those weasels at Unity Co. are trying to cover up their incompetence by claiming cosin is only supposed to go from -1 to 1, otherwise we can look in the Asset Store. Typical.