You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oculus Quest 2医疗沉浸式可视化应用输入冲突及多控制方案求助

解决Input System冲突并实现Oculus手部+键盘双控制方案

一、先修复Input系统冲突错误

你遇到的InvalidOperationException是因为项目启用了Unity新Input System,但mouseLook.cs仍在使用旧的UnityEngine.Input API(比如Input.GetAxis)。优先推荐迁移到新Input System,具体改造步骤:

  1. 在Unity编辑器中创建新的Input Action Asset(右键菜单 > Input Actions),添加Look动作,绑定Mouse的X/Y轴输入。
  2. 替换原mouseLook.cs为新Input System实现:
using UnityEngine;
using UnityEngine.InputSystem;

public class MouseLook : MonoBehaviour
{
    public float sensitivity = 100f;
    public Transform playerBody;
    private float xRotation = 0f;
    private PlayerInput _playerInput;
    private InputAction _lookAction;

    void Awake()
    {
        _playerInput = GetComponent<PlayerInput>();
        _lookAction = _playerInput.actions["Look"];
    }

    void Update()
    {
        Vector2 lookInput = _lookAction.ReadValue<Vector2>();
        
        float mouseX = lookInput.x * sensitivity * Time.deltaTime;
        float mouseY = lookInput.y * sensitivity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);
    }
}
  1. 给XR Rig的Camera Offset对象添加PlayerInput组件,关联你创建的Input Action Asset,默认控制方案选键鼠类。

二、实现Oculus手部+键盘控制切换

要让用户按需选择控制方式,核心是利用新Input System的多Action Map切换功能:

1. 配置双Action Map

在Input Action Asset中创建两个Action Map:

  • OculusHandControl:绑定Oculus手柄输入,比如用摇杆控制视角移动、扳机键触发模型交互;
  • KeyboardMouseControl:绑定WASD移动、鼠标视角、键盘按键控制模型旋转/缩放。

2. 添加控制切换逻辑

给XR Rig添加切换脚本,绑定一个触发按键(比如键盘F键或Oculus手柄菜单键):

using UnityEngine;
using UnityEngine.InputSystem;

public class ControlSwitcher : MonoBehaviour
{
    private PlayerInput _playerInput;
    private InputAction _switchAction;

    void Awake()
    {
        _playerInput = GetComponent<PlayerInput>();
        _switchAction = _playerInput.actions["SwitchControl"]; // 提前绑定切换触发键
    }

    void Update()
    {
        if (_switchAction.WasPressedThisFrame())
        {
            var targetMap = _playerInput.currentActionMap.name == "OculusHandControl" 
                ? "KeyboardMouseControl" 
                : "OculusHandControl";
            
            _playerInput.SwitchCurrentActionMap(targetMap);
            // 可选:添加UI提示当前控制模式
        }
    }
}

3. 适配XR与键盘导航逻辑

  • Oculus手部控制:用XR Interaction Toolkit的XR Ray Interactor或Direct Interactor实现模型的抓取、旋转等交互,绑定到OculusHandControl的动作上;
  • 键盘控制:在导航脚本中读取键盘动作,实现模型的平移/旋转/缩放,示例代码:
using UnityEngine;
using UnityEngine.InputSystem;

public class ModelNavigation : MonoBehaviour
{
    public Transform targetModel;
    public float rotateSpeed = 2f;
    private PlayerInput _playerInput;
    private InputAction _rotateLeft;
    private InputAction _rotateRight;

    void Awake()
    {
        _playerInput = GetComponent<PlayerInput>();
        _rotateLeft = _playerInput.actions["RotateLeft"]; // 绑定A键
        _rotateRight = _playerInput.actions["RotateRight"]; // 绑定D键
    }

    void Update()
    {
        if (_playerInput.currentActionMap.name != "KeyboardMouseControl") return;

        if (_rotateLeft.IsPressed())
        {
            targetModel.Rotate(Vector3.up, -rotateSpeed * Time.deltaTime);
        }
        if (_rotateRight.IsPressed())
        {
            targetModel.Rotate(Vector3.up, rotateSpeed * Time.deltaTime);
        }
        // 可扩展WASD平移、滚轮缩放等逻辑
    }
}

三、关键注意事项

  • 确保Player Settings中Active Input Handling设置为Input System Package (New);
  • 安装最新版的Oculus Integration和XR Interaction Toolkit包,避免兼容性问题;
  • 真机测试前,在XR Plug-in Management中启用Oculus插件;
  • 切换控制模式时,可添加UI提示(比如Canvas文本)让用户明确当前模式。

内容的提问来源于stack exchange,提问作者Mass17

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 00:21:43