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 Darkkingdom · May 04, 2015 at 05:15 AM · c#inputkeycode

Last key pressed

Hey guys^^

I wanted rebuild my 4 way, 2D shooting script, so that it shoots only in the direction where the last key is pressed.

This is what I have so far:

        if (Input.GetKey ("down")) {
             shotDirection = 1;
         }if (Input.GetKey ("up")) {
             shotDirection = 2;
         }if (Input.GetKey ("left")) {
             shotDirection = 3;
         }if (Input.GetKey ("right")) {
             shotDirection = 4;
         }


But with this way, if more keys pressed it takes the last key from the order. I expermented with Event.current.key only to see that Event works only in OnGUI xD

Sadly I have no idea how to solve this :(

Does anyone have some suggestions for dealing with this problem?

Thanks a lot guys :)

Comment
Add comment · Show 3
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 jodev · May 05, 2015 at 05:28 PM 0
Share

I'm not quite sure what you mean by "if more keys pressed it takes the last key from the order". Could you rephrase or eleborate on that?

avatar image Darkkingdom · May 05, 2015 at 10:07 PM 0
Share

Oh I'm very sorry my english isn't the best :(

What I mean is: The player press the right key to shoot right and than press the left key. Atm the player would still shoot right and ignors the left key because "if (Input.Get$$anonymous$$ey ("right")) { shotDirection = 4; }" is after "if (Input.Get$$anonymous$$ey ("left")) { shotDirection = 3; }" But I want that the player shoots left because it's the last pressed key.

avatar image jodev · May 05, 2015 at 10:17 PM 0
Share

Now I understand what you meant. I think the answer by EpicPants is the one you'll need to look at. Get$$anonymous$$eyDown() might work better, because users are less likely to press two keys down on the exact same frame. You'll have to keep in $$anonymous$$d though that Get$$anonymous$$eyDown() only returns true in that one frame the key was pressed. So, you'll have to make sure the shotDirection variable keeps it state. You can do that by making it a class variable for example.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by EpicPants · May 05, 2015 at 08:41 PM

GetKey will return true every frame the key is down. If you use GetKeyDown then it will only return true the frame you first press the key. Try using that instead.

It will still take the last key from the order if all statements are true in the same frame, but it will only happen if you press them all at once. Unfortunately I think you need to prioritize directions in this case.

Comment
Add comment · Show 2 · 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 Darkkingdom · May 07, 2015 at 06:19 PM 0
Share

Omg certainly is Get$$anonymous$$eyDown the solution. It was so easy but I still didn't realized it x.x

Anyways thank's alot :)

avatar image EpicPants · May 07, 2015 at 08:19 PM 0
Share

No problem! :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

KeyCode parse problem 0 Answers

String as a input 2 Answers

Key binding screen 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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