• 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 vmvenkat · Dec 29, 2014 at 11:52 AM · c#timecounter

How to create an Idle counter ?

hello, I am new for unity. I'm trying to implement a counter that counts down whenever the player input stops, and whenever input begins again the counter is to reset. I try many code but not working. Any tips would be much appreciated. 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 Mmmpies · Dec 29, 2014 at 12:03 PM

Set a float e.g.

 private float counterStart = 20f; // whatever value you want

and anther

 private float theCounter;

In start set

 theCounter = counterStart;

check for user input and if the user is inputting set

 theCounter = counterStart;

if there is no input set

 theCounter = theCounter - Time.deltaTime;

And check for it going below zero.

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 vmvenkat · Dec 30, 2014 at 04:12 AM 0
Share

Thank for reply. how to find "no input set" ? That's what i want to know.

avatar image Mmmpies · Dec 30, 2014 at 08:46 AM 0
Share

just use an if statement to capture which input button is being pressed

 if(Input.GetButtonDown("Button1")
 {
     // do input stuff and set 
     theCounter = counterStart;
 }
 else if (Input.GetButtonDown("Button2")
 {
     // do input stuff and set
     theCounter = counterStart;
 }
 else if (Input.GetButtonDown("Button3")
 {
     // do input stuff and set
     theCounter = counterStart;
 }
 else
 {
    theCounter = theCounter - Time.deltaTime;
 }
avatar image vmvenkat · Dec 30, 2014 at 09:16 AM 0
Share

Thank you.

avatar image Mmmpies · Dec 30, 2014 at 09:18 AM 0
Share

No problem - glad it helped.

avatar image
1

Answer by danvalho · Dec 30, 2014 at 08:11 PM

If you want only to know if any key/button was pressed, you can use Input.anyKeyDown and reset or increment the counter:

 private float idleCounter = 0.0f;
 
 void Update() {
   if (Input.anyKeyDown) {
     idleCounter = 0.0f;  // reset counter          
   } else {
     idleCounter += Time.deltaTime; // increment counter
   }
 }

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 vmvenkat · Dec 31, 2014 at 06:32 AM 0
Share

It's work fine. Thank you.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 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

Related Questions

How to execute a Skript global? 2 Answers

Need help calling a script to another keep getting errors 1 Answer

Unity units to pixel? 1 Answer

Float not working all the time? 1 Answer

Script Execution Order Question 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges