【发布时间】:2021-07-10 05:26:43
【问题描述】:
我正在尝试创建一个游戏对象可以位于另一个游戏对象内部的应用程序。我希望能够在外部游戏对象内部移动内部游戏对象。当用户正在查看内部对象并进行空中点击手势时,外部对象是移动的对象。有没有办法在 MRTK 中实现这种行为。
【问题讨论】:
-
您想按层次结构、用户查看时间或两者同时查找游戏对象?
我正在尝试创建一个游戏对象可以位于另一个游戏对象内部的应用程序。我希望能够在外部游戏对象内部移动内部游戏对象。当用户正在查看内部对象并进行空中点击手势时,外部对象是移动的对象。有没有办法在 MRTK 中实现这种行为。
【问题讨论】:
我认为您的意思是:外部对象的 Collider 阻挡了 Raycast,因此您无法击中内部 Collider。
尝试使用RaycastAll 获取所有对象,然后检查是否命中子对象,如果是则选择子对象而不是父对象。
我将为此使用Linq Where 和Linq 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;
}
【讨论】:
只需移除外部对象的碰撞器
【讨论】:
尝试使用transform.localPosition 而不是transform.position。
Transform.position 使用场景坐标。 Transform.localPosition 使用父对象的本地坐标。
【讨论】: