当前位置:   article > 正文

Unity+Pico 手柄按键控制_inputdevice.trygetfeaturevalue

inputdevice.trygetfeaturevalue

一、定义手柄按键API


1、InputDevices.GetDeviceAtXRNode,通过XRNode获取对应的设备;

2、XRNode是一个枚举类型,包含LeftEye、RightEye、CenterEye、Head、LeftHand、RightHand、GameController、TrackingReference、HardwareTracker;

3、TryGetFeatureValue,得到某个特性的值;

4、CommonUsages定义了用于从XR.InputDevice.TryGetFeatureValue获取输入特征的静态变量,用来指定想要获取的特性。

1

2

3

4

5

6

7

8

9

10

11

12

Vector2 vec2DAxis = Vector2.zero;

bool isGrip = false;

bool isTrigger = false;

bool isMenu = false;

bool isPrimaryButton = false;

bool isSecondButton = false;

InputDevices.GetDeviceAtXRNode(XRNode.LeftHand).TryGetFeatureValue(CommonUsages.primary2DAxis, out vec2DAxis);

InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.gripButton, out isGrip);

InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.triggerButton, out isTrigger);

InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.menuButton, out isMenu);

InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.primaryButton, out isPrimaryButton);

InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.secondaryButton, out isSecondButton);

二、控制物体移动


编写脚本用手柄控制物体的前后左右移动,如果把脚本挂载到头显上,就变成控制自身的移动。 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;

public class ControlObject : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        Vector2 vec2DAxis = Vector2.zero;
        InputDevices.GetDeviceAtXRNode(XRNode.LeftHand).TryGetFeatureValue(CommonUsages.primary2DAxis, out vec2DAxis);
        transform.position = new Vector3(transform.position.x + vec2DAxis.x * Time.deltaTime,
            transform.position.y, transform.position.z + vec2DAxis.y * Time.deltaTime);
    }
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/1016420
推荐阅读
相关标签
  

闽ICP备14008679号