how to add gaze input timer to trigger an action after gazing for 3secs?

蹲街弑〆低调 提交于 2019-12-06 07:38:32
Eugene Sia

Please see a similar question and answer here Use Gaze Input duration to select UI text in Google Cardboard

In summary, create a script to time the gaze, by cumulatively adding Time.deltaTime on each frame when the object is gazed at. When gaze time hits a pre-specified duration, trigger the button's OnClick event.

On the object, activate the script's gaze timing functions using event triggers Pointer Enter and Pointer Exit. See screenshot:

VR Camera usually contains a main camera and eye cameras (right and left). Since Main camera's center point will always be the center of the eyes of the user's point of view, you could use Raycast from its transform.position to its transform.forward and check whether it hits your object. Then simply add a timer which will call the action after it reaches the duration you have set.

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

    [SerializeField]
    float gazeDuration; // How long it should be gazed to trigger the action

    public Action OnGazeAction; // Your object's action after being gazed

    Collider gazeArea; // Your object's collider

    float timer; // Gaze timer

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

    public void Update () {
        RaycastHit hit;

        if (Physics.Raycast (cam.position, cam.forward, out hit, 1000f)) {
            if (hit.collider == gazeArea) {

                timer += Time.deltaTime;

                if (timer >= gazeDuration) {
                    if (OnGazeAction != null)
                        OnGazeAction ();
                }

            } else {
                timer = 0f;
            }
        } else {
            timer = 0f;
        }
    }
}

Hope you get the idea.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!