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 /
This question was closed Aug 30, 2014 at 06:20 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Steviebops · Aug 01, 2013 at 03:05 PM · inputstore

Does Unity have a way of storing and comparing the last key pressed with the current one.

Im using a dash system based on wasd input

   if (Input.GetKeyDown(KeyCode.W))
 
         {
 
             if ((Time.time - lastTapTime) < tapSpeed)
 
             {
 
                 this.Trigger_FDodge();
 
                 Debug.Log("ForDodge");
 
             }
 
             lastTapTime = Time.time;
 
         }
 
  
 
         if (Input.GetKeyDown(KeyCode.D))
 
         {
 
             if ((Time.time - lastTapTime) < tapSpeed)
 
             {
 
                 this.Trigger_RDodge();
 
                 Debug.Log("RightDodge");
 
             }
 
             lastTapTime = Time.time;
 
         }


Any one of these on their own is fine, but, because I need to be able to dodge in four directions, I hit a problem. While strictly still a double tap, pressing 'W' then 'D' will trigger a dodge, when it should only be triggered with 'WW' or 'DD'.

When I used XNA, there were options for input states, so I could store and compare the last key pressed with the current one.

  KeyboardState lastKeyboardState = KeyboardState;

Does Unity have a way of doing this?

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

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by gregzo · Aug 01, 2013 at 03:10 PM

No, but caching key presses should'nt be too hard...

declare

  private KeyCode lastHitKey;

Then cache every keyboard input :

  if (Input.GetKeyDown(KeyCode.W))
  {
     lastHitKey = KeyCode.W;
     //your foo here
  }

Or, more elegant as suggested in the comments, but afaik neeeds to be in OnGUI and not Update :

 if ( Input.anyKeyDown )
 {
     lastHitKey == Event.current.keyCode;
 }

You can then check if a key is the same as lastHitKey.

Comment
Add comment · Show 3 · 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 Loius · Aug 01, 2013 at 03:43 PM 1
Share

Event.current.keyCode will get you the current key without having to check every single option :)

avatar image gregzo · Aug 01, 2013 at 03:53 PM 0
Share

Thanks, edited my answer.

avatar image Steviebops · Aug 01, 2013 at 04:03 PM 0
Share

It seems to working here if (lastHit$$anonymous$$ey == $$anonymous$$eyCode.W && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W)) { if ((Time.time - lastTapTime) < tapSpeed) { this.Trigger_FDodge(); Debug.Log("ForDodge"); } lastTapTime = Time.time; } if (lastHit$$anonymous$$ey == $$anonymous$$eyCode.D && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D)) { if ((Time.time - lastTapTime) < tapSpeed) { this.Trigger_RDodge(); Debug.Log("RightDodge"); } lastTapTime = Time.time; } if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W)) { lastHit$$anonymous$$ey = $$anonymous$$eyCode.W; //your foo here } if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D)) { lastHit$$anonymous$$ey = $$anonymous$$eyCode.D; //your foo here } Thank you very much :) Any tips on tidying it up?

Follow this Question

Answers Answers and Comments

16 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

Related Questions

A node in a childnode? 1 Answer

Simultaneous button presses 2 Answers

How do I invert the Y axis in Penelope tutorial? 3 Answers

Triggering Main Camera Script with GUITexture 1 Answer

Drag+an+object+to+touch+position(Problem) 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