GMU:Tutorials/Networking/Controlling Unity with Processing: Difference between revisions

From Medien Wiki
m (→‎Unity: formatting)
Line 116: Line 116:
c) Add Audio variables to your script. This script will also draw a circle depending on the audio volume. <br>
c) Add Audio variables to your script. This script will also draw a circle depending on the audio volume. <br>
<source lang="javascript">
<source lang="javascript">
float x;<br>
float x;
float y;<br>
float y;
Minim minim;<br>
 
Minim minim;
AudioInput input;
AudioInput input;
</source> <br>
</source>
d) In the Setup() function add the values for the circle and create the audiotoolkit <br>
d) In the Setup() function add the values for the circle and create the audiotoolkit <br>
<source lang="javascript">
<source lang="javascript">
size (320, 240);<br>
size (320, 240);
smooth();<br>
smooth();
stroke (255, 25);<br>
stroke (255, 25);
noFill ();<br>
noFill ();
// Set start position<br>
 
x = 0;<br>
// Set start position
y = 20;<br>
x = 0;
minim = new Minim (this);<br>
y = 20;
input = minim.getLineIn (Minim.STEREO, 512);<br>
 
minim = new Minim (this);
input = minim.getLineIn (Minim.STEREO, 512);
background (0);
background (0);
</source> <br>
</source>
e) The draw() function will be the container of the circle’s size. It will only send the values to Unity if the circle is big enough. <br>
e) The draw() function will be the container of the circle’s size. It will only send the values to Unity if the circle is big enough. <br>
<source lang="javascript">
<source lang="javascript">
void draw () {<br>
void draw () {
int i =0;<br>
int i =0;
float dim = input.mix.level () * width;<br>  
float dim = input.mix.level () * width;<br>  
if (dim >= 80){System.out.println("Loud enough: "+dim);<br>
 
OscMessage myMessage = new OscMessage("/");<br>
if (dim >= 80){System.out.println("Loud enough: "+dim);
myMessage.add(dim);<br>
  OscMessage myMessage = new OscMessage("/");
myMessage.add(true);<br>
    myMessage.add(dim);
oscP5.send(myMessage, myRemoteLocation);}
    myMessage.add(true);
</source> <br>
  oscP5.send(myMessage, myRemoteLocation);}
</source>
f) When you are ready, press Play. <br> <br>
f) When you are ready, press Play. <br> <br>
'''2. In Unity:''' <br>
'''2. In Unity:''' <br>
Line 150: Line 154:
b) Open up the Osc Receiver Script.  Define a new private variable. <br><br><source lang="javascript">
b) Open up the Osc Receiver Script.  Define a new private variable. <br><br><source lang="javascript">
private var moveSpeed : int = 0;
private var moveSpeed : int = 0;
</source> <br>  
</source>  
c)  Create a new public function.<br>
c)  Create a new public function.<br>
<source lang="javascript">
<source lang="javascript">
Line 157: Line 161:
moveSpeed = msgValue;
moveSpeed = msgValue;
}
}
</source> <br>
</source>
d)  In the Update() function add <br>
d)  In the Update() function add <br>
<source lang="javascript">
<source lang="javascript">
var go = GameObject.Find(gameReceiver);<br>
var go = GameObject.Find(gameReceiver);
go.transform.Translate(0, moveSpeed, 0);  
  go.transform.Translate(0, moveSpeed, 0);  
</source> <br>
</source>
e) In the AllMessageHandler() function add <br>
e) In the AllMessageHandler() function add <br>
<source lang="javascript">
<source lang="javascript">
Line 169: Line 173:
   MoveObject(msgValue);
   MoveObject(msgValue);
   break;}
   break;}
</source> <br>
</source>
f) When you are ready, press Play.
f) When you are ready, press Play.