- Home /
Arduino to Unity 3D communication Game Interface
How do you get multiple outputs from Arduino to read on Unity and successfully apply those outputs to work in the game interface?
Background: I have three potentiometers outputting data to an Arduino nano, which is connected to Unity. Arduino produces 3 columns of values for each potentiometer. The plan is to use the potentiometers to extend an object in Unity in X,Y,Z plane simultatneously. I have come across a problem where I am unable to get all of the potentiometers to work simulatenously on the game interface of Unity. I was able to succeed with one potentiometer to extend an object only in one plane, but cannot get them all to work. I am pretty sure the problem lies in the C# code, since I am a beginner in programming. How do I fix the code to allow all the output values to be processed into the game interface of Unity.
Here is my Arduino code
float value = 0;
boolean flip = false;
void setup() {
Serial.begin(9600);
}
void loop() {
if(value >= 1023)
{
flip = true;
}else if(value <= 0)
{
flip = false;
}
if(flip == false)
{
value += 1;
}else{
value -= 1;
}
float values[] = {0,0,value};
Serial.flush(); //clears any possible left over information
Serial.print(map(analogRead(A0),0,1023,0,50)); //Resizes values from the range on arduino input to 360 of rotation.
Serial.print(",");
Serial.print(map(analogRead(A1),0,1023,0,50));
Serial.print(",");
Serial.print(map(analogRead(A2),0,1023,0,50));
Serial.println();
}
C# Code
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class XBehavior : MonoBehaviour
{
SerialPort serial = new SerialPort("COM3", 9600);
void Update()
{
if (!serial.IsOpen)
serial.Open();
int grow = int.Parse(serial.ReadLine());
transform.localEulerAngles = new Vector3(0, grow, 0);
transform.localScale = new Vector3(1, 1 + grow++, 1);
transform.localScale = new Vector3(1 + grow++, 1, 1);
}
}