Search code examples
androidunity-game-enginegoogle-cardboardvirtual-reality

Google Cardboard demo in Unity3d: How to center a 3D object in your field of vision by double tapping touchscreen?


I am trying to modify the Google Cardboard SDK demo in Unity3d (version 4.6.4f1) for an Android device.

The following description is based on this -picture- I made of what I am trying to accomplish.

  1. At the start of the game a stationary red cube will be in center of vision.
  2. The user can look around in 360 degrees and for example he tilts his/her head up to look at the clouds.
  3. When the user double taps the touch screen the red cube will transport to the center of the new field of vision facing the user.

Thanks!


Solution

  • You need to use the CardboardHead part of the CardboardMain prefab. In your cube script, leave a public GameObject reference and set it to CardboardHead in the editor.

    Next, in your double tap handling function, set your position accordingly to the forward vector of the head.

    If you want the cube (or any other 3D object) to face the player, you need to use transform.LookAt()

    It will probably look something like this:

    public GameObject cardboardHead;
    public float distanceFromCamera;
    
    public void doubleTapped()
    {
      Vector3 newPosition = cardboardHead.transform.position + (cardboardHead.transform.forward * distanceFromCamera);
      transform.position = newPosition;
      transform.LookAt(cardboardHead.transform);
    }