zakjma
zakjma

Reputation: 2110

Google Cardboard - How to detect focus on an object?

I try to create a VR scene in unity using google cardboard sdk. I add a cube and CardboardMain.prefab to scene. There is an example scene that detect focus on cube. Its hierarchy view is :

enter image description here

I don't know how to add GUIReticle object or prefab like as the image.

How can I detect focus on an object?

Upvotes: 0

Views: 1092

Answers (1)

dkrprasetya
dkrprasetya

Reputation: 114

Actually you could make the script by your own and it is quite simple.

You could detect whether the user is looking at your object or not by using RayCast from the Main Camera. If the RayCast hit your object, then it is being focused on.

For example:

using UnityEngine;
using System;

[RequireComponent(typeof(Collider))]
public class LookableObject : MonoBehaviour {

    [SerializeField]
    Transform cam; // This is the main camera.
    // You can alternately use Camera.main if you've tagged it as MainCamera

    bool focus; // True if focused

    Collider gazeArea; // Your object's collider

    public void Start () {
        gazeArea = GetComponent<Collider> ();
    }

    public void Update () {
        RaycastHit hit;

        if (Physics.Raycast (cam.position, cam.forward, out hit, 1000f)) {
            focus = (hit.collider == gazeArea);    
        } else {
            focus = false;
        }
    }
}

Edit: This is just an example. You probably would want to make a script to do the Raycast only just once instead of doing the Raycast on each of your object over and over again to make your project runs faster.

Upvotes: 2

Related Questions