Search code examples
hololensmrtk

How do I get the position of an active MRTK pointer?


I'm trying to place a preview object at the end of an articulated hand pointer in Mixed Reality Toolkit. How do I get the position of where the pointer is hitting geometry?

I have the DefaultControllerPointer set to articulated hand, but I need to get a reference to it and then get the transform position of the tip.


Solution

  • Here is an example of how you would iterate over all controllers, find articulated hands that are hand rays, and then get the position of the end point (as well as the ray start point), and finally figure out if the ray is hitting geometry (an object), since it has a default length:

    using Microsoft.MixedReality.Toolkit;
    using Microsoft.MixedReality.Toolkit.Input;
    using UnityEngine;
    
    public class HitPointTest : MonoBehaviour
    {
        // Update is called once per frame
        void Update()
        {
            foreach(var source in MixedRealityToolkit.InputSystem.DetectedInputSources)
            {
                // Ignore anything that is not a hand because we want articulated hands
                if (source.SourceType == Microsoft.MixedReality.Toolkit.Input.InputSourceType.Hand)
                {
                    foreach (var p in source.Pointers)
                    {
                        if (p is IMixedRealityNearPointer)
                        {
                            // Ignore near pointers, we only want the rays
                            continue;
                        }
                        if (p.Result != null)
                        {
                            var startPoint = p.Position;
                            var endPoint = p.Result.Details.Point;
                            var hitObject = p.Result.Details.Object;
                            if (hitObject)
                            {
                                var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                                sphere.transform.localScale = Vector3.one * 0.01f;
                                sphere.transform.position = endPoint;
                            }
                        }
    
                    }
                }
            }
        }
    }
    

    Note that this is for latest mrtk_development codebase, should also work as of RC1.