Jump character with click on space key in Javascript

Hi. I created a simple game in Unity with JavaScript and I want when Space key is pressed, character jump, I did a lot of Search about jump function, but I was not successful to find its code! I have completely confused. What code to write? Thanks.

1 Answer

1

By default the space bar is tied to the “Jump” key. There are 3 kinds of “Jump” events.

  1. When space bar first goes down from being pressed: Input.GetButtonDown("Jump")
  2. When space bar gets released: Input.GetButtonUp("Jump")
  3. Every frame the space bar is held down: Input.GetButton("Jump")

Here’s a video with details of how to use Input.GetButton: GetButton and GetKey - Unity Learn

Here’s a basic implementation.

function Update ()
{        
  if (Input.GetButtonDown("Jump"))
  {
    // code that moves your character in a jump like manner
  }
}