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?

Use subroutines.

if (Input.GetButtonDown("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 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:

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;