3

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!

Dave Wong
  • 33
  • 4
  • Thanks smd and K.L. The answers you both provided worked perfect! However, I added: transform.rotation = Camera.main.transform.rotation; to the end of the code so the cube will face the camera. – Dave Wong Apr 10 '15 at 06:08
  • 1
    I also wanted to mention that I consider myself a complete noob at coding and Unity. I still get eye shock when I look at a page of demo code. But after this little success I'm excited to tackle the next challenge. Thx again to you both! – Dave Wong Apr 10 '15 at 06:08
  • I added to my answer a line of code that makes the cube face the player like you wanted. Note that it is a better solution than the one you mention in your comment. In your solution, the cube looks the same direction as the player. If it were another mesh, say a humanoid robot, it would have its back turned to the player, instead of its face. If you make the robot LookAt the player, he will face him correctly :) – K.L. Apr 10 '15 at 08:06
  • +1 to using LookAt for the general case. Also, note that in the special case where the object is a child of the CardboardHead, then you can simplify all of this to just "localTransform.z = distance". For example, see the current SDK's demo scene. The gaze cursor is set up this way. – smd Apr 12 '15 at 16:09

2 Answers2

2

Here's one way to do it, using CardboardHead's Gaze property which returns a Ray:

// Places the game object at distance meters from the user, in the
// direction they are looking.
public void MoveToView(float distance)
{
    CardboardHead head = Camera.main.GetComponent<StereoController>().Head;
    transform.position = head.Gaze.GetPoint(distance);
}

(Assumes there's a StereoController on the main camera. You can check for null if that might not be the case.)

smd
  • 829
  • 2
  • 6
  • 7
1

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);
}
K.L.
  • 2,419
  • 1
  • 21
  • 26