• 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 Var1g · Jun 02 at 03:10 PM · script.getkeydowngetkeyup

Can someone help me use GetKeyDown functions with this script. I've tried so many combinations

This script works fine using UI "Buttons" However, I am looking to use GetKeyDown & GetKeyUp functions to start and stop the timer. It seems logical to do but I just can't find a solution using Input.GetKeyDown functions.

 float val;
 bool timerActive;
 public Text timerText;

 // Start is called before the first frame update
 void Start()
 {
     val = 0;
     timerActive = false;
 }

 void Update()

 {
     if (timerActive)

     {
         val += Time.deltaTime;
     }

     double b = System.Math.Round(val, 2);

     timerText.text = b.ToString("F2");
 }

 public void startbutton()
 {
     timerActive = true;
 }

 public void stopbutton()
 {
     timerActive = false;
 }




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 stachojakub · Jun 02 at 06:07 PM

Hi @Var1g ! It would be enough just to add this snippet of code at the beginning of the Update function.

  if (Input.GetKeyDown(KeyCode.Q))
      timerActive = true;
 
 if (Input.GetKeyDown(KeyCode.E))
     timerActive = false;

And you can put your own keys there instead my Q and E

Hope it helps!

Comment
Add comment · Show 5 · 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 Var1g · 3 days ago 0
Share

Thank you. It's exactly what I am looking for. How would I store this number and use it as a time to beat. Like a fastest lap per se ?

avatar image Quatnu Var1g · 3 days ago 0
Share

Put this before the Start() function:

 float bestTime;
 bool firstLap = true;

And this into the Update() function:

 if (firstLap == true)
 {
     bestTime = val;
     firstLap = false;
 }
 
 else if (val < bestTime)
 {
     bestTime = val;
 }
avatar image stachojakub · 3 days ago 0
Share

Could you clarify more what are you trying to do? What number do you mean?

avatar image Var1g stachojakub · 3 days ago 0
Share

Yeh sorry. Questions always seem much clearer for the person asking them. Thanks for getting back to me.

My game is time based. Where the shortest time to complete the level is the time to beat. I want to take that level time and display it as the Best Time. If that makes sense? If you want the snippet of code. Let me know. thanks in advance.

avatar image stachojakub Var1g · 3 days ago 0
Share

I would add new function with the newTime parameter. It will be sufficient to call that function when you set the new time of the lap. Here is whole code:

 float val;
 float bestTime;
         bool timerActive;
         public Text timerText;
         // Start is called before the first frame update
         void Start()
         {
             val = 0;
             timerActive = false;
             bestTime = float.MaxValue;
         }
         void Update()
         {
             if (Input.GetKeyDown(KeyCode.Q))
                 timerActive = true;
 
             if (Input.GetKeyDown(KeyCode.E))
                 timerActive = false;
 
             if (timerActive)
             {
                 val += Time.deltaTime;
             }
 
             double b = System.Math.Round(val, 2);
             timerText.text = b.ToString("F2");
         }
 
         public void SetNewTime(float newTime)
         {
             if (newTime < bestTime)
                 bestTime = newTime;
         }
avatar image
0

Answer by lCedric · Jun 02 at 04:53 PM

Have you tried using th

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

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

Update GetKey If Statements both playing instantly 1 Answer

Can/how do I set up a boolean to continue an animation after button press 1 Answer

OnGUI: += and -= operations problem 2 Answers

AudioSource does not work for some reason 1 Answer

Play half of Animation on KeyDown/Other Half on KeyUp 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