【问题标题】:Using Arduino with Unity (Arduino Nano 33 IoT)?将 Arduino 与 Unity 结合使用(Arduino Nano 33 IoT)?
【发布时间】:2021-10-28 15:59:45
【问题描述】:

我将 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;
            }

        }
    }
}

【问题讨论】:

    标签: c# unity3d arduino gyroscope


    【解决方案1】:

    所以我从来没有使用过统一,但通过一些研究我发现这篇文章向您展示了如何使用统一应用程序连接到串行端口。 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

    最后,在您的统一程序中,您只需解析字符串并将其用作输入。祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2021-12-27
      • 2022-07-14
      相关资源
      最近更新 更多