- Home /
Scripting Heart BPM Using Different Values With Switch & Yield
Hello there,
I am working on an experimental project and got stuck right now. Basicly, whenever my kinect-controlled character gets into different triggers, they change a value in different script called BPM. The script below should get the value name as a case, and switch to that. Each case changes localScale and mainTexture with different yield values acting like heart bpm (will work on yield values).
The problem I am having right now is, whenever I call this changeBPM from function Start, it won't get updated after I change the value of BPM. When I put it into function Update, somehow they keep getting bigger without any yield applied.
What can cause this and how can I fix it? I am really out of ideas, therefore I am open to any help. I am more comfortable coding in Javascript, but I am open to c# as well.
Thanks so much for your help!
#pragma strict
var texture1 : Texture2D;
var texture2 : Texture2D;
var change : boolean = true;
var bpm : int = 1;
function Start() {
changeBPM();
}
function Update() {
}
function changeBPM() {
switch (bpm) {
case 3:
print("BPM: 3");
while(change) {
transform.localScale += new Vector3(0.5, 0.5, 0.5);
GetComponent.<Renderer>().material.mainTexture = texture1;
yield WaitForSeconds(0.2);
transform.localScale += new Vector3(-0.5, -0.5, -0.5);
GetComponent.<Renderer>().material.mainTexture = texture2;
yield WaitForSeconds(0.4);
}
break;
case 2:
print("BPM: 2");
while(change) {
transform.localScale += new Vector3(0.5, 0.5, 0.5);
GetComponent.<Renderer>().material.mainTexture = texture1;
yield WaitForSeconds(0.2);
transform.localScale += new Vector3(-0.5, -0.5, -0.5);
GetComponent.<Renderer>().material.mainTexture = texture2;
yield WaitForSeconds(0.3);
}
break;
case 1:
print("BPM: 1");
while(change) {
transform.localScale += new Vector3(0.5, 0.5, 0.5);
GetComponent.<Renderer>().material.mainTexture = texture1;
yield WaitForSeconds(0.2);
transform.localScale += new Vector3(-0.5, -0.5, -0.5);
GetComponent.<Renderer>().material.mainTexture = texture2;
yield WaitForSeconds(0.2);
}
break;
default:
print("Default");
break;
}
}
I worked more on this and here is another problem: Whenever state changes, it doesn't remove the other states that has peen applied before, like each state localScale keeps applied. But why? I removed the unnecessary variables for easy-reading.
#pragma strict
var change : boolean = true;
var state : int = 0;
var firstState : int = 0;
function Update() {
if (firstState != state) {
print("state changed");
firstState = state;
changeState();
}
}
function changeState() {
switch (state) {
case 3:
print("Case: 3");
while(change) {
transform.localScale += new Vector3(0.5, 0.5, 0.5);
yield WaitForSeconds(0.2);
transform.localScale += new Vector3(-0.5, -0.5, -0.5);
yield WaitForSeconds(0.4);
}
break;
case 2:
print("Case: 2");
while(change) {
transform.localScale += new Vector3(0.5, 0.5, 0.5);
yield WaitForSeconds(0.2);
transform.localScale += new Vector3(-0.5, -0.5, -0.5);
yield WaitForSeconds(0.3);
}
break;
case 1:
print("Case: 1");
while(change) {
transform.localScale += new Vector3(0.5, 0.5, 0.5);
yield WaitForSeconds(0.2);
transform.localScale += new Vector3(-0.5, -0.5, -0.5);
yield WaitForSeconds(0.2);
}
break;
default:
print("Default");
break;
}
}
Answer by ZefanS · Dec 17, 2015 at 02:24 AM
The reason that it won't change even when bpm is changed (when only called in Start()) is that you check for the bpm case before you check whether change is true. In other words, the first time you call changeState() you get locked into whichever bpm was set at the time you called it. The only way to move to another case is to change bpm, set change to false, let changeState() return, set change back to true, and then call changeState() again. Based on your description of what it sounds like you want to do, I would try modifying the script like this:
#pragma strict
var texture1 : Texture2D;
var texture2 : Texture2D;
var change : boolean = true;
var bpm : int = 1;
functionStart()
{
ChangeBPM();
}
function Update()
{}
function ChangeBPM()
{
while (change)
{
switch (bpm)
{
case 3:
print("BPM: 3");
transform.localScale += new Vector3(0.5, 0.5, 0.5);
GetComponent.<Renderer>().material.mainTexture = texture1;
yield WaitForSeconds(0.2);
transform.localScale += new Vector3(-0.5, -0.5, -0.5);
GetComponent.<Renderer>().material.mainTexture = texture2;
yield WaitForSeconds(0.4);
break;
case 2:
print("BPM: 2");
transform.localScale += new Vector3(0.5, 0.5, 0.5);
GetComponent.<Renderer>().material.mainTexture = texture1;
yield WaitForSeconds(0.2);
transform.localScale += new Vector3(-0.5, -0.5, -0.5);
GetComponent.<Renderer>().material.mainTexture = texture2;
yield WaitForSeconds(0.3);
break;
case 1:
print("BPM: 1");
transform.localScale += new Vector3(0.5, 0.5, 0.5);
GetComponent.<Renderer>().material.mainTexture = texture1;
yield WaitForSeconds(0.2);
transform.localScale += new Vector3(-0.5, -0.5, -0.5);
GetComponent.<Renderer>().material.mainTexture = texture2;
yield WaitForSeconds(0.2);
break;
default:
print("Default");
break;
}
}
}
This will allow you to change bpm while continually "beating" the heart as long as change is true.
As for your second problem, it looks to me like you might need to reset the local scale when you change states. You could try adding
transform.localScale = Vector3(1.0f, 1.0f, 1.0f);
at the beginning of each case statement.
Hope this helps.
Hello ZefanS, Yesterday night I found a workaround for this and posted it but it is still waiting for moderation somehow. Yet this is surely a better solution considering what I did. Thank you so much, it seems it is working now
Answer by excalith · Dec 17, 2015 at 04:53 PM
Okay, I solved this by chance while playing out. I am adding the whole script, might be useful to someone. Only thing to consider is the active state bools for each state, I am sure there is a better way to do. Hope someone contributes a better idea :)
#pragma strict
var heartbeat : AudioClip;
var state : int = 0;
var firstState : int = 0;
var state0Active : boolean = true;
var state1Active : boolean = false;
var state2Active : boolean = false;
var state3Active : boolean = false;
var state4Active : boolean = false;
var state5Active : boolean = false;
var state6Active : boolean = false;
var state7Active : boolean = false;
var state8Active : boolean = false;
var state9Active : boolean = false;
function Update() {
if (firstState != state) {
print("state changed");
firstState = state;
changeState();
}
}
function changeState() {
switch (state) {
case 0:
print("Case: 0");
resetState();
state0Active = true;
transform.localScale += new Vector3(0, 0, 0);
break;
case 1:
state1Active = true;
while(state1Active) {
print("Case: 1");
resetState();
state1Active = true;
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(0.1, 0.1, 0.1);
yield WaitForSeconds(0.2);
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(-0.1, -0.1, -0.1);
yield WaitForSeconds(0.1);
}
break;
case 2:
state2Active = true;
while(state2Active) {
print("Case: 2");
resetState();
state2Active = true;
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(0.1, 0.1, 0.1);
yield WaitForSeconds(0.2);
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(-0.1, -0.1, -0.1);
yield WaitForSeconds(0.2);
}
break;
case 3:
state3Active = true;
while(state3Active) {
print("Case: 3");
resetState();
state3Active = true;
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(0.1, 0.1, 0.1);
yield WaitForSeconds(0.2);
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(-0.1, -0.1, -0.1);
yield WaitForSeconds(0.3);
}
break;
case 4:
state4Active = true;
while(state4Active) {
print("Case: 4");
resetState();
state4Active = true;
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(0.1, 0.1, 0.1);
yield WaitForSeconds(0.2);
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(-0.1, -0.1, -0.1);
yield WaitForSeconds(0.4);
}
break;
case 5:
state5Active = true;
while(state5Active) {
print("Case: 5");
resetState();
state5Active = true;
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(0.1, 0.1, 0.1);
yield WaitForSeconds(0.2);
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(-0.1, -0.1, -0.1);
yield WaitForSeconds(0.5);
}
break;
case 6:
state6Active = true;
while(state6Active) {
print("Case: 6");
resetState();
state6Active = true;
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(0.1, 0.1, 0.1);
yield WaitForSeconds(0.2);
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(-0.1, -0.1, -0.1);
yield WaitForSeconds(0.6);
}
break;
case 7:
state7Active = true;
while(state7Active) {
print("Case: 7");
resetState();
state7Active = true;
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(0.1, 0.1, 0.1);
yield WaitForSeconds(0.2);
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(-0.1, -0.1, -0.1);
yield WaitForSeconds(0.7);
}
break;
case 8:
state8Active = true;
while(state8Active) {
print("Case: 8");
resetState();
state8Active = true;
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(0.1, 0.1, 0.1);
yield WaitForSeconds(0.2);
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(-0.1, -0.1, -0.1);
yield WaitForSeconds(0.8);
}
break;
case 9:
state9Active = true;
while(state9Active) {
print("Case: 9");
resetState();
state9Active = true;
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(0.1, 0.1, 0.1);
yield WaitForSeconds(0.2);
GetComponent.<AudioSource>().PlayOneShot(heartbeat);
transform.localScale += new Vector3(-0.1, -0.1, -0.1);
yield WaitForSeconds(1);
}
break;
default:
print("Case: Default");
resetState();
state0Active = true;
transform.localScale += new Vector3(0, 0, 0);
break;
}
}
function resetState() {
state0Active = false;
state1Active = false;
state2Active = false;
state3Active = false;
state4Active = false;
state5Active = false;
state6Active = false;
state7Active = false;
state8Active = false;
state9Active = false;
}