コード例 #1
0
    /// <summary>
    /// Raycast/Sphercast way to detect mementos
    /// </summary>

    /*
     * void FixedUpdate()
     * {
     *  Ray ray = new Ray(Eye.position, Eye.forward);
     *  RaycastHit hit;
     *
     *  //if (Physics.Raycast(ray, out hit, Mathf.Infinity, GazeLayer))
     *  if(Physics.SphereCast(ray, 2.5f, out hit, Mathf.Infinity, GazeLayer))
     *  {
     *      // If there is no current gazed object, set gazed object to the hit
     *      if (!gazedObject)
     *          gazedObject = hit.collider.GetComponent<GazeObject>();
     *
     *      // If there is previous gazed object
     *      if (gazedObject)
     *      {
     *          // If the previous game object is not the same as the new hit
     *          if (gazedObject != hit.collider.GetComponent<GazeObject>())
     *          {
     *              gazedObject.OnGazeExit();
     *              gazedObject = hit.collider.GetComponent<GazeObject>();
     *              gazedObject.OnGazeStart();
     *          }
     *          else // If the previous game object is the same as the new hit
     *          {
     *              gazedObject.OnGazeStart();
     *              Debug.Log(gazedObject.name);
     *          }
     *      }
     *
     *  }
     *
     *  // If not gazing at anything
     *  if (!Physics.Raycast(ray, out hit, Mathf.Infinity, GazeLayer))
     *  {
     *      if (gazedObject)
     *      {
     *          gazedObject.OnGazeExit();
     *          //gazedObject = null;
     *      }
     *  }
     *
     * }
     */
    #endregion

    #region Attached to Capsule under Camera (eye)

    /// <summary>
    /// Pinocchio Nose Da Wae
    /// </summary>
    /// <param name="other"></param>
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("GazeObject"))
        {
            // If there is no current gazed object, set gazed object to the hit
            if (!gazedObject)
            {
                gazedObject = other.GetComponent <GazeObject>();
            }

            // If there is previous gazed object
            if (gazedObject)
            {
                // If the previous game object is not the same as the new hit
                if (gazedObject != other.GetComponent <GazeObject>())
                {
                    gazedObject.OnGazeExit();
                    gazedObject = other.GetComponent <GazeObject>();
                    gazedObject.OnGazeStart();
                }
                else // If the previous game object is the same as the new hit
                {
                    gazedObject.OnGazeStart();
                }
            }
        }
    }
コード例 #2
0
ファイル: GazeCaster.cs プロジェクト: wildr2/CartesianPiano
    private void Update()
    {
        Vector3 dir = transform.forward;

        RaycastHit[] hits = Physics.RaycastAll(transform.position, dir, 100);
        GazeObject   g    = hits.Length > 0 ? hits[0].collider.GetComponent <GazeObject>() : null;

        // Update currently gazed and events
        if (g != gazed)
        {
            // Exit old gaze object
            if (gazed != null)
            {
                gazed.OnGazeExit();
            }

            // Enter new gaze object
            if (g != null)
            {
                g.OnGazeEnter();
            }

            // Gazed could now be null or a new object
            gazed = g;
        }
        else
        {
            // Stay in same gaze object (or no gaze object)
            if (g != null)
            {
                g.OnGazeStay();
            }
        }


        // Reticle
        if (reticle != null)
        {
            if (g == null)
            {
                //reticle.gameObject.SetActive(false);
                reticle.position = transform.position + dir * default_distance;
            }
            else
            {
                //reticle.gameObject.SetActive(true);
                reticle.position = hits[0].point - dir * reticle_offset;
            }

            reticle.rotation = Quaternion.LookRotation(dir);
        }
    }