User Tools

Site Tools


Interacting with scene objects

FingerGestures can be made aware of collidable objects in your scene by hooking up a ScreenRaycaster component to your gesture recognizers.

GestureRecognizers and FingerDetectors use the ScreenRaycaster component to fire rays into your scene in order to find the top-most objects under your fingers. These objects must be equipped with a collider for the ScreenRaycaster to “see” them.

ScreenRaycaster Setup

Hooking up a ScreenRaycaster component to your gesture recognizers is very straightforward. By default, the gesture recognizers will look for a ScreenRaycaster component on the same game object, but you may also explicitly set a reference to the desired ScreenRaycaster through the Raycaster property of the gesture recognizer.

Note: Gesture recognizers can share the same ScreenRaycaster.

The following properties are exposed:

  • Cameras: lets you specify your own list of cameras to raycast from. By default, the ScreenRaycaster fires rays from the main camera if this is left empty. This may be useful in situations where you're using multiple cameras to render different layers of your scene but you still want to pick objects in each layer (e.g. one 2D camera to render the UI, and one for the world).
  • Ignore Layer Mask: specify the layers that the ScreenRaycaster component should not interact with
  • Ray Thickness: if greater than zero, the ScreenRaycaster will use Physics.SphereCast() with the provided thickness instead of Physics.Raycast. This will not work properly on triggers (Unity limitation). Not supported for 2D objects.
  • Use Physics 2D (Unity 4.3+) when enabled, the ScreenRaycaster will first attempt to raycast 2D objects using the Physics2D API on orthographic cameras, and then fall back to regular 3D raycasting if no hits were found
  • Visualize Raycasts: used in editor mode only; draws a colored line showing each raycast for a few seconds


Once your gesture recognizer is properly hooked up with a ScreenRaycaster, you can access the following gesture data in your event handling functions:

  • gesture.Selection: the GameObject currently located at this gesture current position (null if none)
  • gesture.StartSelection: the GameObject that was at the gesture start position (null if none)
  • gesture.Raycast: last raycast hit data (position, normal, etc…)

Sample code showing how to output a debug message when tapping an object:

void OnTap( TapGesture gesture )
    GameObject hitObject = gesture.Selection;
    if( hitObject )
        Vector3 hitPos3d = gesture.Raycast.Hit3D.point;
        Vector3 hitNormal = gesture.Raycast.Hit3D.normal;
        Debug.Log( "Tapped on " + hitObject + " @ " + hitPos3d );

A very basic object drag & drop logic showing how to use the StartSelection and Selection properties:

void OnDrag( DragGesture gesture )
    // object we started the drag gesture on
    GameObject startObject = gesture.StartSelection;
    // object the dragging finger is currently over
    GameObject currentObject = gesture.Selection;
    // drop?
    if( gesture.Phase == ContinuousGesturePhase.Ended )
        if( startObject && 
            currentObject &&
            startObject != currentObject )
            Debug.Log( "Dragged " + startObject + " on " + currentObject );

Raycast Hit Data

Since FingerGesture 3.1 added support for 2D raycasting via the new Physics2D API, the gesture.Raycast structure contains a hit result field for both 3D and 2D raycasts:

void OnFingerDown( FingerDownEvent e )
    GameObject hitObj = e.Selection;    // object hit
    // didn't hit any objects, so exit now
    if( !hitObj )
    // was this a 2D sprite/object hit?
    if( e.Raycast.Is2D )
        RaycastHit2D hit2d = e.Raycast.Hit2D;
        Debug.Log( "FingerDown on 2D obj " + hitObj + " @ " + hit2d.point );
    else // regular 3D raycast hit
        RaycastHit hit3d = e.Raycast.Hit3D;
        Debug.Log( "FingerDown on 3D obj " + hitObj + " @ " + hit3d.point );

Forwarding Events To Objects

In addition to providing you object-picking data, using a ScreenRaycaster with your gesture recognizers or finger detectors also allows FingerGestures to automatically forward the gesture/finger event messages to the object being interacted with.

The Send Message To Selection property controls which selection object should receive the gesture event (setting it to None will disable this functionality)

manual/object_interaction.txt · Last modified: 2013/12/11 08:54 by wravaine