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 yusolaifdfer · Jul 16, 2014 at 08:23 AM · scoreintcontrolvaluehealth

Checking for a value overtime

Hey guys, I am working on a simple health system but it turns out to be a little more complex as I started writing it. I am looking for a way to decrease my player's health over time UNLESS it makes score. Now I have the score script done which simply increases the value of score by 1.

Now to make it work I will have to check if in the past 5 seconds player has done any score, if yes then ignore if not then decrease health by 10 then again repeat until it reaches 0 and game ends.

Any idea on how to implement this? Can use

 int delayTime = 5;
 if(delatTime > 0)
 {
   delayTime -= 1*Time.time;
 }

 

I honestly don't know weather to put it in update or make a new function and make the function called in update or how else to get this done. Basically I can't figure out how to compare the two scores, one before 5 seconds and one currently. If they're same - BAM. If not everything is happy.

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

2 Replies

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

Answer by smallbit · Jul 16, 2014 at 08:55 AM

Here is a simple solution, When player scores call Scored() method which starts the timer. The flag scoredLastFiveSeconds will remain true for the next 5 seconds, and than will be switched to false again. You can use this bool than to check whether to apply dmg or not in the method you have.

Regards!

  bool scoredInLastFiveSeconds; //flag that check if player scored
     float scoreTimer;   //timer 
 
     void Scored()//when player scores call this method to init counter 
     {
         scoredInLastFiveSeconds = true; //flag to true
         scoreTimer = 0f; //reset timer
     }
     void Update()
     {
         //timer 
         if (scoredInLastFiveSeconds) //count time only if player scored
         {
             if (scoreTimer < 5f) //check if time below 5 secs 
             {
                 scoreTimer += Time.deltaTime; //update timer
             }
             else
             {
                 scoredInLastFiveSeconds = false; //after 10 secs set flag to false
             }
         }
         
         //use flag to apply dmg
         if (!scoredInLastFiveSeconds)  //here you check whether to apply dmg or not 
         {
             ApplyDamage();  //apply damaga.....
         }
         
     }
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 yusolaifdfer · Jul 16, 2014 at 01:37 PM 1
Share

Wow there are still people here going full "I'll write the script for you" haha. Thanks, this -although not out of box- after a bit tweaking here and there worked.

avatar image Hiten2012 · Jan 01, 2015 at 09:12 AM 0
Share

Thanks a lot, It helped.

avatar image
2

Answer by MrAkroMenToS · Jul 16, 2014 at 08:37 AM

Try with Invoke: http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html

 InvokeRepeating("Decrease", 5, 5);
 
     void Decrease() {
         if(youDidntDoIt)
         player.Health -= 10;
     }

This method: call invokerepeating - 5 sec - check is you did what you have to do - if not player health -=10 - if yes nothing happens - this repeats until you stop the invoke (http://docs.unity3d.com/ScriptReference/MonoBehaviour.CancelInvoke.html). I hope it helped.

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 yusolaifdfer · Jul 16, 2014 at 01:36 PM 0
Share

Thanks for this man, much appreciated. It had been long forgotten in my head. Thumbs up.

avatar image MrAkroMenToS · Jul 16, 2014 at 02:23 PM 0
Share

$$anonymous$$ay I ask you which one did you use? :)

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

24 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 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

How to make camera position relative to a specific target. 1 Answer

Increasing an int everytime another int reaches an amount. 1 Answer

Calling Enemy Health Adding to Player Score 0 Answers

Zombie won't die 2 Answers

Multiply float by int? 2 Answers


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