Hello Guys, I’am making a VR headset with Arduino and a game with unity. I wired my nano to an MPU-6050 gyro-accel sensor and send its values to unity and this tutorial’s code works pretty well on the arduino side. But I have a problem with the unity code here it is:
using UnityEngine;
using System.Collections;
using System;
using System.IO.Ports;
public class VR_Main : MonoBehaviour {
public float x;
public float y;
public float z;
SerialPort serial = new SerialPort("COM3", 115200);
void Start() {
serial.Open();
serial.ReadTimeout = 2000;
}
void Update() {
if (!serial.IsOpen)
serial.Open();
serial.Write("a");
float AcX = int.Parse(serial.ReadLine());
serial.Write("b");
float AcY = int.Parse(serial.ReadLine());
serial.Write("c");
float AcZ = int.Parse(serial.ReadLine());
serial.Write("d");
float GyX = int.Parse(serial.ReadLine());
serial.Write("e");
float GyY = int.Parse(serial.ReadLine());
serial.Write("f");
float GyZ = int.Parse(serial.ReadLine());
x = AcX / 190 * 0.5f;
y += GyZ / 3000;
z = AcY / 190 * 0.1f;
transform.localEulerAngles = new Vector3(-x, -y + 180, z);
}
}
and here is the problem:
When I move my VR it works well.
but when I’am not moving it keeps rotating on the Y axis slowly…
and the problem is in this lines of code:
x = AcX / 190 * 0.5f;
y += GyZ / 3000;
z = AcY / 190 * 0.1f;
as you can see when I’am using my Accelerometer I don’t use the " += " I use " = " but when I use the gyroscope I must use the " += " to move if I remove the “+” it wont rotate on its own but also if I move my head it wont move…
Do you have any idea how to solve this ?
Thanks…