英文:
How to create a Сharacter Сontrol script based on specific numeric variables?
问题
我不是一位有经验的程序员,特别是在Unity和C#方面。但我在GitHub上找到了一个Unity项目(https://github.com/hubertgdev/mindwave-unity),使用Neurosky Mindwave 2 BCI设备,你可以控制炸弹的爆炸。我有这个BCI设备,一切都已经连接好了。关注度和冥想值(整数)用来控制炸弹的爆炸或引线熄灭。我认为通过类似的逻辑,我可以使用关注度和冥想值来移动角色。
例如,如果关注度> = 75 -> 角色向前移动。如果关注度<= 40 -> 角色向后移动。如果冥想> = 75 -> 角色向右移动,如果冥想<= 40 -> 角色向左移动。
我创建了一个简单的游戏,游戏地图上有一个胶囊体。角色的移动使用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;
void Start()
{
_controller = GetComponent<CharacterController>();
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
direction.y -= _gravity;
_controller.Move(direction * _moveSpeed * Time.deltaTime);
}
}
我还能够连接MindwaveManager元素,这使我可以将我的BCI连接到游戏并在屏幕上实时显示所有数据。
感谢GitHub仓库的作者,我拥有所有必要的脚本,但由于缺乏经验和知识,我完全不知道如何正确使用它们来创建我构想的角色控制方法。我需要帮助。
还知道BCI的数据以数据模型的形式传递到程序中,其中包括与不同脑波相关的整数值,如Delta、theta、lowalpha、highalpha、lowbeta、highbeta、lowgamma、HighGamma(请参阅GitHub)。但由于我不了解,我没有看到作者是如何创建用于激活炸弹的关注度和冥想变量的脚本。
如果您需要更多信息,请使用GitHub链接,因为我无法在这里放置所有脚本...主要脚本位于路径Assets\Demo\Scipts和Assets\Plugins\MindwaveUnity\Core,并且它们有有用的注释。
如果您能向我展示如何创建这样的角色控制脚本,我将不胜感激。
英文:
I am not an experienced programmer, especially in Unity and c#. But I found a unity project on GitHub (https://github.com/hubertgdev/mindwave-unity) in which, using the Neurosky Mindwave 2 BCI device, you can control the explosion of a bomb. I have this BCI and everything is connected. Values (integers) of attention and meditation are used to make the bomb explode or the wick will go out. I thought that by using similar logic, I can move the character using the values of attention and meditation.
For example, if attention >= 75 -> character moves forward. If attention <= 40 -> character moves reverse. If meditation >= 75 -> character moves to the right, If meditation <= 40 -> character moves to the left.
I created a simple game that has a capsule on a flat map. The simplest script for movement using the WASD buttons is linked to the capsule.
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;
void Start()
{
_controller = GetComponent<CharacterController>();
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
direction.y -= _gravity;
_controller.Move(direction * _moveSpeed * Time.deltaTime);
}
}
I was also able to connect the MindwaveManager element, which allows me to connect my BCI to the game and display all the data in real-time on the screen.
Thanks to the author of the github repository, I have all the necessary scripts, but due to the lack of experience and knowledge, I have absolutely no idea how to use them correctly to create the character control method I conceived. With this I need help.
It is also known that data from BCI gets to the program in the form of dataModel with int
values related to different brain waves, such as Delta, theta, lowalpha, highalpha, lowbeta, highbeta, lowgamma, HighGamma.(see github) But I haven't seen (due to lack of knowledge) how author created the Focus and Meditation variables used script for activating bomb.
If you need more information, please use the GitHub link, because I cannot put all the scripts here... the main scripts are located in the path Assets\Demo\Scipts
and Assets\Plugins\MindwaveUnity\Core
and they have useful comments.
I would appreciate it if you could show me how to create such a character control script.
答案1
得分: 1
以下是代码的翻译部分:
[SerializeField]
private float _moveSpeed = 5f;
[SerializeField]
private float _gravity = 9.81f;
[Range(0, 75)]
[SerializeField] private float attention;
[Range(0, 75)]
[SerializeField] private float meditation;
private CharacterController _controller;
void Start()
{
_controller = GetComponent<CharacterController>();
}
void Update()
{
// axis calculation formula
var z = Mathf.Clamp((attention - 40) / 35, -1f, 1f);
var x = Mathf.Clamp((meditation - 40) / 35, -1f, 1f);
// Set transform to local scape
var direction = transform.TransformDirection(new Vector3(x, 0, z));
_controller.Move(direction * (_moveSpeed * Time.deltaTime));
}
请注意,上述代码部分未被翻译,只提供了原始的C#代码。
英文:
The formula below will help you to move the character according to the above definition.
[SerializeField]
private float _moveSpeed = 5f;
[SerializeField]
private float _gravity = 9.81f;
[Range(0, 75)]
[SerializeField] private float attention;
[Range(0, 75)]
[SerializeField] private float meditation;
private CharacterController _controller;
void Start()
{
_controller = GetComponent<CharacterController>();
}
void Update()
{
// axis calculation formula
var z = Mathf.Clamp((attention - 40) / 35, -1f, 1f);
var x = Mathf.Clamp((meditation - 40) / 35, -1f, 1f);
// Set transform to local scape
var direction = transform.TransformDirection(new Vector3(x, 0, z));
_controller.Move(direction * (_moveSpeed * Time.deltaTime));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论