Interface Lab Week 2: Unity + Arduino

August 4, 2020

For our second week we had to create a one button interaction between Unity and Arduino. I spent the first part of the week rewatching Unity tutorials, and my room this week is already looking better.

The scripting part of the class was much easier for me to get my mind around. For my one button interaction, I thought I would create an object that, once the Arduino button is pushed, generates a wisp-like light of random color that floats or spirals upwards.

First attempt yielded a game object that was always floating upward, but generating lights. So I need to make sure the object generating the lights stays fixed and only the lights float upward. Also, the scene gets flooded with objects, so will need to pace the instantiation of lamps to one per button press.

I then was able to add gravity effects and randomize the color of each generated wisp. The wisps are now dropping to the ground as they generate.

Next steps in this project could be to replace the button with a potentiometer so one could speed up or slow down the generation of wisps. In Unity we could add some controls so a player could walk around the space and look upward as the wisps rise.

I’d also like to explore interplay between the physical and digital interfaces more. I feel like this is 25% physical and 75% digital, so maybe I could add a few more buttons or pots to balance out the interaction.

Here is the script I am using at the moment:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonScript : MonoBehaviour
{

	public Light wisp;

	public Transform LightTarget;

	private Transform currentTarget;


	bool buttonState = false;
	bool lastButtonState = false;
	bool flipMe = false;
	float msg = 0;

	Vector3 movePosition = new Vector3(0, 10000f, 0);
 	public float speed = 2f;

	// Start is called before the first frame update
	void Start()
	{

	}

	// Update is called once per frame
	void Update()
	{
		if(flipMe) {
			// use a function to instantiate a new game object
			createObject();

		}

		// Vector3 newPos = Vector3.MoveTowards(lamp.transform.position, movePosition, speed * Time.deltaTime);
		// lamp.transform.position = newPos;

		
	}

	void createObject()
	{

		Color newLightColor = new Color(
			Random.Range(0f, 1f), 
			Random.Range(0f, 1f), 
			Random.Range(0f, 1f)
		);

		Quaternion newRotation;
		newRotation = Quaternion.Euler(0,45,0);
		Light t = Instantiate(wisp, new Vector3(0,2,0), Quaternion.identity );
		t.color = newLightColor;

		currentTarget = LightTarget;

		
		t.transform.position = Vector3.MoveTowards(transform.position, currentTarget.position, speed * Time.deltaTime);
		
		
		
	}

    void OnMessageArrived(string message)
    {
    	msg = float.Parse(message);

    	/* 
    	We only want to instantiate a new object if
    	the button is pressed, not when its held down.
		*/

    	lastButtonState = buttonState;

    	if(msg == 0) {
            buttonState = true;
        } else {
            buttonState = false;
        }

        if(buttonState == true && lastButtonState == false) {
        	flipMe = true;
        }
        else {
        	flipMe = false;
        }

    	Debug.Log(flipMe);
        
    }

    
    void OnConnectionEvent(bool connected)
    {
        if(connected)
        {
        	Debug.Log("Connection Established");
        }
        else
        {
        	Debug.Log("Nope!");
        }
    }
}