Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 youngapprentice · Feb 08, 2013 at 06:21 PM · functioncallshakeonce

Calling a function only once in Update

I know I should probably be able to figure how to do this by myself, but for some reason I can't wrap my brain around it.

I have a function that is in Update() that checks for dropping health percentage. Every time health drops a significant percentage, I want the camera to shake.

My problem here is that it continually gets called, and therefore doesn't stop shaking after the first time.

How should I fix this?

The function:

 function CheckHealthPercentage( healthPercent : float ){
     
     if(healthPercent == 1){
         damageLevel = "No";
         damageColor = Color(0,1,0);
     }
     
     else if(healthPercent < 1 && healthPercent >= 0.8 ){
         damageLevel = "Minimal";
         damageColor = Color(1,1,0);
     cameraController.ShakeCamera( 0.5, 1 );
     }
     
     else if(healthPercent < 0.8 && healthPercent  >= 0.5 ){
         damageLevel = "Moderate";
         damageColor = Color(1,0.6,0);
     cameraController.ShakeCamera( 1.0, 1 );
     }
     
     else if(healthPercent < 0.5 && healthPercent >= 0.2 ){
         damageLevel = "Heavy";
         damageColor = Color(1,0.2,0);
         cameraController.ShakeCamera( 1.5, 1 );
     }
     
     else if(healthPercent < 0.2 ){
         damageLevel = "Intense";
         damageColor = Color(0.6,0.1,0.1);
         cameraController.ShakeCamera( 2, 1 );
     }
 }

The problem here is that it needs to be called only once, but also EVERY TIME a damage level shifts downward. I can't reset it because it is called on update and will just reset itself every frame :/

Comment
Add comment
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

3 Replies

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

Answer by Dave-Carlile · Feb 08, 2013 at 06:52 PM

The concept here is that you want to execute some code only when the state changes. In this case when the damage level changes. Your current code is always executing the code when you're in the state, not when you change to the state.

In order to check state changes you need to keep the previous state, and the current state. Then, when you want to check for a state change you compare the current state to the previous state. If it's the same then you know you didn't change state. If it's different then you did change state, so you can execute your "state change" code.

Here's what I would do with your code (with most of it left out, just showing this as an example)...

 function CheckHealthPercentage(healthPercent : float)
 {
   // save current damage level
   var previousDamageLevel : string = damageLevel;

   if (healthPercent == 1)
   {
      damageLevel = "No";
   }
   else if (healthPercent < 1 && healthPercent >= 0.8)
   {
     damageLevel = "Minimal";
   }

   // did the damage level change?
   if (damageLevel != previousDamageLevel)
   {
     // TODO : shake it
   }
 }


My Javascript sucks, so no guarantees of syntax correctness :)

Comment
Add comment · Show 3 · 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 youngapprentice · Feb 08, 2013 at 06:55 PM 0
Share

Gotcha. This looks good to me. I'll try it out and post results.

avatar image Dave-Carlile · Feb 08, 2013 at 06:55 PM 0
Share

You can modify this to only execute the state change code when "the damage level shifts downwards" by changing damageLevel to an int, and doing the compare as damageLevel > previousDamageLevel.

avatar image youngapprentice · Feb 08, 2013 at 07:01 PM 0
Share

Ah thank you. I hadn't thought of that! Works like a charm by the way. Thank you.

avatar image
2

Answer by Kergal · Feb 08, 2013 at 06:24 PM

easy - create a bool only call the function when bool is true. at the end of the function - set the bool to false.

example :

 void Update() {
    
      if(randombool){
    
            SomeRandomFunction();
 
       }
   
 } 
 
 void SomeRandomFunction() {
 
       // insert code here
 
       randombool = false;
 
 }
Comment
Add comment · Show 2 · 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 youngapprentice · Feb 08, 2013 at 06:45 PM 0
Share

This would work except for the fact that it needs to repeat

avatar image Kergal · Feb 08, 2013 at 06:48 PM 0
Share

well.. still easy :)

either call the function again. or create a loop how many times does it have to repeat that shaky thing? if each function call is one "iteration" do something like

 if(somebool) {
 
   while(actualShakes < numberofShakes){
 
 
       Call the function ();
       actualShakes ++;
       }
   if(actualShakes == numberofShakes){
  
    somebool = false;
 
     }
 }
avatar image
0

Answer by GC1983 · Feb 08, 2013 at 06:34 PM

Hmm, you could place a timer in there. Call the boolean when collision is made, and then let the timer run out. Make the timer variable public so you can set for how long you want it to shake. For Example:

 float shakeTime;
 bool  cameraShake;
 
 else if(healthPercent < 1 && healthPercent >= 0.8 ){
        damageLevel = "Minimal";
        damageColor = Color(1,1,0);
 
     if(cameraShake)
     {
        shakeTime -= Time.deltaTime
 
        cameraController.ShakeCamera( 0.5, 1 );
 
        if(shakeTime <= 0)
           cameraShake = false;
     }
Comment
Add comment · Show 2 · 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 youngapprentice · Feb 08, 2013 at 06:47 PM 0
Share

This, too, would work, but it needs to reset itself when damage levels change.

avatar image GC1983 · Feb 08, 2013 at 07:31 PM 0
Share

Heh ok, so then add the reset code in the if statement turning the shake off.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling A Function Once 0 Answers

Efficient way for calling a function once 3 Answers

Return multiple parameter 3 Answers

Call a function multiple times 2 Answers

StartCoroutine important for using yield? 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