2
\$\begingroup\$

This question relates to this link: https://www.freetronics.com.au/blogs/news/6537043-experimenting-with-unity-processing-and-arduino#.W2-SFs4zbIU Does anyone have any idea how I would begin doing this? I cannot find the tutorial or code that relates to this. I have a cube on unity that changes colour and texture according to the input from a force sensing resistor. How would I get the cube to react in this way (as in the video)? Any assistance will be appreciated

In this video, a flexiforce sensor is housed in a cube. When the user presses the sensor, a cube on Unity reacts in the same manner that the real cube is squeezed; its basically a virtual version of what is happening to the cube in real life

\$\endgroup\$
  • \$\begingroup\$ How about this? I didn't get to the bottom but apparently Unity ha a SerialPort class to handle that alanzucconi.com/2015/10/07/how-to-integrate-arduino-with-unity \$\endgroup\$ – JoulinRouge Aug 12 '18 at 9:30
  • \$\begingroup\$ I'm unable to view the link & video in their entirety from my current net connection. To make your question easier to understand for users in similar situations, it would help to edit your question to describe what you mean by "doing this" or "react in this way" in words. This also makes your question more searchable for users with relevant expertise, or users trying to solve similar problems. \$\endgroup\$ – DMGregory Aug 12 '18 at 12:18
  • \$\begingroup\$ I have edited the question to add some more information to my query \$\endgroup\$ – user19964 Aug 13 '18 at 1:37
  • \$\begingroup\$ So just to clarify: you have the cube/unity connection working, you just want to figure out how to change the cube's shape? \$\endgroup\$ – The Guy with The Hat Aug 14 '18 at 23:11
  • \$\begingroup\$ Yes, I have the cube/unity and arduino part working, I just want to know how to make the cube change shape as the force is exerted on the cube. Currently the cube changes color and texture as different levels of force is exerted on it \$\endgroup\$ – user19964 Aug 14 '18 at 23:17
0
\$\begingroup\$

This works well:

     using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System.IO;
public class arduino : MonoBehaviour
{

    public SerialPort sp = new SerialPort("\\\\.\\COM5", 9600);// Com port and the baud rate of the arduino
    Material m_Material;
    GameObject Sphere;



    void Awake()
    {
        Sphere = GameObject.FindWithTag("Player");
        m_Material = GameObject.FindWithTag("Player").GetComponent<Renderer>().material;
    }
    void Start()
    {
        if (!sp.IsOpen)
        {
            sp.Open(); // Open 
        }
        sp.ReadTimeout = 250; // Timeout for reading 

    }

    // Update is called once per frame
    void Update()
    {
        if (sp.IsOpen)
        { // Check to see if the serial port is open 
            try
            {

                //string portreading = sp.ReadLine(); // get the string output of the serial port 
                //float amount = int.Parse(portreading);
                string value = sp.ReadTo("EOL"); //Read the information
                float amount = float.Parse(value);


                if ((amount > 251f))
                {
                    m_Material.color = Color.red;
                    Renderer _rend = Sphere.GetComponent<Renderer>();
                    Sphere.GetComponent<Renderer>().material = _rend.material;
                    _rend.material.mainTexture = Resources.Load("face1") as Texture;
                    Sphere.transform.localScale = new Vector3( 2f, 0.03f*amount, 2f);
                    //Sphere.transform.position = new Vector3(2f, 2f , 2f);
                }
                else
                {
                    m_Material.color = Color.white;
                    Sphere.transform.localScale = new Vector3(2f, 10f, 8f);

                }
            }
            catch (System.Exception)
            {

            }
        }
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.