Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

2D Touch Jump Button in Unity 5

Discussion in '2D' started by justforskool, Nov 9, 2019.

  1. justforskool

    justforskool

    Joined:
    Nov 1, 2019
    Posts:
    4
    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
    Code (CSharp):
    1. Rigidbody2D.addForce
    but the editor said that the method was deprecated. Any help?



     

    Attached Files:

  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    638
    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:

    Code (CSharp):
    1. private Rigidbody2D playerRb;
    2. // The player's object with Rigidbody2D component on it
    3.  
    4. private float jumpHeight = 5f;
    5. // You can set this however you wish.
    6. //Make it public to customize within the inspector
    7.  
    8. private void Start()
    9. {
    10. playerRb = GetComponent<Rigidbody2D>();
    11. // This grabs the component RB as long as your script
    12. // is attached to the player.
    13. }
    14.  
    15. private void Update()
    16. {
    17. PlayerJump();
    18. }
    19.  
    20. void PlayerJump()
    21. {
    22. if(Input.GetKeyDown(KeyCode.Space)
    23. {
    24. // Here I demonstrate jumping based on Space bar
    25. // being pushed down.
    26. // You will want this to be within your button fire though.
    27.  
    28. playerRb.AddForce(playerRb.up * jumpHieght, ForceMode2D.Impulse);
    29. }
    30. }
    This code is untested but I'm like 65% sure it'll work.
     
    vakabaka likes this.
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,147
    add the script on the button, add OnClick event (+ on your second screenshot) and select Jump in the buttons event
    Code (CSharp):
    1. //you can reference the player from the editor, or search with tag
    2. public GameObject player;
    3. public float jumpHeight;
    4. Rigidbody2D playerRb;
    5.  
    6. void Start ()
    7. {
    8. //it is better to find the player in the scene with the tag player = GameObject.FindWithTag ("Player");
    9. playerRb = player.GetComponent<Rigidbody2D>();
    10. }
    11.  
    12. public void Jump ()
    13. {
    14. playerRb.AddForce(playerRb.up * jumpHieght, ForceMode2D.Impulse);
    15. //or you can call the jump function on the player
    16. //something like
    17. //player.GetComponent<PlayerMovingScript>().Jump();
    18. }
    19.  
     
    MisterSkitz likes this.
  4. justforskool

    justforskool

    Joined:
    Nov 1, 2019
    Posts:
    4


    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.
     

    Attached Files:

  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,147
    try
    playerRb.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);

    sorry, I have written without to check them
     
    Last edited: Nov 10, 2019
  6. justforskool

    justforskool

    Joined:
    Nov 1, 2019
    Posts:
    4
    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
     
  7. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,147
    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
    Code (CSharp):
    1. Debug.Log ("Jump");
     
    Last edited: Nov 10, 2019
  8. justforskool

    justforskool

    Joined:
    Nov 1, 2019
    Posts:
    4


    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class doJumpButton : MonoBehaviour
    6. {
    7.     //you can reference the player from the editor, or search with tag
    8.     public GameObject player;
    9.     public float jumpHeight;
    10.     Rigidbody2D playerRb;
    11.  
    12.     void Start()
    13.     {
    14.         //it is better to find the player in the scene with the tag player = GameObject.FindWithTag ("Player");
    15.         playerRb = player.GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     public void Jump()
    19.     {
    20.         playerRb.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
    21.         Debug.Log("Jump");
    22.         //or you can call the jump function on the player
    23.         //something like
    24.         //player.GetComponent<PlayerMovingScript>().Jump();
    25.     }
    26. }
    27.  
     

    Attached Files:

  9. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    638
    My bad lol I totally screwed that one up for ya.

    Code (CSharp):
    1. playerRb.AddForce(Vector2.up * jumpHeight, ForceMode.Impulse);
    2.  
    3. // Which is the same thing as
    4. 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!
     
  10. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    638
    I fixed the mistake in my code and added a few comments to your code @vakabaka
    Code (CSharp):
    1. public GameObject player;
    2. public float jumpHeight;
    3. Rigidbody2D playerRb;
    4.  
    5. public void Jump ()
    6. {
    7. playerRb.AddForce(Vector2.up * jumpHieght, ForceMode2D.Impulse);
    8. }
    9.  
    10. //or you can call the jump function on the player
    11. //something like
    12. //player.GetComponent<PlayerMovingScript>().Jump();
    13.  
    14. // just to Clarify the above code, you will need to create
    15. // a "Jump" script and attach it to to your button.
    16. // So on your Jump script you'd need only one function
    17.  
    18. public void CallForJumpCode()
    19. {
    20. player.GetComponent<PlayerMovingScript>().Jump();
    21. }
    22. // You will then need to go to you buttons OnClick()
    23. // Add an event by Clicking the +
    24. //Drag your button with the Jump Script attached into the OnClick Field
    25. // Then you will select the CallForJumpCode()
     
    vakabaka likes this.
  11. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,147
    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.
     
    MisterSkitz likes this.