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

如何基于注意力与冥想数值创建Unity角色控制脚本?

基于Mindwave脑波数据实现角色移动控制

一、获取Focus与Meditation数值的逻辑

原项目里的MindwaveManager会实时维护一个DataModel实例,其中的attention(注意力)和meditation(冥想)字段就是你需要的数值——这两个是设备直接返回的核心数据,对应炸弹脚本里用到的Focus和Meditation变量。

要在你的角色脚本中获取这些值,只需要引用场景中的MindwaveManager实例,访问它的CurrentData属性即可。

二、修改角色移动脚本

把原WASD输入逻辑替换为脑波阈值判断逻辑,完整修改后的脚本如下:

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

public class Player : MonoBehaviour
{
    [SerializeField]
    private float _moveSpeed = 5f;
    [SerializeField]
    private float _gravity = 9.81f;

    private CharacterController _controller;
    private MindwaveManager _mindwaveManager;

    void Start()
    {
        _controller = GetComponent<CharacterController>();
        // 获取场景中的MindwaveManager实例
        _mindwaveManager = FindObjectOfType<MindwaveManager>();
    }

    void Update()
    {
        float horizontalInput = 0f;
        float verticalInput = 0f;

        // 确保设备已连接且MindwaveManager可用
        if (_mindwaveManager != null && _mindwaveManager.IsConnected)
        {
            var currentData = _mindwaveManager.CurrentData;
            
            // 注意力控制前进/后退
            if (currentData.attention >= 75)
            {
                verticalInput = 1f;
            }
            else if (currentData.attention <= 40)
            {
                verticalInput = -1f;
            }

            // 冥想控制左移/右移
            if (currentData.meditation >= 75)
            {
                horizontalInput = 1f;
            }
            else if (currentData.meditation <= 40)
            {
                horizontalInput = -1f;
            }
        }

        Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
        direction.y -= _gravity;

        _controller.Move(direction * _moveSpeed * Time.deltaTime);
    }
}

三、注意事项

  • 确保场景中已添加MindwaveManager组件,且设备连接配置正确
  • 可根据实际操作体验调整_moveSpeed或阈值数值,优化移动手感
  • 若需要平滑移动效果,可添加Mathf.Lerp插值逻辑,避免移动状态突然切换

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 06:27:49