-3

I am currently working with the Hololens 2 for a project and am now trying to spawn an object at the position I target with the hand ray. First I created a mesh with the Spatial Awareness System from MRTK and now I want to spawn an object at the position of the cursor.

I read about a lot of ways I can handle this, like cursor.transform.position (which doesn't work, maybe because I use the default cursor?) and Instantiate, the component "Tap to Place", or using RaycastHit. Unfortunately, because this is my first time working with the Hololens, I don't know which solution might be the best. I don't think it can be that hard to spawn something, but maybe I am just blind.

Is there an easy way to solve my problem or get the right coordinates from the cursor of the hand gaze?

1 Answers1

1

Here you go, use IMixedRealityPointerHandler. See the documentation.

public void OnPointerClicked(MixedRealityPointerEventData eventData)
{
    var result = eventData.Pointer.Result;
    var hitPosition = result.Details.Point;
    // Check if hitting spatial mapping layer
    if (result.CurrentPointerTarget?.layer == 31)
        Instantiate(yourPrefab, hitPosition, yourRotation);
    else
        Debug.Log("Hit surface with layer: " + result.CurrentPointerTarget?.layer.ToString());
}
chris
  • 1,831
  • 18
  • 33
Perazim
  • 1,501
  • 3
  • 19
  • 42
  • I am also facing the same problems. I did the same thing similar to yours. But it is only working on the game object to which it is attached. I want to use it on the ground plane. I want to make it generic on all objects (such as mesh, planes, holograms Which have rigid body component attached) – Rajat Sharma Jan 14 '21 at 14:54
  • so where is the problem then? Delete the if-line with mentioning the layer and thats it. – Perazim Jan 14 '21 at 16:18
  • The problem is that this pointer event is attached to a particular game object. So, the pointer event will work only if this object is in focus therefore there is no meaning if-else. To fix this issue either we can attach the same OnPOinterClicked for all game objects or we can register for global input events in the start method (in this case your if will true when it hits the floor). To register global input use `CoreServices.InputSystem?.RegisterHandler(this);` in start method. – Rajat Sharma Jan 15 '21 at 09:11