2D Touch Jump Button in Unity 5

I am trying to make a simple touch-to-jump button so that when I press a UI button, the Player Rigidbody will jump upwards. I have done extensive research and I could not find ANYTHING that could help me. I tried using Rigidbody2D.addForce but the editor said that the method was deprecated. Any help?


5158706--511154--Capture.PNG

There’s a lot of depreciated code from way back on Unity 5. Crazy because I remember when it came out and I was super excited! lol

Anyway, try:

private Rigidbody2D playerRb; 
// The player's object with Rigidbody2D component on it

private float jumpHeight = 5f;
// You can set this however you wish.
//Make it public to customize within the inspector

private void Start()
{
playerRb = GetComponent<Rigidbody2D>();
// This grabs the component RB as long as your script
// is attached to the player.
}

private void Update()
{
PlayerJump();
}

void PlayerJump()
{
if(Input.GetKeyDown(KeyCode.Space)
{
// Here I demonstrate jumping based on Space bar
// being pushed down.
// You will want this to be within your button fire though.

playerRb.AddForce(playerRb.up * jumpHieght, ForceMode2D.Impulse);
}
}

This code is untested but I’m like 65% sure it’ll work.

1 Like

add the script on the button, add OnClick event (+ on your second screenshot) and select Jump in the buttons event

//you can reference the player from the editor, or search with tag
public GameObject player;
public float jumpHeight;
Rigidbody2D playerRb;

void Start ()
{
//it is better to find the player in the scene with the tag player = GameObject.FindWithTag ("Player");
playerRb = player.GetComponent<Rigidbody2D>();
}

public void Jump ()
{
playerRb.AddForce(playerRb.up * jumpHieght, ForceMode2D.Impulse);
//or you can call the jump function on the player
//something like
//player.GetComponent<PlayerMovingScript>().Jump();
}
1 Like

I got this error that wasn’t fixed by typo correction (the other 2 errors were typos) and this isn’t my strong suit and I’m clueless. I’ve attached the script to the character while creating, but still errors.

try
playerRb.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);

sorry, I have written without to check them

Vaka, I’ve attached the script to the player and selected “Jump()” as the button function in the Inspector (attaching the PLayer asset and selecting the function) but on button press nothing happens

do you have added the needed button in the event too ? And changed jumpHeight from 0 to something bigger (like 10 or 100 for testing) ?

try to add in the Jump function and check if this will be printed in the console by jumping

Debug.Log ("Jump");

The debug is firing…

The player is tagged (attached to player)…

And the button function is selected as the function

What am I doing wrong?

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class doJumpButton : MonoBehaviour
{
    //you can reference the player from the editor, or search with tag
    public GameObject player;
    public float jumpHeight;
    Rigidbody2D playerRb;

    void Start()
    {
        //it is better to find the player in the scene with the tag player = GameObject.FindWithTag ("Player");
        playerRb = player.GetComponent<Rigidbody2D>();
    }

    public void Jump()
    {
        playerRb.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
        Debug.Log("Jump");
        //or you can call the jump function on the player
        //something like
        //player.GetComponent<PlayerMovingScript>().Jump();
    }
}

5160305--511496--jump.png
5160305--511502--player.png
5160305--511508--button.PNG

My bad lol I totally screwed that one up for ya.

playerRb.AddForce(Vector2.up * jumpHeight, ForceMode.Impulse);

// Which is the same thing as
playerRb.AddForce(new Vector2(0,1) * jumpHeight, ForceMode.Impulse);

I accidentally put your RB inside the Vector field. That was my fault, sorry about that!

I fixed the mistake in my code and added a few comments to your code @vakabaka

public GameObject player;
public float jumpHeight;
Rigidbody2D playerRb;

public void Jump ()
{
playerRb.AddForce(Vector2.up * jumpHieght, ForceMode2D.Impulse);
}

//or you can call the jump function on the player
//something like
//player.GetComponent<PlayerMovingScript>().Jump();

// just to Clarify the above code, you will need to create
// a "Jump" script and attach it to to your button.
// So on your Jump script you'd need only one function

public void CallForJumpCode()
{
player.GetComponent<PlayerMovingScript>().Jump();
}
// You will then need to go to you buttons OnClick()
// Add an event by Clicking the +
//Drag your button with the Jump Script attached into the OnClick Field
// Then you will select the CallForJumpCode()
1 Like

ok, if the debug.log is working, then we should check the rigidbody and the moving script. At first try to add more jumpHeight. Try 1000. Sometimes the power isn’t enough to push the player up.

then look at you moving script, it can be possible, that you are overwriting the push force with the other values by moving (something like rb.velocity = Vector2.left).

and check, if the rigidbody isn’t kinematic.

Also I would say, post here screenshots from the rigidbody component and moving script.

1 Like

So, what do other people do, WHAT DO I DO???

Just Asking how do you stop double jumping? thx for any help

you need something to make the player jump if its only on the ground