# How to calculate vector perpendicular to a direction vector?

Hello,

I am trying to get the direction of the vector perpendicular to the direction vector of 2 points(A,B) illustrated below. I understand a cross product requires a reference vector like transform.right to work, but I get weird behavior when the direction(dir) vector is equal to the reference vector transform.right: the rigidbody2d I am moving with it just wobbles back and forth.

P.S. using the rigidbodies transform.right would be good, but it's always rotating, so that can't work.

Any physics-related suggestions are appreciated!

```
void Rotate(Rigidbody2D rb)
{
Vector3 dir = rb.transform.position - transform.position;
dir.Normalize();
Vector3 side = Vector3.Cross(dir, -transform.right);
Vector3 cross = Vector3.Cross(dir, side);
rb.AddForce(cross.normalized * speed);
}
```

**Answer** by Winterblood
·
Jun 15, 2016 at 07:46 PM

If it's in 2D you can just swap the components and negate one:

```
blueVector.x = redVector.y;
blueVector.y = -redVector.x;
```

(You'll probably then want to normalise it so it's a consistent magnitude)

FWIW, the cross product will give you a vector perpendicular to both the input vectors, so it's useless in 2D.

[EDIT] BTW, if you keep adding that tangential velocity it will spiral rapidly outwards, even with damping. I'm guessing you might want it to orbit, in which case you'll need a "gravity" force to stop it flying off.

Thanks, WinterBlood. Are you effectively rotating the vector by doing that- i'm not sure? If not, I managed to fix it by using Quaternion.AngleAxis to rotate the direction vector 90 degrees.

That's the same as cross product basically.

the cross product will give you a vector perpendicular to both the input vectors, so it's useless in 2D.

Awww. you were so close :) this "problem" is in fact the solution.

Cross product works perfectly for 2D if you just use Vector3.forward (or 'back') as the reference vector (it can never have the same direction as your other 2D vector so you don't have that problem).

The 2 vectors you input to get a cross product define the plane to which the result is a normal for. If the 2 input vectors are the same or one of them is zero, they don't define a plane so there is no normal either.

P.S. I don't want to encourage anyone to prematurely optimize working code, but cross product is probably 20 times faster than quaternions for this performance wise

**Answer** by unity_ioeaHQCQ0I7G2A
·
Sep 03, 2019 at 05:56 AM

In 2D: direction vector = (x,y) => normal vector = (-y,x) or (y,-x)

In 3D: direction vector = (x,y,z) => normal vector (in plane y, y stay) = (-z,y,x) or (z,y,-x)

### 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

Predicting the hit point on X axis, based on vector direction 1 Answer

Draw simple 2D shapes like circles, rectangles, paths etc... 0 Answers

How to get Vector3.Distance from the perimeter of a circle? 1 Answer

How do you draw 2D circles and primitives 5 Answers

Keep object within circle on plane 0 Answers