- Home /

# How to get the normal of a line in relation to a specific point in 3D?

So I´ve got three points: 2 of the line and another independent one. These are aligned and I want to get the perpendicular vector that goes from the third point to the line (or viceversa). However, I only care about direction, not magnitude or position. The problem is that is not as easy as in 2D where you just invert the x´s and y´s and change the sign of the second. In 3D there are infinite solutions, but I want the solution where the thee points are on the same plane.

What do you mean with all 3 points must be on the same plane? 3 points will always define a plane in space. Do you want a vector from the third independent point towards the closes point on the line? This vector will always have a 90° angle from the line to the third point.

**Answer** by Fuzzel_
·
Jun 04, 2020 at 12:00 AM

To get the closes point (P2) on the line (P0,P1) from a third point (Q) not on the line (which will always be at an 90° angle from the line) you first build a *normalized vector* that describes the line, build the vector from P0 to Q and then calculate the *dot (scalar) product* between the two. This gives you the "amount of steps" along the line vector starting from P0 to reach the point (P4) on the line.

```
Vector3 lineDirection = (P1 - P0).normalized;
Vector3 P0toQ = Q - P0;
float dot = Vector3.Dot(P0toQ , lineDirection);
Vector3 P2 = P0 + lineDirection * dot;
```

Thanks, but this is confusing me. What is exactly P0? This only has four points including the result: P1 and P2, which are the line, Q, the independent one, and P3 that is the point on the line that forms the perpendicular line along with Q. Answering your question, 3 points don´t necessarily form a plane. I mean, two are from the line and the other is from a different figure. It was a way of visualizing it. Anyway, I actually want the normal direction, not the closest point on the line. I know that I can get that by Q - P3, but I want a more direct method if possible, specially since I don´t need that vector to go from P3 to Q, I just need its direction. Actually, I wanted to see if the Q point, along with other new points, were on the same side of the line. That´s also what I meant by "all 3 points are on the same plane", because a line doesn´t divide the space in two. But turning it into a plane was more complicated, I think. So it´s like checking if the Q points are on one side of this line or the other. Again, do you know a simpler method for getting this?

Sorry during writing my answer I switched up my "point counter" I corrected it.

To answer the question: If my school math knowledge doesn't betray me, there is no simpler way to get the direction from Q towards P3. You have to do

```
(P3 - Q).normalized;
```

About checking if points are on "one side" of the line, you might consider actually using a plane for this. (Think about not only point on the left/right of the line, but also about point above or below. Where are they in comparison to the other points? On the left or right?

If you define a plane as a point (P) and a normal vector (N), you can then very simply find out on which side of the plane a point is using the dot product:

Calculate the dot product between N and (X - P).normalized for any point X you want to check. If it is positive, it's on the side the normal vector is pointing at. If it's negative it's on the other side. And if it is exactly 0, it is on the plane.

Here is an illustration:

I think that I explained wrong. The solution of the plane works, but I want simplicity. This is going to be calculated thousands of times, and by intuition I assumed that getting the side of a line was easier than getting the side of a plane. You said something about being below or above besides left or right, so I think you thought points could be anywhere, but no. Points can be anywhere ONLY on the plane. And am talking about a different plane. I know this sounds confusing, but is simple. There´s the plane where everything stands on and the plane perpendicular to it (the one that you drew). So, knowing that all of the points are aligned, we only need to know "left or right" in relation to the line on that plane. Again, is there a way of doing this simple? By the way, what´s the point of normalizing everything? Wouldn´t it work otherwise? I was actually using a similar method for other side detection, but was more centered around using an imaginary line with the normal direction and checking the sign of the multiplier.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Finding a specific point on the edge of a triangle 1 Answer

A script to cut planes from where you mouseDown to mouseUp... 0 Answers

Getting perpendicular direction vector from surface normal 2 Answers

How to create imaginary perpendicular lines for use in camera targeting in an 3d environment? 0 Answers