Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by excalith · Dec 15, 2015 at 09:50 PM · javascripttexture2dyield waitforsecondsswitch-case

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;
     }
 }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image excalith · Dec 16, 2015 at 09:25 PM 0
Share

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;
         }
     }
     

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image excalith · Dec 17, 2015 at 02:00 PM 0
Share

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

avatar image
0

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;
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Yield Waitforseconds not working at all 3 Answers

yield WaitForSeconds preventing further actions. 1 Answer

Switch Statements 1 Answer

New GameObject with GUITexture is a Horribly Incorrect Size 1 Answer

Swap texture2D on enemy sprite once reached a certain point 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges