3
\$\begingroup\$

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;

private CardboardHead head;
// Use this for initialization
void Start()
{
    head = Camera.main.GetComponent<StereoController>().Head;
}

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

    }
    if (fingerCount > 0)
        transform.position += sub_speed * head.Gaze.direction;

}

}

\$\endgroup\$
  • \$\begingroup\$ It is not clear what exactly are you looking for. It means, what is your precise question here? \$\endgroup\$ – MAnd Dec 9 '15 at 2:21
  • \$\begingroup\$ 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. \$\endgroup\$ – exomni Dec 9 '15 at 2:50
  • \$\begingroup\$ 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? \$\endgroup\$ – MAnd Dec 9 '15 at 3:03
  • \$\begingroup\$ 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. \$\endgroup\$ – exomni Dec 9 '15 at 3:11
  • \$\begingroup\$ And yes, by smoothly I mean gradually; easing in and out of the movement. \$\endgroup\$ – exomni Dec 9 '15 at 3:23
0
\$\begingroup\$

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);
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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"); \$\endgroup\$ – exomni Dec 9 '15 at 4:10
  • \$\begingroup\$ 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. \$\endgroup\$ – exomni Dec 9 '15 at 4:33
  • \$\begingroup\$ Sure enough. After tweaking this bit of code, it is doing what I want it to. I thank you sincerely for your time. \$\endgroup\$ – exomni Dec 9 '15 at 5:00
  • \$\begingroup\$ @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. \$\endgroup\$ – MAnd Dec 9 '15 at 5:13
  • \$\begingroup\$ @exomni can you share what you wound up doing? \$\endgroup\$ – Kaolin Fire Feb 16 '16 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.