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 ramonfr · Dec 14, 2011 at 02:57 AM · deltatime

How to count seconds while mouse is pressed?

I have a very simple problem, but I trough it should be easier.

What I need is:

1 - While some mouse button is pressed, a counter starts running on a loop, it goes from 1 to 5. When the counter reaches 5, it stars again from 1.

What I've done already:

1 - I can detect when mouse button is pressed and released whitout any problem. So that's not a problem.

2 - I tried to make a loop using "for" and later "while" but Unity3D crashes. So I tried time.deltaTime, but I don't know how to count the time while mouse is pressed.

Comment
Add comment · Show 2
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 jahroy · Dec 14, 2011 at 03:09 AM 0
Share

If you share your code, we can tell you what is causing the infinite loop.

avatar image Lo0NuhtiK · Dec 14, 2011 at 03:23 AM 0
Share

var timeIsUp : float = 5.0 ;
var timeNow : float = 1.0 ;
function timer(){
if(timeNow >= timeIsUp){
timeNow = 1 ; }
if(timeNow < timeIsUp){
timeNow += Time.deltaTime ; }
Debug.Log(timeNow) ;
}
I put this in a comment because my answers take an hour or more to get posted since I'm a No0b in this website, and this is a short one so whatever. Just put this in whatever function you're wanting to call it in (change the function name, obviously, to whatever you're using).Not sure if you want it to display the count on the screen or what, but this will do what you asked for. Just tweak it for whatever else....

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by jahroy · Dec 14, 2011 at 03:07 AM

Here is how you would count how long it's been since a button was clicked.

var buttonPressTime : float = 0.0;

var buttonRect : Rect = Rect(200, 200, 120, 40); var labelRect : Rect = Rect(200, 240, 120, 40);

function OnGUI () { if ( GUI.Button(buttonRect, "Start Timer") ) { buttonPressTime = Time.time; }

 var timeSince : float = Time.time - buttonPressTime;

 GUI.Label(labelRect, "Time since: " + timeSince);

}

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 alex-dacol · Aug 01, 2015 at 01:28 PM 0
Share

Thanks man! That really helped , I use C# and wanted to adapt with a key press ins$$anonymous$$d of mouse button . I was having trouble with the update Function , but with some tweeking I finally did it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

What exactly is in Touch.deltaTime? 1 Answer

Certain timers run too fast 1 Answer

Using Time.DeltaTime doesn't result in framerate independence 1 Answer

Would this code not work because there is no Time.DeltaTime? 1 Answer

How can I keep "notes" in sync with music? 0 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