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.

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;

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 :wink: