# Get camera rotation constrained to left/right (ie. yaw)?

I’m trying to “project” a GUI/HUD screen (canvas in world-space) in front of the user in a VR game, but would want the canvas to be consistently upright/vertical (ie. I wouldn’t want to project it at an angle if head is tilted sideways; nor would I want it to appear on ceiling or floor if user is looking up or down)

In other words, I need just the normal/projection forward from the left/right turn of the head, ignoring the other rotations (ie. I need just yaw, while ignoring pitch and roll of the camera)

What is the best way to get this angle (and a transformation forward some distance from that angle)?

I’m assuming I can convert the rotation from Quaternion to Euler to check the rotation angle, but then how do I place the Transform “forward” from that Euler angle accordingly?

Thanks!

using UnityEngine;
using System.Collections;

public class swiperotation :TouchLogic
{
public float rotateSpeed = 100.0f;

``````private float pitch = 0.0f,
yaw = 0.0f;

void onTouchMoveAnywhere ()
{
pitch -= Input.GetTouch (0).deltaPosition.y = * rotateSpeed * invertPitch * Time.deltaTime;
yaw += Input.GetTouch (0).deltaPosition.x = * rotateSpeed * invertPitch * Time.deltaTime;

//do the rotations of our camera
this.transform.eulerAngles = new Vector3 (pitch, yaw, 0.0f);
}
``````

}

If this is what you want, all you really need to do, is take the standard euler angles and use only the Y axis, this would give you your yaw.

``````float angle = transform.rotation.eulerAngles.y;
``````

Now that we know this angle, we can calculate the direction:

``````Vector3 direction = new Vector3(
0f,
);
``````

By default this will already be a unit vector because of the use of sine and cosine, so no need to normalize.

Now, to find the normal you need to render the UI, just invert the direction:

``````Vector3 normal = Vector3.zero - direction;
``````

And to find the location to place the center of the UI:

``````Vector3 position = direction * distance;
``````

Where distance is the number of units you want it far away.

If this renders on the side instead of in front of you, then you need to flip the sin and cos. If the ui renders behind you at all times, then you need to add 180 to the angle or negate the x and z values of the direction vector (preferable). If the position of the UI moves based on if you’re facing on Z or X axis, then one of the two (either x or z) of the direction need to be negated.

EDIT!

Alternatively to the sin and cos method, you can also take the forward vector and set it’s y to 0, then normalize:

``````Vector3 direction = yourCamera.transform.forward;
direction.y = 0;
direction.Normalize();
``````

This may be faster (no trig, but still a quare root so…) and should still give the same results.