- Home /
Arduino Unity processing
Hello all,
This question refers to the link below: https://freetronicsblog.wordpress.com/2012/09/06/experimenting-with-unity-processing-and-arduino/
How would I go about doing this? currently I have a cube that responds to a force sensor. The cube changes colour and texture according to the force exerted. How would I do something like in the link?
I have tried the code below but the cube just disappears when i press the force sensor: using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO.Ports; using UnityEngine.SceneManagement;
public class Change_scale : MonoBehaviour
{
SerialPort stream = new SerialPort("\\\\.\\COM6", 9600);
GameObject Cube;
Material m_Material;
private object go;
// Renderer rend;
void Awake()
{
stream.Open(); //Open the Serial Stream.
stream.ReadTimeout = 10; //Give a Timeout to Serial Stream Reading
Cube = GameObject.FindWithTag("Player");
//m_Material = GameObject.FindWithTag("Player").GetComponent<Renderer>().material;
// go = GameObject.FindWithTag("Player").GetComponent<Renderer>();
}
void Start()
{
GameObject go = GameObject.Find("Cube");
}
void Update()
{
try
{
string value = stream.ReadTo("EOL"); //Read the information
float intensity = float.Parse(value);
Debug.Log(intensity);
if (intensity > 20)
{
Cube.transform.localScale = new Vector3(intensity, 0f, 0f);
// m_Material.color = Color.grey;
}
else
{
Cube.transform.localScale = new Vector3(0f, 0f, 0f);
}
if (intensity > 26)
{
Cube.transform.localScale = new Vector3(intensity, 0f, 0f);
// m_Material.color = Color.blue;
}
else
{
Cube.transform.localScale = new Vector3(0f, 0f, 0f);
}
stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
}
catch (System.Exception e)
{
Debug.Log("Error: " + e);
}
}
}
Can someone tell me how to fix this so that the cube in unity responds to the sensor by changing shape in the same manner that the real cube would (as in the video)?
Answer by Bunny83 · Sep 19, 2018 at 11:28 PM
You set the scale to "0" which makes no sense. That would squish the cube to the size of 0. The default scale is "1,1,1". So instead of 0f,0f,0f
you should use 1f,1f,1f
and instead of intensity, 0f, 0f
you should use intensity, 1f, 1f
![alt text][1]
Thank you for your reply. I understand what you are saying and i have changed the code accordingly. However, now the cube stretches outwardly as depicted in the picture. Would i have to make the values 0.1 or something to get a reduction in the cube size? [1]: /storage/temp/124894-1.png