Calculate distance HoloLens to object and object to object

This guide includes two scenes. In the first scene, you have to calculate the distance between HoloLens and any surface from the real world and the second one is a calculation which summarizes the distance from multiple three-dimensional points.


The calculation of the distance between HoloLens and any three-dimensional point in the space is described by the following method. You can use the spatial mapping component from HoloToolkit in order to recognize the surfaces from the real-world environment. These two points are the location of the HoloLens and the position that HoloLens recognizes as a surface through Gaze.


void Update()
        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;
        var gazePosition = GameObject.Find("GlobalCursor").transform.position;

        RaycastHit hitInfo;

        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
            meshRenderer.enabled = true;

            this.transform.position = hitInfo.point;
            this.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);

            //Display distance (Hololens - Cursor)
            float distance = Vector3.Distance(headPosition, gazePosition);
            TextMessage.Instance.ChangeTextMessage(string.Format("Distance: {0}", distance >= 1.0f ? distance.ToString("####0.0") + "m" : distance.ToString("0.00") + "cm" ));
            meshRenderer.enabled = false;

You can find this sample and download it from github to this link


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s