Using serial makes game very laggy
I've been trying to use a bluetooth module and an accelerometer connected to an Arduino Board to use as a controller in Unity. The serial baud rate is set to 9600. It sends me x and y coordinates that I store in a float array. The data is successfully sent and received. My problem is that whenever I press play, the game is VERY slow. The game is hardly playable, everything is just so slowed down. If I desactivate the script using the serial, the game is back to its normal speed. Any idea of how I could fix this ? My code is very simple (see below).
public class Moving: MonoBehaviour
{
SerialPort serial = new SerialPort("COM7", 9600);
public float[] data = new float [2];
void Start()
{
}
void Update()
{
if (!serial.IsOpen)
serial.Open();
Move();
}
void Move()
{
data[0] = float.Parse(serial.ReadLine());
data[0] = data[0] / 10;
data[1] = float.Parse(serial.ReadLine());
data[1] = data[1] / 10;
transform.position = new Vector3(data[0], data[1], 0);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612150305im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How Do I Activate The Switch Statement 1 Answer
Unity hangs when I press play (serial port, unity + arduino) 0 Answers
Reading data from serial port causes unity to hang 1 Answer
Lags in editor 0 Answers