I am trying to create an application where a gameobject can be inside another gameobject. I want to be able to move the inner gameobject while it is inside the outer one. When the user is looking at the inner one, and does the air tap gesture the outer object is the one that moves. Is there any way to implement this behavior in MRTK.
Asked
Active
Viewed 123 times
-1
-
You want to find the game object by hierarchy, when user is looking at it, or both? – rustyBucketBay Apr 15 '21 at 07:26
2 Answers
2
I think what you mean is: The outer object's Collider is blocking the Raycast so you can't hit the inner Collider.
try using getting all of them using RaycastAll
and then check whether a child object is hit if yes select the child instead if the parent.
I will use Linq Where
and Linq Any
for that
Something like e.g.
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;
}

derHugo
- 83,094
- 9
- 75
- 115