- Home /
Transform Unity Object with Arduino
hey guys hope you can help me! i try to use the serial numbers from my arduino board to transform the object in unity. on arduino site my code is really simple and looks like this:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int a = analogRead(A0);
//Sleep for 50ms, which provides the recommended sample rate (20Hz)
delay(500);
Serial.println(a);
}
in the serial monitor from arduino i get these values (picture)
now i want to use these values to constantly transform my geometry (a cube) in unity. for example if the value from arduino is 50 so the cube should be scaled by 50
do you know how this can work???
thanks in advance
Answer by Glurth · Oct 11, 2018 at 04:32 PM
You need to put that code in a custom MonoBehavior class. Then, you will be able to make that loop function, your monobehavior's "Update" function instead (and setup() stuff inside the monobehavior's Start() function). I would suggest you create a cube in your scene using the unity menu, then ADD this new monobehavior component you just created, to the gameobject.
Monobehavior classes, when attached to a game object as a component, can access the object's Transform. This is how you will change it's size, inside Update():
transform.localScale= Vector3.one * analogRead(A0);
hey thanks for your answer....so it doesnt sound that easy any more :D
do you think (on unity side) it could work with something like this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class Scale : $$anonymous$$onoBehaviour
{
private float analogRead;
SerialPort sp = new SerialPort("CO$$anonymous$$3", 9600);
// Use this for initialization
void Start()
{
sp.Open();
sp.ReadTimeout = 1;
}
// Update is called once per frame
void Update()
{
analogRead = analogRead;
if (sp.IsOpen)
{
ScaleObject(sp.ReadByte());
}
}
void ScaleObject(int analogRead)
{
transform.localScale = Vector3.one * analogRead;
}
}
kind regards
yes, that looks good to me, how does it work?
$$anonymous$$ay I suggest you make the analogRead variable, public ins$$anonymous$$d of private? That way, you will be able to see its value change, in the unity editor inspector.
$$anonymous$$eep in $$anonymous$$d: the Update function only runs when you hit "play" in the editor (or run the final build). If you want it to execute BEFORE you hit play, while in the editor, you will need to apply the ExecuteInEdit$$anonymous$$ode attribute to your monobehavior class.
Answer by cgressel · Oct 11, 2018 at 09:38 PM
yeah tried to change it to public but you can not see a change, i mean it gets closer but still not the thing that i want
now i have the problem that the geometry gets some kind of "disco flash light"-effect because the scale always goes back to the original value...for example 34,1,23,1,33,1,12,1 and so on
never thought that this could be so tricky
Your answer
Follow this Question
Related Questions
Sending Data From Unity To Arduino 0 Answers
Two Arduino Boards for Same project 0 Answers
Arduino with Unity: Bad Framerate! 3 Answers
send string to serial port on android 0 Answers