头-注视 UnityHead-gaze in Unity

注视是用户在混合现实中以影像为目标的主要方式。Gaze is the primary way for users to target holograms your app creates in Mixed Reality.

实现机头Implementing head-gaze

从概念上讲,您通过从用户的耳机中向前投影一条射线 来确定其 点击情况。Conceptually, you determine head-gaze by projecting a ray forward from the user's headset to see what it hits. 在 Unity 中,用户的头位置和方向通过 相机公开,特别是 UnityEnginetransformUnityEngine转换。位置In Unity, the user's head position and direction are exposed through the Camera, specifically UnityEngine.Camera.main.transform.forward and UnityEngine.Camera.main.transform.position.

调用 RayCast 可提供一个 RaycastHit ,其中包含有关冲突的信息,其中包括3d 冲突点,以及打印头看点的另一 GameObject。Calling Physics.RayCast gives you a RaycastHit containing information about the collision, including the 3D collision point and the other GameObject the head-gaze ray hit.

示例:实现机头Example: Implement head-gaze

void Update()
{
       RaycastHit hitInfo;
       if (Physics.Raycast(
               Camera.main.transform.position,
               Camera.main.transform.forward,
               out hitInfo,
               20.0f,
               Physics.DefaultRaycastLayers))
       {
           // If the Raycast has succeeded and hit a hologram
           // hitInfo's point represents the position being gazed at
           // hitInfo's collider GameObject represents the hologram being gazed at
       }
}

最佳实践Best practices

尽管上面的示例从更新循环中触发了单个 raycast 以查找用户头所在的目标,但我们建议使用单个对象来管理所有的头节点。While the example above fires a single raycast from the update loop to find the target the user's head points at, we recommended using a single object to manage all head-gaze processes. 将您的头看起来的逻辑结合起来可节省您的应用程序的宝贵处理能力,并将 raycasting 限制为每帧一次。Combining your head-gaze logic will save your app precious processing power and limit your raycasting to one per frame.

形象注视Visualizing head-gaze

就像在计算机上使用鼠标指针一样,你应该实现表示用户头看的 光标Just like with a mouse pointer on a computer, you should implement a cursor that represents the user's head-gaze. 了解用户的目标内容会对要与之交互的内容增加信心。Knowing what content a user is targeting increases confidence in what they're about to interact with.

打印头-注视混合现实工具包Head-gaze in the Mixed Reality Toolkit

可以通过 MRTK 中的 输入管理器 访问 head。You can access head-gaze from the Input Manager in MRTK.

下一个开发检查点Next Development Checkpoint

如果遵循我们所说的 Unity 开发旅程,就是在浏览 MRTK 核心构建基块。If you're following the Unity development journey we've laid out, you're in the midst of exploring the MRTK core building blocks. 从这里,你可以继续了解下一部分基础知识:From here, you can continue to the next building block:

或跳转到混合现实平台功能和 API:Or jump to Mixed Reality platform capabilities and APIs:

你可以随时返回到 Unity 开发检查点You can always go back to the Unity development checkpoints at any time.

另请参阅See also