Search Unity

Question Different jump heights while using new UI button

Discussion in 'Input System' started by xDayhawk, Apr 14, 2023.

  1. xDayhawk

    xDayhawk

    Joined:
    Jun 24, 2020
    Posts:
    57
    Hi,

    I've switched a small game i'm working on to use the new input system as i want to get it ready for Mobile as well and i've started playing around with UI Buttons.
    I have movement, dash, and jump.
    I managed to get Movement and dash working perfectly but i'm having an issue with Jumping.

    The issue is very visible on this video, the Button jump seems to be very low:


    This is the code i'm using, while pressing Spacebar it works great as i have low and high jumps.
    The button has this script on it which may be wrong?

    upload_2023-4-14_10-59-46.png

    This is the New Input jump movement :

    upload_2023-4-14_11-0-17.png

    This is the code i've changed for the new input system, before and after:
    Code (CSharp):
    1. Input.GetButtonDown("Jump") --> Keyboard.current.spaceKey.wasPressedThisFrame
    2. Input.GetButtonDown("Jump") --> Keyboard.current.spaceKey.wasReleasedThisFrame
    This is the full code block of the Jump:
    Code (CSharp):
    1.         if (Keyboard.current.spaceKey.wasPressedThisFrame)
    2.         {
    3.             jumpBufferCounter = jumpBufferTime;
    4.         }
    5.         else
    6.         {
    7.             jumpBufferCounter -= Time.deltaTime;
    8.         }
    9.  
    10.         if (isTouchingGround)
    11.         {
    12.             _coyoteTimeCounter = _coyoteTime;
    13.         }
    14.         else
    15.         {
    16.             _coyoteTimeCounter -= Time.deltaTime;
    17.         }
    18.  
    19.         if (_coyoteTimeCounter > 0f && jumpBufferCounter > 0f)
    20.         {
    21.             print("BIG jump!");
    22.             soundManager.PlaySound(SoundManager.Sounds.PLAYER_JUMP);
    23.             Instantiate(jumpParticles, transform.position, Quaternion.identity);
    24.             _anim.SetBool("jumping", true);
    25.             _rb.velocity = Vector2.up * _jumpForce;
    26.             jumpBufferCounter = 0f;
    27.         }
    28.  
    29.         if (Keyboard.current.spaceKey.wasReleasedThisFrame && _rb.velocity.y > 0f)
    30.         {
    31.             print("SMALL jump!");
    32.             _rb.velocity = new Vector2(_rb.velocity.x, _rb.velocity.y * 0.5f);
    33.             _coyoteTimeCounter = 0f;
    34.         }

    Thanks,