- Home /

# Calculate vector3 from angle

I'm currently making a camera, but I'm having trouble finding out a ventor3 from an angle.

I want the camera to be able to rotate around my character. I'm using mouseX and mouseY axes as input. Now, if I have an angle, how do I calculate the vector3 if it has to be x units away from the character.

**Answer** by Peter G
·
Jul 02, 2011 at 03:39 PM

You can do the trig if you want to (its really easy), but Unity has a bult in operator to do the math for you.

```
var distFromObj : float;
function SetVectorFromAngle (x : float, y : float, z : float) : Vector3 {
var rotation = Quaternion.Euler(x, y, z);
var forward = Vector3.forward * distFromObj;
return forward * rotation;
}
```

multiplying a quaternion by a vector rotates the vector by the quaternion. So for example:

```
var rotation = Quaternion.AngleAxis(90, Vector3.up);
var forward = Vector3.forward;
var right = forward * rotation;
```

A slight correction to your code: the Quaternion should come first in the multiplication. It throws an error if you try to put the Vector3 first. That little difference got me for a bit.

Hi sir, thanks for your reply to this question, it's helped me a lot. This must be a noob question but could you please explain for me why multiply a vector with a quaternion can get the direction of the other vector? I try to explain it myself but I have no idea. :( Thanks and regard Vince

**Answer** by Bunny83
·
Jul 02, 2011 at 03:55 PM

Well, the most straight forward way that came to my mind is simple trigonometry.

```
//C#
float radHeading = heading*Mathf.Deg2Rad;
float radPitch = pitch*Mathf.Deg2Rad;
Vector3 relDirection = new Vector3(Mathf.Cos(radHeading),Mathf.Sin(radHeading),0);
relDirection *= Mathf.Cos(radPitch);
relDirection.z = Mathf.Sin(radPitch);
// The camera position would be your target postion (the player's position) + relDirection*dist;
// You might need to invert one of the angles to match your setup (invert an angle will reverse the direction).
transform.position = target.position + relPosition * distance;
```

Another, more Unity like, way would be to place your camera into an empty GameObject and move the camera locally to (0,0,-distance). Now you can place the empty GO at the player position and just need to rotate it.

```
transform.rotation = Quaternion.AngleAxis(pitch,Vector3.right) * Quaternion.AngleAxis(heading,Vector3.up)
```

Something like that ;)

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