dwilches / Ardity

Assets for integrating Arduino and Unity (or Unity and any hardware that communicates over a COM port)
329 stars 62 forks source link

Using the input from the Arduino to controll GameObject #41

Closed Iriemonkey closed 4 years ago

Iriemonkey commented 4 years ago

Hello,

i am fairly new to working with Arduino and Unity, and i don't really understand how i can use the input from the Arduino to control "stuff" ( movement of a Game Object for example ). I can print messages to the console in Unity by pressing a pushbutton on an Arduino circuit, but how do i use these messages to trigger something? (" e.g. save the value to a variable and move a cube, when the value is "1" .... i hope you get the point :/ )

Thank you in advance! kind regards, Tobi

dwilches commented 4 years ago

Hello,

Search around for how to move objects in Unity, then you can put that code inside of the methods that Ardity is executing.

For example, if you follow this guide try putting the following code inside of the method OnMessageArrived:

transform.Translate(Vector3.forward * Time.deltaTime);

Moreover, that parameter msg you see in the method OnMessageArrived can be used to move the object in different directions, just add an if and depending what the msg contains you can do:

transform.Translate(Vector3.forward * Time.deltaTime);

or:

transform.Translate(Vector3.back * Time.deltaTime);

Regards.