Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

Help detecting what object is focused/tapped and only do certain things when it is

So i'm building an application where there is a humanoid model with a UI next to them. I want to be able to move the model wherever I'd like (and did have that part working before now), but now I'm trying to make it where the TapToPlace script is only called when the user is looking at and taps the humanoid model, because as it stands right now, the entire scene wants to get moved around even when I tap on the UI elements that I have. Basically I just need to know how to properly detect the object that is being gazed at/tapped on and trigger the TapToPlace script only when it is my humanoid model. I've tried many different combinations of what I thought were acceptable detection methods all to no avail or to null pointer exceptions, so any help would be greatly appreciated. Below is my GazeGestureManager, and for reference the "Fake Method" that is called is just a stand-in that sets an arbitrary int to 0 and prints it to the console.

using UnityEngine;
using UnityEngine.VR.WSA.Input;

public class GazeGestureManager : MonoBehaviour
{
public static GazeGestureManager Instance { get; private set; }

// Represents the hologram that is currently being gazed at.
public GameObject FocusedObject { get; private set; }
GameObject val = GameObject.Find("val_arm_fix");
RaycastHit hitInfo;

GestureRecognizer recognizer;

// Use this for initialization
void Awake()
{
    Instance = this;

    // Set up a GestureRecognizer to detect Select gestures.
    recognizer = new GestureRecognizer();
    recognizer.TappedEvent += (source, tapCount, ray) =>
    {
        // Send an OnSelect message to the focused object and its ancestors.
        // need this if statement to detect whether or not val is here
        if (true)
        {
            FocusedObject.SendMessageUpwards("OnSelect");
        }
        else
        {
            FocusedObject.SendMessageUpwards("FakeMethod");
        }
    };

    recognizer.StartCapturingGestures();
}

// Update is called once per frame
void Update()
{
    GameObject val = GameObject.Find("val_arm_fix");

    // Figure out which hologram is focused this frame.
    GameObject oldFocusObject = FocusedObject;

    // Do a raycast into the world based on the user's
    // head position and orientation.
    var headPosition = Camera.main.transform.position;
    var gazeDirection = Camera.main.transform.forward;

    RaycastHit hitInfo;
    if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
    {
        // If the raycast hit a hologram, use that as the focused object.
        FocusedObject = hitInfo.collider.gameObject;
    }
    else
    {
        // If the raycast did not hit a hologram, clear the focused object.
        FocusedObject = null;
    }

    // If the focused object changed this frame,
    // start detecting fresh gestures again.
    if (FocusedObject != oldFocusObject)
    {
        recognizer.CancelGestures();
        recognizer.StartCapturingGestures();
    }
}
}

Comments

  • trzytrzy ✭✭✭

    First of all, it looks like you tried reimplementing HoloToolkit's GazeManager on your own. Don't do that. Go through the tutorials at Holographic Academy.

    The HoloToolkit components are designed to be used as-is and the scripts are attached to prefabs that you can simply drop into your project. When you import the HoloToolkit-Unity package into your project, go to HoloToolkit -> Input -> Prefabs (in your project folder hierarchy) and draw InputManager into your scene hierarchy. When you inspect this object, you'll see a bunch of sub-objects and scripts, one of which is GazeManager.

    GazeManager is a singleton object. When the first and only instance is spawned, it sets a reference to itself at GazeManager.Instance. Now, any script can access the global gaze manager through GazeManager.Instance. Your code is not creating a singleton.

    Other than that, your code does indeed work (I've confirmed it) once you fix the following error (which should be printed in your Unity console): you cannot call GameObject.Find() inside of a constructor or initializer. Do not initialize "val" at the top of the file and your code ought to work.

    Also, make sure your objects have colliders attached with them. Raycast intersects with colliders, not the meshes themselves.

  • ReeleyReeley ✭✭✭

    I think what you are looking for is the HandDraggable script. Give that a shot :)

    Pssst: make sure you have a collider attached

  • Yes I've got colliders attached, but I'll try using the InputManager prefab and see how that goes. Thanks.

Sign In or Register to comment.