简体   繁体   中英

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

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:

using UnityEngine;
using System.Collections;

public class lookAtMe : MonoBehaviour {

    private CardboardHead head;

    private Vector3 offset;
    public GameObject scrimshaw;

    // Use this for initialization
    void Start () {
        head = Camera.main.GetComponent<StereoController>().Head;
        scrimshaw = GameObject.FindGameObjectWithTag ("Scrimshaw");
    }

    // Update at end of frame
    void LateUpdate () {

        // head.transform.position = the positon of the head on the plane
        // head.Gaze.direction = positon of where the head is looking
        offset = head.Gaze.direction + head.transform.position;

        scrimshaw.transform.position = scrimshaw.transform.position + offset;
    }
}

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.

Turns out no script is needed. The necessary step to achieve the functionality is to disable both Track Rotation and Track Position from the Head child of CardboardMain , which can be done in the editor. Without tracking the camera stays static.

But since the CardboardHead script is applied to the object itself, all actual tracking transformations get applied to that object directly.

如果要将任何对象固定在摄像机前面,请将其设置为摄像机的子对象。您可以在编辑器中或运行时执行此操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM