Inspired by the possibilities I saw using p5.js, I thought I would try serial communication between the Arduino and Unity. I found this tutorial which helped me get started, but there were some missing pieces I had to add to accomplish my goal, which was to use four pushbuttons to maneuver a character up, down, left, right, and diagonally.
The Arduino side isn't complicated: I needed a strategy for sending relevant information to Unity so that the character would move only for as long as I pressed the pushbuttons. At first, I sent single bytes with
Serial.write() corresponding to the directions I wanted the character to move. But I learned I risked overflowing the buffer because I was writing a byte every loop as long as I was pressing the corresponding button.
On the Unity side, I came across a peculiar problem as well. While I was able to read the bytes from the Arduino and convert them into character movements, I had trouble moving the character at a predictable speed. In Unity, it's typical to increment the position of an object in response to player commands using something like
yPos += speed * Time.deltaTime, where
Time.deltaTime is the time that passes between each frame in a single second. In other words, if the game runs at 60fps,
Time.deltaTime is 1/60s.
But in my code, I was updating the position of the character every frame, and for every byte read by the buffer. In other words, given a buffer either filled with hundreds of bytes that each increment position or nothing at all, it was difficult to scale the
speed parameter appropriately.
Handshaking seemed to fix issues on both sides. I moved the button digital reads inside a conditional checking
Serial.available() > 0 on the Arduino, and added a ping function that writes a byte to serial in the beginning of each
Update() loop in Unity. With these additions, the frequency at which the buffer is filled and read is controlled to the rate which the frame updates, meaning I can use
speed to scale movement.
Another improvement I tried making after discussing with Tom was to deliver the button states in 4-byte arrays and calculating the appropriate movement in Unity. This way, I can better control what happens when the user presses, for example, the up and down buttons at the same time. Instead of privileging one direction over the other, I can just "cancel" the two opposing inputs out.
The Arduino code and an excerpt of the Unity code are shared below.