• 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 hassonhamo3 · Jul 30, 2018 at 03:11 AM · errorkeypresskeycode

access keycodes by pressing a button

I made Ui for controlling my Player, all is fine , but i have methods that work when i press F on my keyboard , how can I make my button equals to keycode.F ? like when I press the button it types F ? please help :( , i don't want it in If statement "If(input.getkeydown(keycode.F)" i don't want this , i tried to make input.getkeydown(keycode.F) and that doesn't work i couldn't access the F keyCode.

Comment
Add comment · Show 4
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 hexagonius · Jul 30, 2018 at 04:33 AM 0
Share

make the button just call the same method your F key calls when pressed

avatar image hassonhamo3 hexagonius · Jul 30, 2018 at 06:37 AM 0
Share

thanks for replaying , it's not a method that could be called , it's OnTrigger2D < when ever my player collides with somehing and press F he can move the thing he;s colliding with around

avatar image RocketFriday · Jul 30, 2018 at 06:14 AM 0
Share

On Click OnClick

avatar image hassonhamo3 RocketFriday · Jul 30, 2018 at 06:38 AM 0
Share

thanks for replaying , may I have further information ?

4 Replies

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

Answer by RocketFriday · Jul 30, 2018 at 09:23 AM

Change the condition in the if statement to:

 bool allowObjMovement = false;

 void Update()
 {
     if(input.getkey(keycode.F))
     allowObjMovement = true; 
 }
 

 Void OnTrigger2D(Colllider2D other)
 { 
        if player.collider...etc && allowObjMovement == true;
        { 
              do something. 
        } 
 }

Now for the UI Button try this or set allowObjMovement to true however you want.

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 hassonhamo3 · Jul 30, 2018 at 05:14 PM 0
Share

thanks alot , this is much better than the way i was trying to use , the problem is solved :D

avatar image RocketFriday hassonhamo3 · Jul 30, 2018 at 06:11 PM 0
Share

Awesome! glad to hear it! Goodluck with your game!

avatar image
0

Answer by hassonhamo3 · Jul 30, 2018 at 06:34 AM

@hexagonius it's not a method , when ever my player collides with something and press F i can move the thing the player colides with around .something like :

Void OnTrigger2D(Colllider2D other){ if player.collider...etc && input.getkeydown(keycode.F){ do something. } }

Comment
Add comment · Show 1 · 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 hassonhamo3 · Jul 30, 2018 at 07:12 AM 0
Share

file:///C:/Users/S%20N%20I%20P%20E%20R/Documents/oCam/%D8%A7%D9%84%D8%AA%D9%82%D8%A7%D8%B7_2018_07_30_09_46_25_373.png

here's the method the button attached to , and it doesn't work when i press the button the F on my keyboard isn't pressed

avatar image
0

Answer by lucadome · Jul 30, 2018 at 09:05 AM

For this you have to create a public method, that you call when the press the F and when u press a UI button.

To call the method when clicking the button, simply go to the inspector and add the method from the On Click() list.

alt text


cattura.png (59.9 kB)
Comment
Add comment · Show 1 · 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 hassonhamo3 · Jul 30, 2018 at 05:15 PM 0
Share

thanks for replying

avatar image
1

Answer by Tanoshimi2000 · Jul 30, 2018 at 02:18 PM

Personally, I think you'd be better off using the Input system. If you define an Input called "Push" and set that input to KeyPress F, you can also assign it to a gamepad button, or Mouse Button 3. The input system works better than specifically defining a key, and it allows the user to remap it in case they don't want to use "F". Not that the other answers are incorrect, or even less correct. :D

Just my two cents.

Comment
Add comment · Show 1 · 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 hassonhamo3 · Jul 30, 2018 at 05:13 PM 0
Share

thanks for replying :D

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

121 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

Related Questions

How to get Key with KeyCode.Question? 1 Answer

Problem with pressing multiple keys at once? 2 Answers

Numpad Keypress not being registered on mobile phone. 0 Answers

My scripts are not working properly 0 Answers

Can someone explain this?? KeyCode Error 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