Search Unity

How to get the position of a Hololens Clicker?

Discussion in 'VR' started by meng_meng, Dec 5, 2016.

  1. meng_meng

    meng_meng

    Joined:
    Nov 23, 2016
    Posts:
    1
    I want to control a gameobject with a clicker movement,so i want to get the position of Clicker.I try to use the method below,but its no work,the method "TryGetPosition()" always return 0.How to solve the problem,thx for reply.

    InteractionManager.SourcePressed += InteractionManager_SourcePressed;
    //
    private void InteractionManager_SourcePressed(InteractionSourceState state)
    {
    if (state.source.kind == InteractionSourceKind.Controller)
    {
    Vector3 v;
    Vector3 position=state.properties.location.TryGetPosition(out v);
    FocusedGameObject = InteractibleManager.Instance.FocusedGameObject;
    }
    }
    }
     
  2. Onsterion

    Onsterion

    Joined:
    Feb 21, 2014
    Posts:
    215
    Same problem here.
     
  3. Hodgson_SDAS

    Hodgson_SDAS

    Joined:
    Apr 30, 2015
    Posts:
    123
    The clicker does not return a position because there's no way to track your hands if they're outside sensor range.
     
  4. pfreese

    pfreese

    Unity Technologies

    Joined:
    Feb 23, 2015
    Posts:
    85
    There is no positional information from the clicker, since it is not a tracked controller. Most use the clicker in tandem with gaze information to select/target.
     
    Hodgson_SDAS likes this.
  5. BrandonFogerty

    BrandonFogerty

    Joined:
    Jan 29, 2016
    Posts:
    83
    Hi @meng_meng,

    As Peter pointed out, the clicker device does not provide positional information. It only measures orientation changes. However, the orientation information can not be accessed directly. You can indirectly get relative clicker orientation information by using a GestureRecognizer that responds to navigation gesture events. The following script will work with both a hand gesture or a clicker device.

    Create an empty scene and put this script on your Main Camera.
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.VR.WSA.Input;
    4.  
    5. public class ClickerTest : MonoBehaviour
    6. {
    7.     GameObject targetObject = null;
    8.     GestureRecognizer gr = null;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         CreateTargetObject();
    14.         SetupGestureRecognizer();
    15.         MoveTarget(0.5f);
    16.     }
    17.  
    18. #if UNITY_EDITOR
    19.     void Update ()
    20.     {
    21.         MoveTarget(GetMouseInput());
    22.     }
    23. #endif
    24.  
    25.     void CreateTargetObject()
    26.     {
    27.         targetObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    28.         targetObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    29.         targetObject.transform.parent = Camera.main.transform;
    30.     }
    31.  
    32.     void SetupGestureRecognizer()
    33.     {
    34.         gr = new GestureRecognizer();
    35.         gr.NavigationUpdatedEvent += NavigationUpdatedEvent;
    36.         gr.StartCapturingGestures();
    37.     }
    38.  
    39.     float GetMouseInput()
    40.     {
    41.         return Camera.main.ScreenToViewportPoint(Input.mousePosition).x;
    42.     }
    43.  
    44.     void NavigationUpdatedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
    45.     {
    46.         float t = normalizedOffset.x * 0.5f + 0.5f;
    47.         MoveTarget(t);
    48.     }
    49.  
    50.     // t must be a value between 0.0 and 1.0
    51.     void MoveTarget(float t)
    52.     {
    53.         Vector3 pos = Camera.main.ViewportToWorldPoint(new Vector3(t, 0.5f, 5.0f));
    54.         targetObject.transform.position = pos;
    55.     }
    56. }
    57.  
    I hope that helps!
     
    MyOwnGames, Hodgson_SDAS and pfreese like this.
  6. MyOwnGames

    MyOwnGames

    Joined:
    Jul 21, 2014
    Posts:
    25
    Awesome man! Exactly what I was looking for!
     
  7. ocramot

    ocramot

    Joined:
    Mar 11, 2014
    Posts:
    3
    Awesome! Thanks for the script, really useful!
    Although I tried to move the "cursor" in 3D space and for some reasons, the normalized offset is reporting only the clicker's movement along the X and Y axis, while the Z axis is always zero. The same doesn't happen if I use the hand, though.
    I don't think this is your script's fault, I noticed that this happens also with the windows generated from the HoloLens main menu: if you move them around in the 3D space, you can move them back and forth with the hand, but not with the clicker.
    Anyone has any idea why this happens?

     
  8. ricardobah

    ricardobah

    Joined:
    Jul 19, 2017
    Posts:
    2
    Use cursor position
     
  9. ricardobah

    ricardobah

    Joined:
    Jul 19, 2017
    Posts:
    2
    like that : Instantiate(Car, GameObject.Find("Cursor").GetComponent<Transform>().position,new Quaternion());
     
  10. myieye

    myieye

    Joined:
    Apr 22, 2015
    Posts:
    1
    @BrandonFogerty
    Is it still the case that orientation from the clicker is only available during navigation or manipulation gestures?
    These gestures only start on a tap. I'd like to simply use it as a joystick. You don't have to click/tap to activate a joystick.
     
  11. krishnanpc

    krishnanpc

    Joined:
    Oct 30, 2017
    Posts:
    19