# With Cardboard, reorient player to direction of gaze

This may be a little hard to follow. I have a script set up in Unity, and attached to my CardboardMain object that allows a user to press the button on a Cardboard headset to move in the direction of their gaze. They move only when the button is pressed. Since this game is a seated experience, I want the "virtual seat" to gradually reorient itself in the direction of the user's gaze, in the same way a pilot's seat, and plane would reorient with a turn. This way, whichever direction the user is traveling, it always becomes forward in the game, and reality. The idea is that the user's body can remain facing in it's starting orientation, while being able to freely look - and travel around. I'm new to C# and Unity, but I've made enough progress on this project that I cannot give up.

I'm using head.Gaze.direction to drive movement, and it looks like rotating CarboardMain will rotate everything inside of it. The hierarchy looks like this:

CardboardMain > Head > Main Camera

So, I think I could apply the needed rotation to the CardboardMain object, right?

This is the code so far, but I'm not sure where to go from here. My first thought was to rotate the scene, but that seems like it could complicate things as the project continues to develop.

public class movementScript : MonoBehaviour {
public float sub_speed = 0.03f;

// Use this for initialization
void Start()
{
}

void Update() {
int fingerCount = 0;
foreach (Touch touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;

}
if (fingerCount > 0)

}


}

• It is not clear what exactly are you looking for. It means, what is your precise question here? – MAnd Dec 9 '15 at 2:21
• How can I gradually rotate the CardboardMain parent object toward the point given by head.Gaze.direction? That's what it boils down to right now. – exomni Dec 9 '15 at 2:50
• I see. Just checking: so 1) is CardboardMain a GameObject? 2) is head.Gaze.direction really a direction or the position toward which CardboardMain should look at? 3) by gradually do you mean rotating smoothly? – MAnd Dec 9 '15 at 3:03
• CardboardMain is a game object, and it is the parent object of Head, which rotates inside of it. When I log head.Gaze.direction to the console at start, I get (0.0, 0.0, -1.0), which leads me to believe it is a direction rather than a position. – exomni Dec 9 '15 at 3:11
• And yes, by smoothly I mean gradually; easing in and out of the movement. – exomni Dec 9 '15 at 3:23

So, I think I could apply the needed rotation to the CardboardMain object, right?

I am not sure if by doing that you will achieve what you were thinking.

But if that is the case, by your comments your current problem boils down to a mere issue of rotating any GameObject towards a given specific direction.

If that is the case, I would suggest you try:

void Update() {
float currentstep = speed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(CardboardMain.transform.forward,  head.Gaze.direction, currentstep, 0.0F);
CardboardMain.transform.rotation = Quaternion.LookRotation(newDirection);
}

• I get the error "The name CardboardMain does not exist in the current context". I tried adding a tag ("Cmain") to it, and used GameObject CardboardMain = GameObject.FindGameObjectWithTag("Cmain"); – exomni Dec 9 '15 at 4:10
• I removed CardboardMain from the transforms (the transforms should apply to the object the script is attached to), and I was able to get this script to run without errors, but it's not quite doing what I expected. I might be able to tinker with it, though. – exomni Dec 9 '15 at 4:33
• Sure enough. After tweaking this bit of code, it is doing what I want it to. I thank you sincerely for your time. – exomni Dec 9 '15 at 5:00
• @exomni You'r certainly welcome. Sorry that I didn't see your comments in time so to help more, but I'm glad you were able to improve it further to reach your specific need. – MAnd Dec 9 '15 at 5:13
• @exomni can you share what you wound up doing? – Kaolin Fire Feb 16 '16 at 22:39