Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by _Frost · Oct 02, 2016 at 07:01 PM · timetimerassignment

How to check if no buttons are pressed for a specific time?

 float timeVariable = 0f;
 
 void Update () {
 if ( canCheck == true){
 timeVariable = Time.time + 5;
 Do{
 //nothing until time has passed
 }while ( Time.time < timeVariable && Input.AnyKey == false)
 If (Time.time >= timeVariable){
 //do stuff
 }

Am I wrong or what? Without the Do/While, I can tell that timeVariable is always the same as Time.time, So how do I do this?

Comment
Add comment · Show 5
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 Sergio7888 · Oct 02, 2016 at 07:13 PM 0
Share

Use if(!Input.any$$anonymous$$ey)

avatar image _Frost Sergio7888 · Oct 02, 2016 at 09:06 PM 0
Share

I've tried that. When I use the Do/While loop the Unity Editor freezes on play.

avatar image Sergio7888 _Frost · Oct 02, 2016 at 09:33 PM 0
Share

put in a coroutine, don't use while in Update, Update is called once per frame. Time don't change inside the same frame, for this reason you enter in a infinite loop and unity freezes.

Show more comments

2 Replies

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

Answer by JathOsh · Oct 03, 2016 at 06:00 AM

This code should hopefully fit your needs

                  public int time = 0;    
         
                 //Use fixed update beacuase its called every fixed framerate frame
                  void FixedUpdate () {
     
                     if(!Input.anyKey){
                         
                         //Starts counting when no button is being pressed
                         time = time + 1;
                     } else {
                         
                         // If a button is being pressed restart counter to Zero
                         time = 0;
                     }
                     
              //Now after 100 frames of nothing being pressed it will do activate this if statement
                     if(time == 100) {
                         Debug.Log("100 frames passed with no input");
                         
                         //Now you could set time too zero so this happens every 100 frames
                         time = 0;
                     }
     
                 }
 

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 Zetario · May 09 at 05:53 AM 0
Share

you can use this for actual time in seconds

 public float timer = 0;
 
     void FixedUpdate () {
 
         if(!Input.anyKey){
             timer += Time.deltaTime;
         } else {
             timer = 0;
         }
         
         if(timer >= (TIME IN SECONDS)) {
             //your code
             timer = 0;
         }
 
     }

avatar image
0

Answer by schizmo · Oct 03, 2016 at 07:46 AM

You would need to start a coroutine to increment a timer and evaluate some bools when the timer has begun and elapsed, this may not solve your problem directly but I believe the framework should help you reach a solution of your own. I also tried to comment it as best as I could for clarity, hopefully this should allow you to write your own similar code that fits within the script you're using.

 using UnityEngine;
 using System.Collections;
 
 public class ButtonTimer : MonoBehaviour {
 
     public float inputTimer; //the timer begins at 0
     public float frequency; //the frequency the timer is checked, if you need to check the timer more frequently then try 0.5f or 0.25f or even 0.1f, but 1f should be sufficient.
     public float timeLimit; //the limit of the timer 
 
     public bool timing;
     public bool noInput;
 
     void Start () { //none of these things need to be set at start, they can be set elsewhere if need be
     
         inputTimer = 0f;
         frequency = 1f;
         timeLimit = 5f;
 
     }
 
     void Update () {
 
         if (!noInput && !timing) { //first, the script makes sure the timer hasn't already begun or elapsed
                 if (!Input.anyKey) { //then, if there is no input
                     timing = true; //the timing begins
                     StartCoroutine ("InputTimer"); //and the coroutine is called
                 }
             }
 
          if (Input.anyKey) { //if input is ever detected
             timing = false; //the timing ends
             noInput = false; //the "noInput" status is reset (if it had been activated in the first place)
             inputTimer = 0f; //the timer is also reset to 0, this is useful if the timer needs to be accessed by another portion of the script but this can be removed because the timer also sets itself to 0 at the start of the coroutine
             StopCoroutine ("InputTimer"); //and the coroutine is ended
         }
     }
 
     IEnumerator InputTimer () {
 
         inputTimer = 0f; //timer resets at the start of the coroutine
 
         while (timing) { //only workins while timing
             while (inputTimer < timeLimit) { //while the timer is below the timeLimit
                 inputTimer += frequency; //the timer counts upward at the same rate it is checked
                 yield return new WaitForSeconds (frequency); // 1 second is sufficient but it can check every half or quarter or whatever you need
             }
 
             if (inputTimer >= timeLimit) { // once the timer has exceeded the timeLimit
                 noInput = true; //it tells the script that there has been no input
                 timing = false; //stops timing (because there's no need once it has determined there's no input)
                 Debug.Log ("No input!"); //I just left this here while testing
                 yield return noInput; 
             }
         }    
     }
 }

EDIT: I forgot to mention it initially, but this only works for key and button inputs. Mouse movement is not counted.

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

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

Unity how to find load time? 1 Answer

creating a timed function 3 Answers

Simple Timer 2 Answers

Timer that stops at trigger 1 Answer

Timer Countdown fix. 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