void ReadControllers(BioIKGroup group, UnityEngine.XR.InputDevice controller, bool isLeft) { if (controller == null) { return; } var _enabled = 0.0f; var trigger = false; if (controller.isValid) { controller.TryGetFeatureValue(UnityEngine.XR.CommonUsages.trigger, out _enabled); controller.TryGetFeatureValue(UnityEngine.XR.CommonUsages.gripButton, out trigger); } int i = isLeft ? 0 : 1; // Show that the arm is active in the state manager //WidgetInteraction.SetBodyPartActive(53 - i, _enabled > 0.9f); // Show that the fingers are active in the state manager //WidgetInteraction.SetBodyPartActive(55 - i, trigger); #if SENSEGLOVE group.WeakSetEnabled(true); #else group.SetEnabled(_enabled > 0.9f); group.UpdateFingers(System.Convert.ToDouble(trigger)); #endif }
// Update is called once per frame private void OnTriggerStay(Collider other) { if (other.gameObject.tag == "test") { if (deviceR.TryGetFeatureValue(UnityEngine.XR.CommonUsages.triggerButton, out triggerValue) && triggerValue) { a.material.color = Color.blue; RideBike(other); handPositionRight = GameObject.Find("handRight").GetComponent <Transform>().localPosition; } } }
/// <summary> /// Return a quaternion for the Y axis of the HMD's orientation. /// Used by orientation handlers to track the current heading before processing user input to adjust it. /// </summary> /// <returns></returns> public Quaternion GetHeadRotationY() { Quaternion headRotation = Quaternion.identity; #if UNITY_2019_1_OR_NEWER UnityEngine.XR.InputDevice device = UnityEngine.XR.InputDevices.GetDeviceAtXRNode(UnityEngine.XR.XRNode.Head); if (device.isValid) { device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.deviceRotation, out headRotation); } #elif UNITY_2017_2_OR_NEWER List <UnityEngine.XR.XRNodeState> nodeStates = new List <UnityEngine.XR.XRNodeState>(); UnityEngine.XR.InputTracking.GetNodeStates(nodeStates); foreach (UnityEngine.XR.XRNodeState n in nodeStates) { if (n.nodeType == UnityEngine.XR.XRNode.Head) { n.TryGetRotation(out headRotation); break; } } #else headRotation = InputTracking.GetLocalRotation(VRNode.Head); #endif Vector3 euler = headRotation.eulerAngles; euler.x = 0; euler.z = 0; headRotation = Quaternion.Euler(euler); return(headRotation); }
// Update is called once per frame void Update() { // left var leftHandDevices = new List <UnityEngine.XR.InputDevice>(); UnityEngine.XR.InputDevices.GetDevicesAtXRNode(UnityEngine.XR.XRNode.LeftHand, leftHandDevices); if (leftHandDevices.Count == 1) { UnityEngine.XR.InputDevice device = leftHandDevices[0]; Debug.Log(string.Format("Device name '{0}' with role '{1}'", device.name, device.role.ToString())); //lHand.transform.position = device. device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.devicePosition, out lHandPos); device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.deviceRotation, out lHandRot); lHand.transform.rotation = lHandRot; //lHand.transform.position = lHand.transform.InverseTransformVector(pbody.transform.position); lHand.transform.localPosition = lHandPos; //lHand.transform.position *= gameObject.transform.localScale.x; } else if (leftHandDevices.Count > 1) { Debug.Log("Found more than one left hand!"); } // right var rightHandDevices = new List <UnityEngine.XR.InputDevice>(); UnityEngine.XR.InputDevices.GetDevicesAtXRNode(UnityEngine.XR.XRNode.RightHand, rightHandDevices); if (rightHandDevices.Count == 1) { UnityEngine.XR.InputDevice device = rightHandDevices[0]; Debug.Log(string.Format("Device name '{0}' with role '{1}'", device.name, device.role.ToString())); //lHand.transform.position = device. device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.devicePosition, out rHandPos); Debug.Log("rHandPos:" + rHandPos); device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.deviceRotation, out rHandRot); rHand.transform.rotation = rHandRot; rHand.transform.localPosition = rHandPos; //rHand.transform.position *= gameObject.transform.localScale.x; } else if (rightHandDevices.Count > 1) { Debug.Log("Found more than one right hand!"); } }
void Update() { Vector3 position; headset.TryGetFeatureValue(UnityEngine.XR.CommonUsages.devicePosition, out position); transform.position = -position; //transform.rotation = Quaternion.Inverse(InputTracking.GetLocalRotation(VRNode.CenterEye)); }
// Update is called once per frame void Update() { bool triggerValue; if (controller.TryGetFeatureValue(CommonUsages.triggerButton, out triggerValue) && triggerValue) { transform.Translate(Camera.main.transform.forward * speed); } }
void SpinGameObject() { float gripValue; // Get controller grip value rightController.TryGetFeatureValue(UnityEngine.XR.CommonUsages.grip, out gripValue); Debug.Log("Grip Value: " + gripValue); // Rotate Chair with grip value ObjectToSpin.transform.Rotate(0, (float)(gripValue * speed * Time.deltaTime), 0); }
void UpdateHandAnimation(UnityEngine.XR.InputDevice targetDevice, Animator handAnimator) { if (targetDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.trigger, out float triggerValue)) { handAnimator.SetFloat("Trigger", triggerValue); } else { handAnimator.SetFloat("Trigger", 0); } if (targetDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.grip, out float gripValue)) { handAnimator.SetFloat("Grip", gripValue); } else { handAnimator.SetFloat("Grip", 0); } }
private void UpdateHandAnimation() { _handAnimator.SetFloat("Grip", grip.action.ReadValue <float>()); _handAnimator.SetFloat("Trigger", trigger.action.ReadValue <float>()); if (_targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue)) { _handAnimator.SetFloat("Trigger", triggerValue); } else { _handAnimator.SetFloat("Trigger", 0); } if (_targetDevice.TryGetFeatureValue(CommonUsages.grip, out float gripValue)) { _handAnimator.SetFloat("Grip", gripValue); } else { _handAnimator.SetFloat("Grip", 0); } }
//Here we need to add code to work void CheckForChanges() { Vector2 leftTouchCoords; Vector2 rightTouchCoords; bool triggerVal; if (leftController.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out leftTouchCoords) && leftTouchCoords != Vector2.zero) { if (leftTouchCoords.x < -deadZoneAmount) { MoveLeft(leftTouchCoords.x); } else if (leftTouchCoords.x > deadZoneAmount) { MoveRight(leftTouchCoords.x); } if (leftTouchCoords.y < -deadZoneAmount) { MoveBackward(leftTouchCoords.y); } else if (leftTouchCoords.y > deadZoneAmount) { MoveForward(leftTouchCoords.y); } } if (rightController.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out rightTouchCoords) && rightTouchCoords != Vector2.zero) { if (rightTouchCoords.x < -deadZoneAmount) { RotateLeft(rightTouchCoords.x); } else if (rightTouchCoords.x > deadZoneAmount) { RotateRight(rightTouchCoords.x); } if (rightTouchCoords.y < -deadZoneAmount) { MoveDown(rightTouchCoords.y); } else if (rightTouchCoords.y > deadZoneAmount) { MoveUp(rightTouchCoords.y); } } }
/// <summary> /// Returns whether the secondary button is pressed on the left or right /// controller (Y or B respectively on the Quest). /// </summary> private bool SecondaryButtonIsPressed(bool left) { UnityEngine.XR.InputDevice controller = left ? _leftController : _rightController; if (controller.TryGetFeatureValue( UnityEngine.XR.CommonUsages.secondaryButton, out bool secondaryButtonValue ) ) { return(secondaryButtonValue); } // If we can't get whether the button is pressed, assume it's not. return(false); }
private void playerMovement() { if (deviceML.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out primaryAxisValueL) && primaryAxisValueL != Vector2.zero) { transform.position += Vector3.Scale((transform.forward * primaryAxisValueL.y + transform.right * primaryAxisValueL.x), new Vector3(0.05f, 0.05f, 0.05f)); } if (deviceMR.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out primaryAxisValueR) && primaryAxisValueR != Vector2.zero) { if (primaryAxisValueR.x > 0.5) { transform.Rotate(new Vector3(0, 1, 0), 5); } if (primaryAxisValueR.x < -0.5) { transform.Rotate(new Vector3(0, 1, 0), -5); } } }
private void UpdateOutputs(Vector3 pos, double time) { inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.deviceRotation, out Quaternion dRot); inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.deviceAngularVelocity, out Vector3 dRotRate); dForward = (dRot * relativeRotation * Vector3.forward); dUp = (dRot * relativeRotation * Vector3.up); dRight = (dRot * relativeRotation * Vector3.right); for (int i = samplesTaken - 1; i >= 1; i--) { this.pos[i] = this.pos[i - 1]; velX[i] = velX[i - 1]; velY[i] = velY[i - 1]; velZ[i] = velZ[i - 1]; accel[i] = accel[i - 1]; gyro[i] = gyro[i - 1]; gyroRate[i] = gyroRate[i - 1]; dGravity[i] = dGravity[i - 1]; timestamp[i] = timestamp[i - 1]; } if ((float)time - timestamp[0] > 1) { timestamp[0] = Time.realtimeSinceStartup; } else { timestamp[0] = (float)time; } this.pos[0] = pos; // Apply smoothing if samples are too close together int j = 1; while (timestamp[0] - timestamp[j] <= minTimeDiff & j < samplesTaken) { j++; } if (j > 0) { double diffTime = timestamp[0] - timestamp[j]; // m/s velX[0] = (this.pos[0].x - this.pos[j].x) / diffTime; velY[0] = (this.pos[0].y - this.pos[j].y) / diffTime; velZ[0] = (this.pos[0].z - this.pos[j].z) / diffTime; gyroRate[0] = dRotRate * (float)(180f / Math.PI); if (samplesTaken >= samples) { // Divide by 9.8 to get g since v/dt will be in m/s/s accel[0] = new Vector3( (float)((velX[0] - velX[j]) / (9.8f * diffTime)), (float)((velY[0] - velY[j]) / (9.8f * diffTime)), (float)((velZ[0] - velZ[j]) / (9.8f * diffTime)) ); } } // We need movement relative to the device's own axes, so use dot products dGravity[0] = new Vector3( Vector3.Dot(Vector3.up, -dRight), Vector3.Dot(Vector3.up, -dUp), Vector3.Dot(Vector3.up, dForward) ); Vector3 dAccel = new Vector3( Vector3.Dot(accel[0], dRight), Vector3.Dot(accel[0], dUp), Vector3.Dot(accel[0], -dForward) ); samplesTaken++; if (samplesTaken >= samples) { samplesTaken = samples; // Debug here if needed } // Don't forget to get pointer location and buttons // Also will need to check that gyro rate works OK with this smoothing - may also need to derive that if (timestamp[0] - timestamp[j] >= minTimeDiff) { lastTimestamp = (Int64)(timestamp[0] * 1000000); accelerometer = dAccel + dGravity[0]; gyroscopeRate = new Vector3( gyroRate[0].x, gyroRate[0].z, -gyroRate[0].y ); inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryButton, out primaryButton); inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryButton, out secondaryButton); inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.gripButton, out gripButton); inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.grip, out grip); inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.triggerButton, out triggerButton); inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.trigger, out trigger); inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out primary2DAxis); inputDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxisClick, out primary2DAxisClick); if (callbackDSUDevice != null) { callbackDSUDevice.UpdateFromDorsalDevice(); } } }
void Update() { AssignControllers(); if (controller != null) { //trigger down event if (controller.TryGetFeatureValue(UnityEngine.XR.CommonUsages.triggerButton, out trigger) && trigger && !hasTrigger) { print("Trigger down"); hasTrigger = true; controller.SendHapticImpulse(0, .2f, .1f); } //trigger up event if (!trigger && hasTrigger) { print("Trigger up"); hasTrigger = false; } //grip down event if (controller.TryGetFeatureValue(UnityEngine.XR.CommonUsages.gripButton, out grip) && grip && !hasGrip) { print("Grip Down"); hasGrip = true; } //grip up event if (!grip && hasGrip) { print("Grip Up"); hasGrip = false; } //joystick Vector2 joystick; if (controller.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out joystick) && joystick.sqrMagnitude > 0f) { print("joystick value: " + joystick); } //button primary down event if (controller.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryButton, out buttonPrimary) && buttonPrimary && !hasPrimary) { print("Primary down"); hasPrimary = true; } //button primary up event if (!buttonPrimary && hasPrimary) { print("Primary up"); hasPrimary = false; } //button secondary down event if (controller.TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryButton, out buttonSecondary) && buttonSecondary && !hasSecondary) { print("Secondary Down"); hasSecondary = true; } //button secondary up event if (!buttonSecondary && hasSecondary) { print("Secondary Up"); hasSecondary = false; } } }