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 _eternal · Jan 27, 2015 at 03:53 PM · inputcoroutinetimebuffertime.time

How to check if input was made x seconds/frames ago?

I'm using the Rewired input plugin, and it comes with an input buffer. I enabled the buffer for my directional keys and airdash button so that the player doesn't have to hit both keys on the same frame in order to dash.

However, this means that if you press both keys and hold them down for more than a frame, the airdash code gets run again. I tried to stop this by comparing Time.time at the beginning of Update() to Time.time at the beginning of the coroutine, but this seems inconsistent and it doesn't feel like the right way to do it.

How would you check for the player's input time and compare it to the time since some previous input?

Here's a sample of the dash code within FixedUpdate()

         //Dash
 
         if (grounded)
         {
             dashCount = 0;
             dashTime = 0;
         }
 
         if (dashCount < 2)
         {
             //print(currentTime - dashTime);
             if (currentTime - dashTime < 0.1 || dashCount == 0)
             {
                 //Double Tap Dash
                 if (player.GetButtonDoublePressDown("Left"))
                 {
                     StartCoroutine(Dash("left"));
                 }
 
                 //Button Dash
                 if (player.GetButtonDown("Dash") && player.GetButtonDown("Left"))
                 {
                     StartCoroutine(Dash("left"));
                 }
             }
             
         }
 
         print(dashCount);

and here's a sample of the coroutine:

     //Dash
     IEnumerator Dash(string axis) {
 
         dashCount++;
 
         dashTime = Time.time;
 
         if (axis == "left")
         {
             if (inDash == true)
             {
                 anim.SetBool("dashUp", false);
                 anim.SetBool("dashDown", false);
                 anim.SetBool("Hover", false);
             }
             canControl = false;
             inDash = true;
             anim.SetBool("dashHorizontal", true);
             anim.SetBool("inDash", true);
             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
             rigidbody2D.AddForce(-dashForceHorizontal, ForceMode2D.Impulse);
             rigidbody2D.velocity = Vector2.ClampMagnitude(rigidbody2D.velocity, maxVelocity);
             rigidbody2D.gravityScale = 0;
 
             yield return new WaitForSeconds(0.2f);
             
 
             rigidbody2D.gravityScale = 2;
             rigidbody2D.drag = 25f;
             yield return new WaitForSeconds(0.2f);
             
 
             if (grounded == true)
             {
                 yield return new WaitForSeconds(0.1f);
             }
 
             rigidbody2D.drag = 0f;
             canControl = true;
             anim.SetBool("dashHorizontal", false);
             anim.SetBool("inDash", false);
 
             inDash = false;
 
             yield break;
         }
   }


Comment
Add comment · Show 1
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 Glurth · Jan 27, 2015 at 06:19 PM 0
Share

perhaps a side route to the solution: It sounds like that input buffer is not "consu$$anonymous$$g" the keys when you check them. $$anonymous$$eep in $$anonymous$$d I know nothing about that library, but often this is because you need to call a different function to both get AND "consume" the last keypresses.

another possible solution, that does not answer the question about time.

hold them down for more than a frame, the airdash code gets run again

Sounds to me like you should set a flag when airdash is called, and only clear it when the airdash operation is done. Then check this flag at the very begining of you airdash function: if it's set, just return. I guess this flag COULD be a time value, rather than a bool. Rather than checking if true, you could compare it to the current time.

The reason I didn't answer the question about time is, I think this will require you to RECORD the time of each keypress, essentially making your own parallel input buffer, but with times rather than keystrokes. This would be a bit of a project.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Hypothetical: What happens if Time.time overflows? 4 Answers

Need help developing Damage Over Time mechanic 0 Answers

Animation Curve Data Recording 0 Answers

Starting and stopping Coroutines 1 Answer

How to prevent holding down a key to infinitely loop an action? 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