- Home /

# How should I rotate a 2D unit vector by X degrees?

As the title says, I have a 2D unit vector, and I simply need to rotate it by X degrees. I've done a lot of googling, and couldn't really find what I needed (most similar problems I found were quite a bit more complex).

Also I'd be calling this up to 4 times per update, so I'm hoping for a solution that isn't horrible on performance.

Oh and X degrees is actually coming from the camera's Y rotation. So if there's a better way to offset the vector by the camera's Y rotation that involves not using degrees, I suppose that'd be just as useful. (And by better I mean more efficient.)

Tried multiplying my vector like the following as suggested online: `Quaternion.AngleAxis(Camera.main.transform.rotation.y, Vector3.forward) * new Vector2(Input.GetAxis("P1MoveHorizontal"), Input.GetAxis("P1MoveVertical"))`

Didn't make any difference. And

```
Quaternion.AngleAxis(Camera.main.transform.rotation.y, Vector3.forward) * new Vector2(Input.GetAxis("P1MoveHorizontal"), Input.GetAxis("P1MoveVertical"))
```

Gives me an error where Quaternion cannot multiply with a vector2.

Tried this:

```
float sin = Mathf.Sin(Camera.main.transform.rotation.y);
float cos = Mathf.Cos(Camera.main.transform.rotation.y);
float tx = Input.GetAxis("P1MoveHorizontal");
float ty = Input.GetAxis("P1MoveVertical");
Move(new Vector2((cos * tx) - (sin * ty), (cos * ty) + (sin * tx)));
```

Made a difference, but still not the correct directions.

I even tried convert the unit vector to degrees, adding the camera's rotation in degrees, and then converting it back into a vector:

```
float directionAngle = Mathf.Atan2(Input.GetAxis("P1MoveVertical"), Input.GetAxis("P1MoveHorizontal"));
directionAngle += Camera.main.transform.rotation.y;
Move(new Vector2(Mathf.Sin(directionAngle), Mathf.Cos(directionAngle)));
```

But EVEN THIS which I was certain would work but was hoping to avoid for inefficiency, is not working at all as expected. Without even inputting a direction my object moves around in circles and wanders off the scene.

Your first try, the one multiplying a quaternion with a vector should had work... Also I dont get any error when I multiply a quaternion by a vector2... What version of Unity are you using?

And if you want to rotate around the Y axis of the camera you should use transform.up and not forward. Also you talked about performance, so I suggest you to cache your camera transform

Thanks for the answer. I Tried `motherBoard.mover.Move(Quaternion.AngleAxis(Camera.main.transform.rotation.eulerAngles.y, Vector3.up) * new Vector2(Input.GetAxis("P1MoveHorizontal"), Input.GetAxis("P1MoveVertical")));`

and

```
motherBoard.mover.Move(Quaternion.AngleAxis(Camera.main.transform.rotation.eulerAngles.y, Camera.main.transform.up) * new Vector2(Input.GetAxis("P1MoveHorizontal"), Input.GetAxis("P1MoveVertical")));
```

But it made no difference, movement still occurs relative to world not the camera.

Also just to point out, I started using rotation.eulerAngles.y instead of rotation.y as, according to another user, rotation.y is something else..

**Answer** by PsychoDuckArcade
·
Jun 18, 2014 at 05:30 PM

Solved.

Converted angle from degrees to radians, since apparently sin and cos take radians despite using degrees in school, and then made sin negative since apparently the sign that sin returns isn't always consistent between math libraries.

```
float x = Input.GetAxis("P1MoveHorizontal");
float y = Input.GetAxis("P1MoveVertical");
float sinAngle = -Mathf.Sin(Mathf.Deg2Rad * Camera.main.transform.rotation.eulerAngles.y);
float cosAngle = Mathf.Cos(Mathf.Deg2Rad * Camera.main.transform.rotation.eulerAngles.y);
Move(new Vector2(x * cosAngle - y * sinAngle, y * cosAngle + x * sinAngle));
```

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

Set rotation based on 2 points problem 1 Answer

Rotate but keep direction 1 Answer

Wrong rotation while moving 1 Answer

rotate smooth toward vector defined by controls 1 Answer

C# Raycast from Object direction (z-axis) + another Vector3 1 Answer