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

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

Problem with player movements 1 Answer

How to make specific text in a string array Bold C# 2 Answers

Movement Script Help 1 Answer

C# Plane Detecting a Gameobject 1 Answer

C# Script interaction between separate locations 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