首页 > 其他分享 >Unity+Pico 手柄按键控制

Unity+Pico 手柄按键控制

时间:2023-01-10 15:01:14浏览次数:43  
标签:手柄 InputDevices Unity Pico XRNode GetDeviceAtXRNode out TryGetFeatureValue Commo

一、定义手柄按键API


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

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

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

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

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);
    }
}

 

标签:手柄,InputDevices,Unity,Pico,XRNode,GetDeviceAtXRNode,out,TryGetFeatureValue,Commo
From: https://www.cnblogs.com/zerotoinfinity/p/17027322.html

相关文章

  • Unity+Pico 响应射线事件
    1、添加组件为了让场景内的物体能够响应射线的操作,需要在该物体上添加“XRSimpleInteractable”组件,并对射线的交互事件编写脚本看,最常用的是“Hover”和“Select”事件......
  • UnityShader入门精要学习 第二章解惑
    困惑什么是OpenGL、DirectX如果开发者直接访问GPU是一件非常麻烦的事情,我们可能需要和各种寄存器、显存打交道。而图像编程接口再这些硬件的基础上实现了一层抽象。Ope......
  • Unity UI显示3D模型,控件与屏幕分辨率不同,导致在屏幕上鼠标点选模型,无法选中模型的问题
    ​UI界面显示3d模型,需要添加模型相机,通过中间RenderTexture来连接相机与界面的承载容器【RawImage】,根据项目在显示时,会对界面做适当调整,但是RawImage的宽和高会产生运行时......
  • Unity优化 - 总览
    基于Unity移动端游戏性能优化简谱-UWA整理而得,不定期更新。细节部分可移步UWA相关博文查阅优化总览Q&A图片放大看不清?A.图片右键->新标签页打开图片->放大B.右键......
  • 【Unity TIL】7. 手动刷新资源
    背景最近的项目,Unity版本是2020.3,电脑是mac,不清楚是哪个的原因,unity经常崩溃。代码敲着敲着就崩溃,关闭了运行时编译代码还是崩,后来就直接关了AutoRefrash,崩溃的问题......
  • [UnityAI]行为树的中断机制
    参考链接:https://www.cnblogs.com/01zxs/p/9863715.htmlhttps://blog.csdn.net/AcmHonor/article/details/123234763https://blog.csdn.net/u012632851/article/details......
  • Unity 2D入门基础教程
    http://www.raywenderlich.com/61532/unity-2d-tutorial-getting-started,作者:ChristopherLaPollo,由Xiaoke翻译,如果用以前版本的Unity做2D游戏,虽然能做,但是......
  • 【Unity TIL】6. 如何判断两条线段是否相交
    AABB碰撞检测,也就是轴对齐碰撞检测,用平行于x,y轴的矩形表示物体。如何判断两个矩形是否相撞,可以通过分别判断x,y轴上的线段是否相交。假设线段分别为(s1,e1),(s2,e2),判......
  • Unity初始界面设计与人物移动代码
    初始界面设计的比较简洁,只有三个按键,开始游戏,设置,退出,一张背景。本来想整一个玩家自定义按键,但是觉得太麻烦且没必要。做到现在的感受就是,遇到不会的地方,网上的教程五花......
  • Unity URP管线如何实现屏幕后处理
    URP管线可扩展的自己的RendererFeature,这里写了一个屏幕后处理的demo,首先shader如下:1Shader"MyURP/Kerry/PostProcess/URPFeature_PostProcessDemo"2{3Pro......