- Home /
Is there a better way to send data from Arduino to Unity?
Right now to send data to unity from arduino i'm writing 1 or 2 to the serialport (i.e. Serial.write(1); in arduino). I was wondering if I can send that plus another value (i.e speed) which is constantly changing? Below is my unity code and I wanted to make the variable 'speed' change with the speed calculated in unity! Any suggestions/ideas?
using UnityEngine; using System.Collections; using System.IO.Ports;
public class cammove : MonoBehaviour {
public float speed;
public float MoveBy;
SerialPort sp = new SerialPort("COM4",9600);
// Use this for initialization
void Start () {
sp.Open();
sp.ReadTimeout = 1;
}
// Update is called once per frame
void Update () {
MoveBy = speed * Time.deltaTime;
if (sp.IsOpen){
try
{
MoveCamera (sp.ReadByte());
}
catch (System.Exception)
{
}
}
}
void MoveCamera (int Direction) {
if (Direction == 1) {
transform.Translate (Vector3.right * MoveBy, Space.World);
}
if (Direction == 2) {
transform.Translate (Vector3.left * MoveBy, Space.World);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Cloud recognition in Vuforia 0 Answers
How to read two separate values from the same SerialPort? 1 Answer
Lag issues with unity3d + Arduino (Xbee) 0 Answers
Making Cross Hair Show Red when over item tagged Enemy 1 Answer
2D Games in Unity3D? 1 Answer