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

gaze interaction for cardboard

Discussion in 'VR' started by shayCohen007, Oct 10, 2018.

  1. shayCohen007

    shayCohen007

    Joined:
    Oct 7, 2018
    Posts:
    5
    hey,
    i'm beginner in unity and i made very basic android app for cardboard.
    i want to put picture that when i gaze for one second the picture will change.
    i would like to know how to do it in the easiest way...
    i search a tutorial that show this specific function but i did'nt find
    can someone help me please...
     
  2. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    Hi,

    You could use raycasting.
    Try putting this script on your picture gameobject.
    Note: it's untested.

    Code (CSharp):
    1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    2. //   ____ ___ _     ___ ____ ___  _   _   ____  ____   ___ ___ ____
    3. //  / ___|_ _| |   |_ _/ ___/ _ \| \ | | |  _ \|  _ \ / _ \_ _|  _ \
    4. //  \___ \| || |    | | |  | | | |  \| | | | | | |_) | | | | || | | |
    5. //   ___) | || |___ | | |__| |_| | |\  | | |_| |  _ <| |_| | || |_| |
    6. //  |____/___|_____|___\____\___/|_| \_| |____/|_| \_\\___/___|____/
    7. //
    8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    9. using UnityEngine;
    10.  
    11. //#############################################################################################################################
    12. //    CLASS: gaze
    13. //#############################################################################################################################
    14. public class gaze : MonoBehaviour
    15. {
    16.     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    17.     //    CONSTANTS
    18.     const float K_F_RAYLEN = 1000.0f;
    19.     const float K_F_GAZE_TIME = 1.0f;
    20.     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    21.     //    AWAKE
    22.     Collider cCollider;
    23.     private void Awake()
    24.     {
    25.         cCollider = this.gameObject.AddComponent<MeshCollider>();
    26.         if (!cCollider)
    27.         {
    28.             UnityEngine.Debug.LogWarning("CANT ADD COLLIDER TO " + this.name);
    29.         }
    30.     }
    31.     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    32.     //    UPDATE
    33.     Ray cRay = new Ray();
    34.     RaycastHit cHit = new RaycastHit();
    35.     bool bHitting;
    36.     bool bTriggered;
    37.     float fTimeGazeStart;
    38.     void Update()
    39.     {
    40.         if (!cCollider)
    41.         {
    42.             return;
    43.         }
    44.         if (!Camera.main)
    45.         {
    46.             return;
    47.         }
    48.         //  SETUP RAY
    49.         cRay.origin = Camera.main.transform.position;
    50.         cRay.direction = Camera.main.transform.forward;
    51.         //  RAYCAST
    52.         if (Physics.Raycast(cRay, out cHit, K_F_RAYLEN))
    53.         {
    54.             //  GAZING AT THIS?
    55.             if (cHit.collider == cCollider)
    56.             {
    57.                 //  GAZE STARTED
    58.                 if (!bHitting)
    59.                 {
    60.                     bHitting = true;
    61.                     fTimeGazeStart = Time.time;
    62.                 }
    63.                 else if (!bTriggered)
    64.                 {
    65.                     if ((Time.time - fTimeGazeStart) >= K_F_GAZE_TIME)
    66.                     {
    67.                         bTriggered = true;
    68.                         DoTriggerAction();
    69.                     }
    70.                 }
    71.             }
    72.             //  GAZE STOPPED BUT STILL ON SOME OTHER COLLIDER
    73.             else if (bHitting)
    74.             {
    75.                 bTriggered = false;
    76.                 bHitting = false;
    77.             }
    78.         }
    79.         //  GAZE STOPPED NOT ON ANY COLLIDER
    80.         else if (bHitting)
    81.         {
    82.             bTriggered = false;
    83.             bHitting = false;
    84.         }
    85.     }
    86.     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    87.     //    YOUR ACTION HERE
    88.     private void DoTriggerAction()
    89.     {
    90.         UnityEngine.Debug.Log("YOU GAZED AT " + this.name + " FOR " + K_F_GAZE_TIME + " SECONDS");
    91.         //  YOU CAN CHANGE YOUR PICTURE HERE, BY CHANGING MATERIALS TEXTURE ETC...
    92.     }
    93. }
    94.  
    95.  
     
    Last edited: Oct 15, 2018
  3. shayCohen007

    shayCohen007

    Joined:
    Oct 7, 2018
    Posts:
    5