Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by CogentJin · Sep 04, 2018 at 01:38 PM · unity 5guibuttononclick

Is it possible to use a GUI Button (On Click) to force Input keyboard commands like spacebar for Jump?

I already have my character controller coded, but now need on screen GUI Buttons to handle some of his moves. The problem is if I make a script saying Input.GetButtonDown("Jump"), and use that for On Click, the character doesn't actually Jump because it's just checking to see if the button is being pressed or not. I can play the animation of the character jumping using On Click - Animator, but that's still not the same as just pressing the space bar. I am using Jump as an example when he has so many animations, that's why I was hoping there was a way someone can tell me how to use a GUI Button to imitate a keyboard press?

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
0

Answer by Vicarian · Sep 04, 2018 at 01:42 PM

Use subroutines.

 if (Input.GetButtonDown("Jump")
     Jump();
 
 public void Jump() {
     // Jump code here
 }

Then hook the player controller script to your button's OnClick event handler in the editor and choose the Jump method.

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 CogentJin · Sep 04, 2018 at 08:09 PM 0
Share

I was going to do that but honestly there's so many commands and actions he can do spaced apart if he has one weapon, two, which hands, etc. I typed his character controller way before I realized I might need to separate the code so his abilities are very scattered in a list. But you are saying there's no way to get around that and it's impossible to imitate a physical keyboard press?

avatar image Vicarian CogentJin · Sep 04, 2018 at 08:11 PM 0
Share

It would be less of a headache for you overall to refactor your code. Shouldn't take that long.

avatar image CogentJin Vicarian · Sep 04, 2018 at 08:33 PM 0
Share

Yes, you're probably right. This is a valuable lesson overall. If you'll excuse me I'm about to go Charlie Brown UUUGH in the corner.

avatar image
0

Answer by eses · Sep 04, 2018 at 01:52 PM

@CogentJin

I think you should go through basic tutorials on UI and start reading more about Input class and how the current / new UI system works in general. You are reading your input now just using Input class. You should separate your input and actions / logic. Movement commands could come from touch, keyboard or on screen buttons.

Input class is for getting button presses of pre-named input axis values, and predefined keyboard button presses and touch events.

Event system, which is related to UI, on the other hand when used together with UI, will be used differently than Input class. You naturally are not reading some keyboard key press. You instead use components like Button, or implement comparable interfaces (like what button use) to get the clicks to event system. First read about UI and check out how UI Button's onClick works: https://docs.unity3d.com/ScriptReference/UI.Button-onClick.html

You can assign you method to handle button click from code or from inspector of button for example. Just make your method do the work of collecting clicks etc, instead of Input class. Then relay this information to your movement code.
Edit (reaction to comment):

You could do something like this - warning - this is very crude and not what you want, but should explain the idea:

 // Call every frame
 void Movement ()
 {
     // Get input from keys or touch buttons
     if (isTouchControls == false) KeyBoardInput();
 
     // Move player by input
     playerTra.position += playerVelocity;
 }
 
 void KeyBoardInput()
 {
     playerVelocity.x = Input.GetAxis("Horizontal");
 }
 
 
 // Assigned To Button OnClick
 public void InputFromButtonOnClick (int direction) 
 {
     // direction in left button on click = -1
     // direction in right button on click = 1
     playerVelocity.x = direction;
 }
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 CogentJin · Sep 04, 2018 at 08:15 PM 0
Share

Trust me, I've read that document about ten times. There has been honest effort by me to do this, it's just maybe I'm not getting it. Not that it matters so much, but just look at this like it's a separate problem by itself rather than my knowledge on the basics. I messed up at the start of this because I wasn't planning on going this route and now I'm paying the price and trying to shortcut--which again, my fault. But basically what I'm reading is it's impossible without re-doing all the code in his character controller and making a separate script that includes the button within that code? I just wanted to know if a GUI Button can imitate a physical keyboard press. Sorry if I typed that out poorly, I was just grasping for one last straw from people much smarter than I with this and thought maybe someone could help with a work around--Haha.

avatar image eses CogentJin · Sep 04, 2018 at 09:40 PM 0
Share

@CogentJin - See the answer, I added crude code sample. I'm pretty much in the same boat as you - learning. I kinda get what you meant but don't have any better answer. Hopefully this gives you idea what I meant.

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

318 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 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 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

Images as buttons with GUI skins 1 Answer

is it possible to have two on click events in one button 1 Answer

Instantiated button prefab causes Delegation error on click and does not add OnClick Listener 0 Answers

Button object has no attribute OnClick 1 Answer

UI (Canvas) element disappears when loading additive scene 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