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 AmateurPrograming · Dec 28, 2020 at 08:19 PM · unity 2dinput.getkey

How do you use the same key for two different conditions.

Sorry if this seems like a dumb question to ask. I am fairly new to unity.

Anyways, I would like my player to pick up an object. I would like to use the same key (E) to pick the object up and also place it down if the player is holding it. I haven't really been able to get it to work.

Code:

alt text

screenshot-41.png (249.0 kB)
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

Answer by sacredgeometry · Dec 28, 2020 at 08:38 PM

Something like?


 var canHold = item.tag == "Grabable";
 
 if((canHold || isHolding) && Input.GetKeyDown(KeyCode.E))
 {
     if(isHolding)
     {
          PutdownItem();
     }
     else 
     {
          PickupItem();
     }
 }


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 AmateurPrograming · Dec 28, 2020 at 08:42 PM 0
Share

I didn't think about that! It would also make the code cleaner.

avatar image sacredgeometry AmateurPrograming · Dec 28, 2020 at 08:46 PM 0
Share

I have updated it with the case for is holding because I don't know how item is populated and if its transient or not.

avatar image
0

Answer by AmateurPrograming · Dec 28, 2020 at 09:13 PM

@sacredgeometry

It kind of works, yet the thing I am trying to pick up freaks out for some reason. Sometimes if I press E it doesn't pick up or put down fully. It will just appear to be placed or picked up for a split second then go back to its normal position.

I wasn't having an issue like this when I used separate keys to pick up and place the object.

I also changed a bit of your code to fit mine.

New Code (Including some more):

alt text

alt text


screenshot-42.png (184.3 kB)
screenshot-43.png (207.1 kB)
Comment
Add comment · Show 8 · 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 sacredgeometry · Dec 28, 2020 at 09:17 PM 0
Share

Use Input.GetKeyDown not Input.GetKey.

One repeats for as long as you have it held down the other just does it once.

avatar image AmateurPrograming · Dec 28, 2020 at 09:19 PM 0
Share

I changed "GetKey" to "GetKeyDown". It takes more keypresses to drop the object.

avatar image sacredgeometry AmateurPrograming · Dec 28, 2020 at 09:21 PM 0
Share

It sounds like you have a bunch of bools and are trying to juggle state between them needlessly and various combinations of that state are resulting in working and others arent.


I would try to simplify it or failing that make sure you are managing the state properly.


Its hard to tell without seeing all of the relevant code

avatar image AmateurPrograming sacredgeometry · Dec 28, 2020 at 09:29 PM 0
Share

Fixed it, I changed FixedUpdate to Update.

Show more comments
avatar image AmateurPrograming AmateurPrograming · Dec 28, 2020 at 09:21 PM 0
Share

Never$$anonymous$$d, it still can take multiple keypresses until you pick up or drop the object.

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

116 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

Related Questions

Scaling unity apps 1 Answer

Unity 2D: Instantiated objects are not visible 1 Answer

Sprite gets draged way to far because of a certain canvas renderer mode. 0 Answers

How to move sprites in the opposite direction to player. 0 Answers

velocity of bullet in rifile rotation 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