# Getting Pitch and Yaw values from a direction vector... and back

Hi, sorry guys I know this kind of stuff is plastered everywhere, but because of that there is to much info and most of it not relevant making it hard to find out what I want to know…

If I have two 3d points, say… myPos and targetPos and I subtract myPos from targetPos I get a direction vector between the two, ok cool… the magnitude is the length or distance between the two but that’s irrelevant so lets turn it into a unit vector by normalizing it…

I know I can swap a direction vector back and forth between local and world coords using transform.TransformDirection and transform.InverseTransformDirection

But how do I get pitch and yaw scalar’s from this direction vector so I can do things like report to the player with some glee, something like… Target 20 degrees up or 45 degrees right (from the players orientation of course)
I know I can use Vector3.Angle to get a single angle in degrees but I really need it broken down into pitch and yaw components.

And then following on from that… If all I knew was the position of something (local or world) and some pitch and yaw scalars how do I get back to a unit vector direction?

I have been playing around with this for ages now and just spent all evening searching the web… and all I am doing is getting myself move confused and It seems what I am trying to do is simple enough.

So far this is what I have got…

Vector3 DirectionFromPitchAndYaw(float pitch, float yaw)
{
return new Vector3(Mathf.Cos(yaw) * Mathf.Cos(pitch), Mathf.Sin(yaw), Mathf.Cos(yaw) * Mathf.Sin(pitch));
}

float PitchFromDirection(Vector3 dir)
{
return Mathf.Asin(dir.x / Mathf.Cos(Mathf.Asin(dir.y)));
}
float YawFromDirection(Vector3 dir)
{
return Mathf.Asin(dir.y);
}

float PitchFromDirection2(Vector3 dir)
{
return Mathf.Sqrt(Mathf.Pow(dir.x, 2) + Mathf.Pow(dir.y, 2)) / dir.z;
}
float YawFromDirection2(Vector3 dir)
{
return dir.x / -dir.y;
}

However neither of these seem to work… If i draw a debug or gizmo ray from a GO given a dir and then convert it into pitch and yaw values… I cannot get back to the same direction ray again.?

Ok I am nearly there… It is giving me the right yaw and pitch angles now, I just can’t quite seam to get them to go back to the correct direction vector… the horizontal is ok but the vertical is off.

I have a simple project scene with an example
If someone could take a quick look and point out where I am going wrong I would be most grateful.