Hey everyone! Let me start by saying I’m not a very good programmer and recently I got stuck trying to figure out a bug in a game I’m making in Unity. I’m using an ArduinoEsp32 with an MPU6050 alongside the Uduino plugin to read the player movements in the game(It’s basically a Plane that can move up or down depending on if I lift the Arduino sensor in one of those directions).
But I don’t know how. But there is a nasty bug that is changing the direction of the plane movements a couple of seconds after the game starts. It can also occur with I plunge the the board out and put it in again. It is also strange that whenever I alter the code. He keeps working fine. But if I disconnect the movements get inverted and after a couple of seconds the plane slowly starts to get slower until stops moving but then he returns back to the " normal " movement of before. Is like X, Y, and Z are moving around themselves from time to time. I don’t have an idea how to solve this.
Any thoughts on the matter? Here’s the scripts I’m using:
This one is from the IMU
using UnityEngine;
using Uduino;
public class ReceiveIMUValues : MonoBehaviour
{
public Vector3 sensitivity = new Vector3(1.0f, 1.0f, 1.0f);
public string imuName = "r";
public float speedFactor = 15.0f;
private Quaternion currentRotation;
public float imuX;
public float imuY;
public float imuZ;
void Start()
{
UduinoManager.Instance.OnDataReceived += ReadIMU;
}
public Quaternion GetRotation()
{
return currentRotation;
}
public void ReadIMU(string data, UduinoDevice device)
{
string[] values = data.Split('/');
if (values.Length == 5 && values[0] == imuName)
{
float w = float.Parse(values[1]);
float rawImuX = float.Parse(values[2]);
float rawImuY = float.Parse(values[3]);
float rawImuZ = float.Parse(values[4]);
imuX = rawImuX * sensitivity.x;
imuY = rawImuY * sensitivity.y;
imuZ = rawImuZ * sensitivity.z;
Quaternion newRotation = new Quaternion(imuX, imuY, imuZ, w);
currentRotation = Quaternion.Slerp(currentRotation, newRotation, Time.deltaTime * speedFactor);
}
}
}
And this one is for the plane movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoveObject : MonoBehaviour
{
public float speed = 10.0f;
public Vector3 minBounds = new Vector3(-10.0f, -10.0f, -10.0f);
public Vector3 maxBounds = new Vector3(10.0f, 10.0f, 10.0f);
private ReceiveIMUValues receiveIMUValues;
public Slider sensitivitySlider; // Slider de sensibilidade
void Start()
{
receiveIMUValues = FindObjectOfType<ReceiveIMUValues>();
}
void Update()
{
float arduinoValueX = receiveIMUValues.imuY;
float arduinoValueY = receiveIMUValues.imuX;
float sensitivity = sensitivitySlider.value;
arduinoValueX *= sensitivity;
arduinoValueY *= sensitivity;
Vector3 movement = new Vector3(arduinoValueX, arduinoValueY, 0) * speed * Time.deltaTime;
Vector3 newPosition = transform.position + movement;
newPosition = Vector3.Max(newPosition, minBounds);
newPosition = Vector3.Min(newPosition, maxBounds);
transform.position = newPosition;
}
}