using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
//using Unity.XR;    //必须引入

public class ControlObjects : MonoBehaviour
{
    private float xSpeed = 100.0f;//左右旋转速度
    private float x = 0.0f;

    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
        //右控制器:移动偏移量
        Vector2 vecRight2dAxis = Vector2.zero;
        InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.primary2DAxis, out vecRight2dAxis);
        transform.position = new Vector3(
            transform.position.x + vecRight2dAxis.x * Time.deltaTime, 
            transform.position.y, 
            transform.position.z + vecRight2dAxis.y * Time.deltaTime);
        //左控制器:旋转偏移量
        Vector2 vecLeft2dAxis = Vector2.zero;
        InputDevices.GetDeviceAtXRNode(XRNode.LeftHand).TryGetFeatureValue(CommonUsages.primary2DAxis, out vecLeft2dAxis);
        x += vecLeft2dAxis.x * xSpeed * 0.02f;

        //欧拉角转化为四元数
        Quaternion rotation = Quaternion.Euler(0, x, 0);
        transform.rotation = rotation;
    }
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐