- Home /
Finding the direction from an angle in 3d space
So im trying to find a direction from an angle. Here is what i got so far.
// Vertical
Vector3 dir1 = new Vector3(0.0f, Mathf.Sin(Mathf.Deg2Rad * vertAngle), Mathf.Cos(Mathf.Deg2Rad * vertAngle));
// Horizontal
Vector3 dir2 = new Vector3(-Mathf.Sin(Mathf.Deg2Rad * horzAngle), 0.0f, Mathf.Cos(Mathf.Deg2Rad * horzAngle));
This will give me either a horizontal or vertical direction. However i want to combine these two directions into one. So i have for example 45 deg up and 90 deg horizontally. I tried adding and normalizing, but it dident really work.
Thx for showed interest! :)
I don't quite understand what kind of result you are looking for because... you have 2 values: horizontal (i.e. how much to the left) and vertical (i.e. how high) and based on this you try to deter$$anonymous$$e a direction in 3D space. I don't believe there's a "natural" way to deter$$anonymous$$e it that way because you'd also need a way or a value to deter$$anonymous$$e how "deep" (along z axis)
The result im looking for is a direction in 3D space based on two angles. So that i can change the angles independently.
So i want a "horizontal" axis to be always the same then i need to change the "vertical" axis dependent on player position.
i came up with something like this:
Vector3 dir3 = new Vector3( -$$anonymous$$athf.Sin($$anonymous$$athf.Deg2Rad horzAngle), $$anonymous$$athf.Sin($$anonymous$$athf.Deg2Rad vertAngle), $$anonymous$$athf.Cos($$anonymous$$athf.Deg2Rad * horzAngle) );
it works with the horizontal change, and also with the vertical; but only to 45deg up or down.
Problem is that the Z value needs to be used by both the horizontal and the vertical.
Answer by Bunny83 · Aug 01, 2015 at 07:15 PM
To convert polar coordinates to a cartesian coordinates you just need to do this:
public static Vector3 GetDirection(float aAzimuth, float aAltitude)
{
aAzimuth *= Mathf.Deg2Rad;
aAltitude *= Mathf.Deg2Rad;
float c = Mathf.Cos(aAltitude);
return new Vector3(Mathf.Sin(aAzimuth) * c, Mathf.Sin(aAltitude), Mathf.Cos(aAzimuth) * c);
}
The Azimuth (heading) specifies the angle around they y axis [-180°, 180°]. The Altitude is the elevation angle which is in the range [-90°,90°].
However you can also simply use Unity's Quaternion type like this:
var dir = Quaternion.Euler(azimuth, altitude, 0) * Vector3.forward;
Bunny you are right this is the more correct awnser! But i dident set the awnser as accepted he did so cant change it. Thx man!
I did set it as answer since you said it was the solution (It was originally a comment). I changed that.
I leave $$anonymous$$e though as I would think some people may find your question while their problem can be solved with cross product (semantic issue).
fafase thx for not removing yours. Still like to have it for reference.
Answer by fafase · Aug 01, 2015 at 06:41 PM
Are you looking for the cross product?
The result of the cross product of two vectors is another vector that is perpendicular to the plane on which the two vectors lie.
Based on the order of the parameter, the result is going up or down as shown on the picture.
Huh? I thought the question asks for something different ... If you create two vectors like stated in the question, if both angles are 0 both vectors will point in the same direction. The cross product would return a null vector in that case.
I don't see how the cross product has any use in this case ^^.
the cross product did work, i dident think there where any other solution.
Your answer
![](https://koobas.hobune.stream/wayback/20220611234017im_/https://answers.unity.com/themes/thub/images/avi.jpg)