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
1By default the space bar is tied to the “Jump” key. There are 3 kinds of “Jump” events.
- When space bar first goes down from being pressed:
Input.GetButtonDown("Jump") - When space bar gets released:
Input.GetButtonUp("Jump") - 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
}
}