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 Shempz · Sep 29, 2014 at 01:37 PM · c#timecombocooldown

Cooldowns with Time.time

Hey guys, usually I'm fairly decent with debugging but this seems fairly hard to google. I am basically trying to check if time < time + cooldown time however using a float of 0.3 (which seems like .3 of a second? or something low) it always seems to return true even after waiting for a long time?

Should I be using something besides float? I tried to make an actual Time class variable but obviously that didn't work out for me. Code below.

using UnityEngine; using System.Collections;

public class Stats : MonoBehaviour {

 public int HP = 100;
 public int score = 0;
 public int currentCombo = 0;
 public int bestCombo = 0;
 public float lastKill = 0f;
 public float comboTime = .3f;

 void Combo (){

     if (Time.time < Time.time + comboTime) {
                     currentCombo ++;
     } 
     else {//if (Time.time > Time.time + comboTime) {
         if (bestCombo < currentCombo) {
             bestCombo = currentCombo;
         }
         currentCombo = 0;
     }

 }

}

FYI Combo is called by sendMessage on each enemy death which seems to be working fine and the else if is commented out because I was trying to get ANYTHING else to return. Thanks!!!

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
1
Best Answer

Answer by HarshadK · Sep 29, 2014 at 01:45 PM

Your if(Time.time < Time.time + comboTime) will always be true since you are checking if time is less than time + comboTime.

This is something like:

 if(1 < 1 + 0.3) 

which is always going to be true.

If you are trying to check if time is less than cooldown time then it should be:

 if(Time.time < startTime + comboTime)

Here, startTime is the time when your cooldown started. So when your condition occurs you assign your Time.time to your startTime.

so,

 startTime = Time.time;

now check if the time passed since your condition occurred is less than your startTime+comboTime, which means your cooldown time is still not ended.

 if(Time.time < startTime + comboTime)
Comment
Add comment · Show 4 · 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 scnoobi · Sep 29, 2014 at 01:49 PM 2
Share

dam you. just 1 $$anonymous$$ute ahead of me and we did the same things:D

avatar image Shempz · Sep 29, 2014 at 02:09 PM 0
Share

Ah right, thanks heaps. So obvious! The brain seems to just become blind to your own program$$anonymous$$g sometimes.

Now I have a way broader question.. I changed it to:

void Combo (){

     if (Time.time < last$$anonymous$$ill + comboTime) {
         currentCombo ++;
         last$$anonymous$$ill = Time.time;
     } 
     else {//if (Time.time > Time.time + comboTime) {
         if (bestCombo < currentCombo) {
             bestCombo = currentCombo;
         }
         currentCombo = 0;
         last$$anonymous$$ill = Time.time;
     }

But this seems to also not work. Are script lines performed in order or at the same time? Where is the best place to set the last$$anonymous$$ill time? (Right after the if also didn't work).

avatar image HarshadK · Sep 29, 2014 at 02:17 PM 1
Share

You don't set your last$$anonymous$$ill inside your if loop. You need to set it before that when the actual kill occurs. Also this last$$anonymous$$ill needs to be set only one when the kill occurs and not every frame since setting it every frame will still keep it in an infinite loop.

Let me give you an example, In script below when the objects collide I set last$$anonymous$$ill and then keep checking if the last$$anonymous$$ill is less inside my Update().

 void OnCollisionEnter()
 {
     last$$anonymous$$ill = Time.time;
 }
 
 void Update()
 {
     if (Time.time < last$$anonymous$$ill + comboTime) {
         currentCombo ++;
     } 
     else {//if (Time.time > Time.time + comboTime) {
         if (bestCombo < currentCombo) {
             bestCombo = currentCombo;
         }
         currentCombo = 0;
     }
 }
avatar image scnoobi · Sep 29, 2014 at 02:43 PM 0
Share

why would you check it in update? would it not be better if it is called when a kill/attack happens

NV$$anonymous$$ saw that he wanted it to bestCombo to change if Time.time is higher than comboTime.

avatar image
1

Answer by scnoobi · Sep 29, 2014 at 01:48 PM

your if statement will always happen because you are checking Time.time is less than Time.time + 0.3 seconds

if time.time is 1 then its if(1 < 1 + 0.3)

which you can see will always happen. what i'm thinking you need to do is take the time from when the last hit happened + combotime

 if(Time.time < lastAttackTime + comboTime)
 {
     currentCombo++;
     lastAttackTime = Time.time;
 }


got no clue if that actually works but might help you :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with displaying gained EXP on screen. 1 Answer

How to add a cooldown sort of thing. 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Returning an IEnumerator as an int? 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