- Home /
how move cube in horizontal and vertical movement in unity using arduino
recently i have hack ps2 mouse and got x and y reading and i wanted to Visualize in Unity but there is error
IndexOutOfRangeException: Array index is out of range. dimension.Update () (at Assets/dimension.cs:84) here is my arduino code
include
/ Pin 5 data pin, 6 is the clock pin / PS2 mouse(6, 5); int newmx = 0; int newmy = 0; int zeroPin = 7; // to zero the counter
void mouse_init() { mouse.write(0xff); // reset mouse.read(); // ack byte mouse.read(); // blank / mouse.read(); // blank / mouse.write(0xf0); // remote mode mouse.read(); // ack delayMicroseconds(100); }
void setup() { pinMode(zeroPin, INPUT_PULLUP); Serial.begin(9600); mouse_init(); }
void loop() { char mstat; char mx; char my;
uint8_t Buf[14];
mouse.write(0xeb); mouse.read(); mstat = mouse.read(); mx = mouse.read(); my = mouse.read();
if ( digitalRead(zeroPin) == LOW) { newmy = 0; newmx = 0;
} else { newmx = newmx + mx; newmy = newmy + my; ;
Serial.print("\tx" );
Serial.print(newmx, DEC);
Serial.print(";");
Serial.print("\ty" );
Serial.print(newmy, DEC);
Serial.println("");
Serial.flush();
delay(25);
} }
and my unity code using UnityEngine; using System.Collections; using System.IO.Ports; public class axis : MonoBehaviour {
private Vector3 temp;
private SerialPort stream = new SerialPort(@"\\.\" + "COM6", 9600);
// Use this for initialization
void Start () {
stream.Open();
stream.ReadTimeout = 25;
StartCoroutine(readString());
}
// Update is called once per frame
void Update () {
}
IEnumerator readString() {
while (true) {
if (stream.IsOpen) {
try
{
string value = stream.ReadLine();
string[] values = value.Split(',');
float x = int.Parse(values[0]);
float y = -1 * (int.Parse(values[1]));
x = x / 10f;
y = y / 10f;
temp = transform.position;
temp.x = x;
temp.y = y;
transform.position = temp;
}
catch (System.Exception) {
}
}
yield return null;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612141641im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to determine the minimum distance between objects? 1 Answer
Should i use terrain generation or make it myself for android? 0 Answers
Instantiating GameObjects(enemies) far away from each other 0 Answers
[Please Help] 2D keeping an object within a fixed radius from main object 0 Answers
[Please Help] 2D keeping an object within a fixed radius from main object 0 Answers