Create a gist now

Instantly share code, notes, and snippets.

@yunojy /HandMove.cs
Last active Jun 15, 2016

What would you like to do?
// usage: http://2vr.jp/2016/05/10/unity-hololens-tips/#NavigationDrag_Gesture
using UnityEngine;
using System.Collections;
using UnityEngine.VR.WSA.Input;
public class HandMove : MonoBehaviour {
public TextMesh Log;
public TextMesh Status;
public TextMesh GrabObj;
public GameObject FocusedObject;
public LayerMask targetLayerMask;
private bool isGrab = false;
private Vector3 initPos;
void Start() {
InteractionManager.SourceDetected += InteractionManager_SourceDetected;
InteractionManager.SourceUpdated += InteractionManager_SourceUpdated;
InteractionManager.SourceLost += InteractionManager_SourceLost;
InteractionManager.SourcePressed += InteractionManager_SourcePressed;
InteractionManager.SourceReleased += InteractionManager_SourceReleased;
}
void Update() {
if (isGrab && FocusedObject != null) return;
var headPosition = Camera.main.transform.position;
var gazeDirection = Camera.main.transform.forward;
RaycastHit hitInfo;
if (Physics.Raycast(headPosition, gazeDirection, out hitInfo) &&
((1<<hitInfo.collider.gameObject.layer) & targetLayerMask) != 0) {
FocusedObject = hitInfo.collider.gameObject;
if (GrabObj != null) GrabObj.text = FocusedObject.name;
} else {
FocusedObject = null;
if (GrabObj != null) GrabObj.text = "";
}
}
void InteractionManager_SourceDetected(InteractionSourceState state) {
if (Status != null) Status.text = "Detected";
isGrab = false;
}
void InteractionManager_SourceLost(InteractionSourceState state) {
if (Status != null) Status.text = "Lost";
isGrab = false;
}
void InteractionManager_SourceUpdated(InteractionSourceState state) {
if (Status != null) Status.text = "Updated";
if (!isGrab || FocusedObject == null) return;
Vector3 handPosition;
if (state.source.kind == InteractionSourceKind.Hand && state.properties.location.TryGetPosition(out handPosition)) {
if (Log != null) Log.text = handPosition.ToString();
FocusedObject.transform.position = initPos + handPosition;
}
}
void InteractionManager_SourcePressed(InteractionSourceState state) {
if (Status != null) Status.text = "Pressed";
if (FocusedObject == null) return;
isGrab = true;
Vector3 handPosition;
if (state.source.kind == InteractionSourceKind.Hand && state.properties.location.TryGetPosition(out handPosition)) {
initPos = FocusedObject.transform.position - handPosition;
}
}
void InteractionManager_SourceReleased(InteractionSourceState state) {
if (Status != null) Status.text = "Released";
isGrab = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment