我正在尝试创建一个应用程序,其中游戏对象可以位于另一个游戏对象中。我希望能够移动内部游戏对象,同时它在外部游戏对象内部。当用户看着里面的物体,并做出敲击手势时,外面的物体就是移动的物体。有什么方法可以在MRTK中实现这种行为吗。
我想你的意思是:外部对象的碰撞器正在阻挡光线投射,所以你不能击中内部碰撞器。
尝试使用RaycastAll
获取所有对象,然后检查子对象是否命中(如果是(,选择子对象(如果是父对象(。
我将使用LinqWhere
和LinqAny
进行
类似的东西
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;
}
只需移除外部物体的对撞机