Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Keep object “fixed” in front of camera (Google Cardboard + Unity)

Discussion in 'AR/VR (XR) Discussion' started by digark, Dec 14, 2015.

  1. digark

    digark

    Joined:
    Dec 14, 2015
    Posts:
    2
    I am new to Unity (and VR) and am trying to set up a small example in Google Cardboard where I have a model that turns when the user turns her head - similar to the mask demo in the Google Cardboard App. So when the user looks up, the model rotates up, when looking left, the model rotates left etc.

    My scene currently has a CardboardMain and my 3D model. I have attached the Cardboard Head script to my model, which now rotates correctly with the head movement. What is missing is having the object remain in front of the camera.

    In order to achieve that functionality, I created a script which I attached to my 3D Model. The script looks like this:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class lookAtMe : MonoBehaviour {
    5.     privateCardboardHead head;
    6.  
    7.     privateVector3 offset;
    8.     publicGameObject scrimshaw;
    9.  
    10.    voidStart()  {
    11.          head =Camera.main.GetComponent<StereoController>().Head;
    12.          scrimshaw =GameObject.FindGameObjectWithTag("Scrimshaw");
    13.    }
    14.  
    15.    voidLateUpdate() {
    16.  
    17.       // head.transform.position = the positon of the head on the plane
    18.       // head.Gaze.direction = positon of where the head is looking
    19.       offset = head.Gaze.direction + head.transform.position;
    20.  
    21.       scrimshaw.transform.position = scrimshaw.transform.position + offset;
    22.    }
    23. }
    However, the position of my model does not change. I was under the impression that if I provide transform.position with a new vector 3 it will move the object accordingly. How else could it be done?

    I did try applying transform.LookAt (target) at the Main Camera instance, setting the target to the model. While this method does work, it is way to jerky to be usable.

    I appreciate any suggestions.
     
  2. Hashton

    Hashton

    Joined:
    Nov 10, 2015
    Posts:
    25
    On the cardboardmain object go into head, then main camera. Once on the main camera simply move the z position into a suitable negative position for you. This is how I have done this previously. Hope this helps.
     
  3. digark

    digark

    Joined:
    Dec 14, 2015
    Posts:
    2
    Thanks for the suggestion.

    Actually what I needed to do was to disable the Position and Rotation tracking in the _Head_ child of _CardBoardMain_. This was sufficient to keep the object in focus. Since the model has the _Cardboard_ Head script attached, all the tracking gets applied only to the model.