Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Jump on UIButton

Discussion in '2D' started by rasmusnielsen, May 5, 2020.

  1. rasmusnielsen

    rasmusnielsen

    Joined:
    Feb 7, 2020
    Posts:
    11
    Hi!

    I'm making a small platformer but have run in to a problem.

    For now I have the character set up so he jumps on
    Code (CSharp):
    1. if (Input.GetKeyDown("space"))
    Then it runs
    Code (CSharp):
    1. public void Jump(){
    2.     rb.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 10), ForceMode2D.Impulse);
    3.     ps.enableEmission = true;
    4. }
    I have a small snippet limiting the player from jumping too high but a little bit higher when holding down the Space button.

    Code (CSharp):
    1.     if (rb.velocity.y < 0){
    2.         rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
    3.     } else if (rb.velocity.y > 0 && !Input.GetButton("Jump") && !JumpButtonDown){
    4.         rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
    5.     }
    I want to make a onscreen button for jumping. So what I've set up a UI Button and made a bool for checking if it's pressed
    Code (CSharp):
    1.     public bool JumpButtonDown;
    and a eventlistener on the UI Button Up + Down. Checking if the button is pressed
    Code (CSharp):
    1.  
    2. public void OnPointerDown(PointerEventData eventData) {
    3. _down = true;
    4. }
    5.  
    6. public void OnPointerUp(PointerEventData eventData) {
    7. _down = false;
    8. }
    9. }
    It kinda of sorta works, but the player is not being limited by the button.

    With Keydown()


    With UIButton()
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,468
    It doesnt look like you are checking whether the player is grounded or not. Add in a isGrounded bool or something that makes it so you can only jump when player is on the ground or an allowable object.
     
  3. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    Your button handling is strange. In fact, you do not need to read OnPointerUp.

    You are working with _down variable as it was bool tracking whether user is currently touching on-screen button. It is true all the time between pointer down and up and if you do not reset it to false in jump processing code, then you are performing many jumps in row (probably every frame while _down is true).

    What you need is to work with it like with trigger - just set it once in OnPointerDown. Check in game loop, whether _down is true. If yes, make jump and reset _down to false. Ignore OnPointerUp.

    This is base. Other issues may arise from what @Cornysam writes - like checking if grounded to prevent in-air jumps, etc.
     
    rasmusnielsen likes this.
  4. rasmusnielsen

    rasmusnielsen

    Joined:
    Feb 7, 2020
    Posts:
    11
    I am checking on PointerUp is because I want the player to jump a little bit higher when the button is being held. Similar to a “Mario-jump”

    If I reset after a jump is being made I will kinda loose that mechanic right?


     
  5. rasmusnielsen

    rasmusnielsen

    Joined:
    Feb 7, 2020
    Posts:
    11
    Just a quick update. I tried to reset the jumping after clicking on the button. It worked in regards to the suuuper high jumping but not in regards to the "hold-jump-down" mechanic. Here's a video of the difference.

    I'm thinking there must be some difference in the way Unity is checking a Input.GetButton and a UI Button.

     
  6. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    I have variable jump in game (and also double jump) and it can be controlled either by keys or by buttons on screen. Here is how it works:
    1] I read jump button down and up in separate varaibles (I call ReadInput() from Update()):

    Code (CSharp):
    1.     // -----------------------------------------------------------
    2.     private void ReadInput() {
    3.  
    4.         // jump
    5.         if (PlayerInput.GetButtonDown(PlayerInput.Action.Jump)) {
    6.             _jumpKeyDown = true;
    7.         }
    8.  
    9.         if (PlayerInput.GetButtonUp(PlayerInput.Action.Jump)) {
    10.             _jumpKeyUp = true;
    11.         }
    12.  
    13.              :
    14.              :
    15.  
    16.     }

    2] In FixedUpdate() I call Jump(). I have these remarks:
    - I start jump if jump down was triggered and player is grounded and minimum required time since last jump passed or is double jump possible,
    - If jump key/button is released, I decrease velocity by half (if player is going up),
    - finally, If jump key is still down and player is going up, I clear it too. But I do not clear it if he is already falling. This is because if user presses button 1-2 pixels above ground, player is not grounded and will not jump. It looks like game is not responsive. So, I allow user to pres jump button even while still falling down and when player lands and is grounded and jump key is still down, player will immediately jump again.

    Code (CSharp):
    1.     // -----------------------------------------------------------
    2.     private Vector2 Jump(Vector2 velocity) {
    3.         if (_isDead) {
    4.             return velocity;
    5.         }
    6.  
    7.         if (_jumpKeyDown && ((_playerController.Grounded && Time.time >= _timeForNextJump) || _doubleJumpPossible)) {
    8.             velocity.y = _playerController.Grounded ? _jumpTakeOffSpeed : _doubleJumpTakeOffSpeed;
    9.             _timeForNextJump = Time.time + 0.05f;
    10.  
    11.             if (_playerController.Grounded) {
    12.                 _doubleJumpPossible = true;
    13.             } else {
    14.                 _doubleJumpPossible = false;
    15.             }
    16.  
    17.             _animator.SetTrigger("jump");
    18.             AudioManager.Instance.PlayOneShotSound(_heroSFX.AudioMixerGroup, _heroSFX[SFX_JUMP], transform.position, _heroSFX.Volume, _heroSFX.SpatialBlend);
    19.             ParticlesOnJump();
    20.  
    21.             _jumpKeyDown = false;
    22.         }
    23.  
    24.         if (_jumpKeyUp) {
    25.             // player is going up
    26.             if (velocity.y > 0) {
    27.                 velocity.y *= 0.5f;
    28.             }
    29.  
    30.             _jumpKeyDown = false;
    31.             _jumpKeyUp = false;
    32.         }
    33.  
    34.  
    35.         // if key for jump is still down (not processed above) and player is going up, then clear it
    36.         if (velocity.y > 0) {
    37.             _jumpKeyDown = false;
    38.         }
    39.  
    40.  
    41.         return velocity;
    42.     }
     
    Last edited: May 5, 2020