PICO 推摇杆移动

SDK版本

Pico UnityXR SDK v2.0.5  

推摇杆,根据摇杆的偏移量,每帧进行移动和旋转

右手的摇杆,如果要想左手,第17行代码改成XRNode.LeftHand

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.XR;
using UnityEngine.XR;
public class PlayerMove : MonoBehaviour
{
    public Transform player;     //用户
    public Transform camera_;       //摄像机
    //摄像机的子物体,本地位置是(0,0,1)   
    public Transform cameraFront;    
    
    void Update()
    {
        Vector2 vecRight2dAxis = Vector2.zero;
        //获取摇杆的数据
UnityEngine.XR.InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.primary2DAxis, out vecRight2dAxis);

        if (vecRight2dAxis.y!=0)
        {
            //获得用户前方的向量,长度为1,这个向量就可以作为移动的单位向量
            Vector3 front = cameraFront.position - camera_.position;
            //只移动X Z ,不移动 Y
            Vector3 front_ = new Vector3(front.x, 0, front.z);
            player.position += vecRight2dAxis.y * Time.deltaTime * front_*0.45f;
            //摇杆的 横向偏移量,给到用户一个旋转
            player.Rotate(vecRight2dAxis.x * Vector3.up * Time.deltaTime * 4);
        }
    }
}

你可能感兴趣的:(Pico,VR)