Placing the bin

The final piece of functionality we need to place the bin is to allow the user to signal that they are happy with the bin's location, and afterward, to transition to the playing state. We will implement this by listening to the tap gesture and, once it's been detected--(with the bin in a valid position)--set the Placeable property Placed to true and ignore further updates.

If we consider the cursor to be synonymous with a mouse pointer, then gestures are synonymous with mouse buttons. Gestures provide a way for the user to input to the system, ideally as naturally and intuitively as possible. Microsoft is working on a catalog of standard interactions that they have made available in their library, these include the following:

  • Air-tap (press and release): Used to select holograms (such as double-clicking on GUI systems)
  • Bloom: A system gesture for bringing up the Start menu
  • Hold: When pressed beyond a specified threshold, this can be used to pick up holograms
  • Manipulation: Activated when pressed and dragged in an absolute movement; movement is normally 1:1, allowing for interactions such as resizing, rotating, and translating holograms
  • Navigation: Like Manipulation, but differs in how it is used; where Manipulation is normally used to directly manipulate the hologram, Navigation is intended to provide a way of isolating on a single axis and is commonly used for User Interface interactions

All gestures follow a common pattern consisting of the following steps:

  1. Creating a new GestureRecognizer.
  2. Specifying the gestures you are interested in.
  3. Registering to events for those gestures.
  4. Beginning to capture gestures.

As discussed, we are interested in the (air-) tap gesture and one is detected, given that the bin is in a valid position, we flag the bin as being placed for the SceneController to handle.

Jump back into the Placeable script and, at the top, add the UnityEngine.VR.WSA.Input namespace to give us access to gesture APIs, and add the following instance variable:

    GestureRecognizer tapGestureRecognizer; 

Our first task is to specify the gestures we're interested in and start listening for gestures; we will add this to the Start method of the Placeable script. Make the following changes to your Start method:

    void Start () 
{ meshFilter = GetComponentInChildren<MeshFilter>(); targetPosition = transform.position; targetNormal = transform.up; tapGestureRecognizer = new GestureRecognizer();
tapGestureRecognizer.SetRecognizableGestures(GestureSettings.Tap); tapGestureRecognizer.TappedEvent +=
TapGestureRecognizer_TappedEvent; tapGestureRecognizer.StartCapturingGestures(); }

In the preceding code snippet, we created our gesture recognizer, specified the events we're interested in, assigned a delegate for the specified event, and finally, start listening for the specified gestures. Now, let's implement the delegate that handles the event TappedEvent:

void TapGestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay) 
    { 
        if (ValidPosition) 
        { 
            Placed = true; 
            tapGestureRecognizer.TappedEvent -=  
TapGestureRecognizer_TappedEvent; } }

The method in the preceding code snippet implements the GestureRecognizer.TappedEventDelegate delegate, and the parameter gives you a sense of how flexible the library is; for example, the source parameter indicates which input triggered the event--This is of the InteractionSourceKind type and includes the following values:

  • Controller
  • Voice
  • Hand
  • Other

When notified of a tap event, we first check to see whether the bin is in a valid position and, if so, we set Placed to true and unregister our event listener from our gesture recognizer. That's it!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.196.217