I'm using Unity for a simple game. My paddle can only move up and down. I actually use Vectors for moving paddle thanks to key up and down with my laptop. I have an arduino Nano 33 IoT connected in usb and I want to know if it's possible to get data (gyroscope) from the arduino directly with Unity?
I don't find good tutorials to do that... And I would be very happy to move my paddle thanks to this arduino without my computer's keys.
I read the documentation, I found that I have to use the collection "System.IO.Ports;", specify the COM Port I use, positions in x, y & z for the gyroscope...
Here is the code for moving player:
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;
}
}
}
}
So i've never used unity but with a little research i found this article that shows you how to connect to a serial port using a unity app. https://www.alanzucconi.com/2015/10/07/how-to-integrate-arduino-with-unity/
The second thing you need is to make a arduino application that writes a string of your coordinates too a serialport. There are plenty of tutorials about that. https://create.arduino.cc/projecthub/Aritro/getting-started-with-imu-6-dof-motion-sensor-96e066
Finally in your unity program you just have to parse your string and use it as an input. I wish you luck.