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.
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.
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.
using System.Collections;
using UnityEngine;
using UnityEngine.VR.WSA.Input;
public class ClickerTest : MonoBehaviour
{
GameObject targetObject = null;
GestureRecognizer gr = null;
// Use this for initialization
void Start ()
{
CreateTargetObject();
SetupGestureRecognizer();
MoveTarget(0.5f);
}
#if UNITY_EDITOR
void Update ()
{
MoveTarget(GetMouseInput());
}
#endif
void CreateTargetObject()
{
targetObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
targetObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
targetObject.transform.parent = Camera.main.transform;
}
void SetupGestureRecognizer()
{
gr = new GestureRecognizer();
gr.NavigationUpdatedEvent += NavigationUpdatedEvent;
gr.StartCapturingGestures();
}
float GetMouseInput()
{
return Camera.main.ScreenToViewportPoint(Input.mousePosition).x;
}
void NavigationUpdatedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
{
float t = normalizedOffset.x * 0.5f + 0.5f;
MoveTarget(t);
}
// t must be a value between 0.0 and 1.0
void MoveTarget(float t)
{
Vector3 pos = Camera.main.ViewportToWorldPoint(new Vector3(t, 0.5f, 5.0f));
targetObject.transform.position = pos;
}
}
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?
@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.