【问题标题】:How to select the inner gameobject when an object is inside another in hololens?当一个对象在hololens中的另一个对象内部时如何选择内部游戏对象?
【发布时间】:2021-07-10 05:26:43
【问题描述】:

我正在尝试创建一个游戏对象可以位于另一个游戏对象内部的应用程序。我希望能够在外部游戏对象内部移动内部游戏对象。当用户正在查看内部对象并进行空中点击手势时,外部对象是移动的对象。有没有办法在 MRTK 中实现这种行为。

【问题讨论】:

  • 您想按层次结构、用户查看时间或两者同时查找游戏对象?

标签: c# unity3d hololens mrtk


【解决方案1】:

我认为您的意思是:外部对象的 Collider 阻挡了 Raycast,因此您无法击中内部 Collider。

尝试使用RaycastAll 获取所有对象,然后检查是否命中子对象,如果是则选择子对象而不是父对象。

我将为此使用Linq WhereLinq Any

类似的东西,例如

using System.Linq;

...

var hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);

GameObject selectedObject = null;

// go through all hits
foreach (var hit in hits)
{
    // get all children colliders of the hit object
    var colliders = hit.gameObject.GetComponentsInChildren<Collider>(true);
    // since these will also include this hit collider itself filter it/them out
    var childrenColliders =  colliders.Where(c => c.gameObject != hit.gameObject);

    // Finally check if any children is contained in the hits
    // if yes then this object can not be selected
    if(childrenColliders.Any(c => hits.Contains(c)) continue;

    // otherwise you have found a child that has no further hit children
    selectedObject = hit.gameObject;

    break;
}

【讨论】:

    【解决方案2】:

    只需移除外部对象的碰撞器

    【讨论】:

      【解决方案3】:

      尝试使用transform.localPosition 而不是transform.position

      Transform.position 使用场景坐标。 Transform.localPosition 使用父对象的本地坐标。

      【讨论】:

      • 我认为这里的问题是外部碰撞器阻挡了光线投射,因此您无法击中内部碰撞器;)
      猜你喜欢
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多