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?
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.
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?
It would be less of a headache for you overall to refactor your code. Shouldn't take that long.
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.
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;
}
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.
@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
Follow this Question
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