c# - 将 Arduino 与 Unity 一起使用(Arduino Nano 33 IoT)?
问题描述
我将 Unity 用于一个简单的游戏。我的桨只能上下移动。由于笔记本电脑的上下键,我实际上使用 Vectors 来移动桨。我有一个通过 USB 连接的 arduino Nano 33 IoT,我想知道是否可以直接使用 Unity 从 arduino 获取数据(陀螺仪)?
我没有找到好的教程来做到这一点......而且我很乐意移动我的桨,这要归功于这个没有我电脑钥匙的 arduino。
我阅读了文档,我发现我必须使用集合“ System.IO.Ports; ”,指定我使用的 COM 端口,陀螺仪在 x、y 和 z 中的位置......
这是移动播放器的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float moveSpeed = 8.0f;
public float topBounds = 8.3f;
public float bottomBounds = -8.3f;
public Vector2 startingPosition = new Vector2(-13.0f, 0.0f);
[Space]
[Header("Game")]
[SerializeField] private Game game;
void Start()
{
transform.localPosition = startingPosition;
}
void Update()
{
if (game.gameState == Game.GameState.Playing)
{
CheckUserInput();
}
}
void CheckUserInput()
{
if (Input.GetKey(KeyCode.UpArrow))
{
if (transform.localPosition.y >= topBounds)
{
transform.localPosition = new Vector3(transform.localPosition.x, topBounds, 0);
}
else
{
transform.localPosition += Vector3.up * moveSpeed * Time.deltaTime;
}
}
else if (Input.GetKey(KeyCode.DownArrow))
{
if (transform.localPosition.y <= bottomBounds)
{
transform.localPosition = new Vector3(transform.localPosition.x, bottomBounds, 0);
}
else
{
transform.localPosition += Vector3.down * moveSpeed * Time.deltaTime;
}
}
}
}
解决方案
所以我从来没有使用过统一,但经过一些研究,我发现这篇文章向您展示了如何使用统一应用程序连接到串行端口。 https://www.alanzucconi.com/2015/10/07/how-to-integrate-arduino-with-unity/
您需要做的第二件事是制作一个 arduino 应用程序,该应用程序将您的坐标字符串也写入串行端口。有很多关于这方面的教程。 https://create.arduino.cc/projecthub/Aritro/getting-started-with-imu-6-dof-motion-sensor-96e066
最后,在您的统一程序中,您只需解析字符串并将其用作输入。祝你好运。
推荐阅读
- java - 使用 Jhipster 和 Spring 配置 Intellij 代码辅助
- javascript - 使用 Angular 从安全端点流式传输视频
- android - 消除或更改“最近使用的应用程序”屏幕上显示的操作栏
- r - r data.table lapply 与多个 SDcols
- gitlab - 何时:下游管道中的手动导致上游报告失败
- prolog - Prolog(CLP)的可变装箱问题
- xml - Postgres XML 解析节点名
- azure-databricks - 有没有办法在 Azure Databricks 中恢复已删除的数据?
- javascript - Threejs 模型加载:“CORS 策略已阻止从源‘null’访问 XMLHttpRequest ..”-如何在本地进行测试?还是简单上传?
- .net - 发布请求期间出现角度 ERR_CONNECTION_REFUSED 错误