Search code examples
c#transformunity-game-enginegoogle-cardboard

Moving a cardboard around


Quick disclaimer: I am not a very advanced C# user, being more accustomed to languages like python, so i apologise if the answer is right in front of me.

I've been making a little game for the Google Cardboard, Using the demo scene as a base. I've got some code that checks for a "Trigger Pull" and then should translate the Cardboard cameras up by 10 units.

            //Checks For Magnet Trigger
        if (Cardboard.SDK.Triggered)
        {
            //moves player up at a rate of 10u/s

            transform.Translate(Vector3.up * 10);
            Debug.Log("Triggered_Head");
            //Tell Cardboard to maintain new position
        }

Currently this works really well for detecting the magnet pull, and it does translate the cardboard. The problem is that almost immediately after translation the cardboard gets teleported back to the base position.

Currently i have this code inserted into the UpdateHead() method (?) from CardboardHead.cs, like so:

      // Compute new head pose.
  private void UpdateHead() {
    if (updated) {  // Only one update per frame, please.
      return;
    }
    updated = true;
    Cardboard.SDK.UpdateState();

    if (trackRotation) {
      var rot = Cardboard.SDK.HeadPose.Orientation;
      if (target == null) {
        transform.localRotation = rot;
      } else {
        transform.rotation = target.rotation * rot;
      }
    }

    if (trackPosition) {
      Vector3 pos = Cardboard.SDK.HeadPose.Position;
      if (target == null) {
        transform.localPosition = pos;
          } 
      else {
        transform.position = target.position + target.rotation * pos;
      }
            //Checks For Magnet Trigger
            if (Cardboard.SDK.Triggered)
            {
                //moves player up at a rate of 10u/s

                transform.Translate(Vector3.up * 10);
                Debug.Log("Triggered_Head");
                //Tell Cardboard to maintain new position
            }
        }

    if (OnHeadUpdated != null) {
      OnHeadUpdated(gameObject);
    }
  }

Doing this does everything right, but the location is reverted almost instantly (in the next frame i assume). So my question is: How do i make the transform stick, and is there a better way for me to handle this?


Solution

  • Alright, i figured out how to handle movement, and I'm posting the solution here for anyone in the future who can't figure it out. Quick note: I did update to the latest version of the SDK, although that shouldn't make a difference, except for naming.

    So, Step 1:

    Create an object to use as your controller. I just used one of the prototyping cubes from an asset pack, but whatever will work (empty objects would be best).

    Place this object more or less in the center-point of the two cameras.

    Parent your GvrMain object to the controller object (by dragging it onto the controller object) as well as any other components you want to move with the player (Guns, GUI's, etc)

    Step 2:

    Create a script for the controller object.

    In the Update() method, add the lines:

    if (GvrViewer.Instance.Triggered)
    {
    transform.Translate(Vector3.up)
    }
    

    for older versions replace GvrViewer.Instance with Cardboard.SDK

    Customise your movement to your liking, any normal unity functions should work.

    Some shortcomings:

    • You have to repeatedly press the trigger, using while() seems to break unity. This seems pretty easy to fix

    • The code snippet instantly translates up by 1. Not sure how to do it as a steady acceleration.

    Hopefully this helps anyone who had my problem.