# Convert a 360 bearing into a Vector 2

Hi there, I’m wondering how I can convert a bearing into a Vector2. I assume it should be fairly straight forward using Sin and Cos, but my maths isn’t very good.
The angle I would be passing into the functions works in the usual way a bearing works (i.e. clockwise starting from a 0 point at Vector2.up), and I need to get a Vector2 out with a magnitude of 1.

Any ideas?

Thanks

P.S. here’s the code I have so far for it.

``````	Vector2 GetCameraNorth ()
{
//Find the forward facing vector of the camera (relative to the camera position) if it were on a 2d plane
Vector2 CameraNorth = new Vector2 (Camera.main.transform.forward.x, Camera.main.transform.forward.z);
Vector2 Offset = CameraNorth - new Vector2(Camera.main.transform.position.x, Camera.main.transform.position.z);
return Offset.normalized;
}

Vector3 GetMoveDirection ()
{
//Get the player input as a 2d vector
Vector2 MovementInput = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"));
Vector2 NormalizedMovementInput = MovementInput.normalized;

//With both the Camera vector and the Input movement vector, we can calculate our desired direction by adding together the angles of both, and making a new vector using those angles.
//To deal with the issue of angles in unity only being calculable between 0-180 (rather than 360) we have to do a little extra work to turn the angles into 360 degree bearings.

float camBearing;
float inputBearing;
float desiredDirectionBearing;

if (GetCameraNorth ().x < 0) {
camBearing = 360 - Vector2.Angle (Vector2.up, GetCameraNorth ());
} else {
camBearing = Vector2.Angle (Vector2.up, GetCameraNorth ());
}
if (NormalizedMovementInput.x < 0) {
inputBearing = 360 - Vector2.Angle (Vector2.up, NormalizedMovementInput);
} else {
inputBearing = Vector2.Angle (Vector2.up, NormalizedMovementInput);
}

desiredDirectionBearing = camBearing + inputBearing;

}
``````

In the above code, I would then be plugging in the float variable ‘desiredDirectionBearing’ to generate a Vector2 using it.

The basic idea being:

• Turn Vector ‘Av’ into Float Angle ‘Af’
• Turn Vector ‘Bv’ into Float Angle ‘Bf’
• Make sure that both ‘Af’ and ‘Bf’ are 0-360 bearings rather than a 0-180 angles.
• ‘Af’ + ‘Bf’ = ‘Cf’
• Turn Float Angle ‘Cf’ into Vector ‘Cv’ (this here being the step I dont know how to do).

Vector2 unitLengthVec = new Vector2(cos(angle), sin(angle) * vectorLength;