Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Problem using any key to jump

Discussion in 'Input System' started by ViRa470, Jan 20, 2023.

  1. ViRa470

    ViRa470

    Joined:
    Jul 7, 2020
    Posts:
    2
    hello everyone!

    I have a problem. I had created a script that allows you to jump or make a dash depending on whether the player presses the space key or holds it down for a defined time.
    I wanted to make this possible not only for the space key but for any key of the keyboard and button on the controller. How can I do?

    I tried different methods (for example with the Input.anykey etc. but it doesn't work correctly). I also tried saving my keystroke to a keycode but it didn't work for the joystick but only for the keyboard. Thank you

    I attach the script for the old code that works only for the space key:

    Code (CSharp):
    1. void Update()
    2.     {
    3.  
    4.         if (Input.GetKeyDown(KeyCode.Space) && !isJumping && !isDashing &&!stopped) // Use has pressed the Space key. We don't know if they'll release or hold it, so keep track of when they started holding it.
    5.         {
    6.             spacePressedTime = Time.timeSinceLevelLoad;
    7.             spaceHeld = false;
    8.         }
    9.  
    10.  
    11.         else if (Input.GetKeyUp(KeyCode.Space) && !isJumping && !isDashing && !stopped)  // Player has released the space key without holding it. Perform the action for when Space is pressed
    12.         {  
    13.             if (!spaceHeld)
    14.             {
    15.                 if (Time.time > canJump)
    16.                 {
    17.                     isJumping = true;
    18.  
    19.                     animator.SetBool("jump", true);
    20.                     animator.SetBool("isOnGround", false);
    21.                     playerRigidBody.AddForce(Vector3.up * jumpForce, ForceMode2D.Impulse);
    22.                     playerRigidBody.gravityScale = jumpGravity; //new add
    23.                     canJump = Time.time + 1f;
    24.                 }      
    25.             }
    26.             spaceHeld = false;
    27.         }
    28.  
    29.         if (Input.GetKey(KeyCode.Space) && !isJumping && !isDashing && !stopped)
    30.         {
    31.             if (Time.timeSinceLevelLoad - spacePressedTime > minimumHeldDuration)  // Player has held the Space key for seconds. Consider it "held"
    32.             {
    33.  
    34.                     if (Time.time > canSlide)
    35.                     {                            
    36.                         isDashing = true;
    37.  
    38.      
    39.                         animator.SetBool("dash", true);
    40.                         StartCoroutine(Dash());
    41.                         spaceHeld = true;
    42.                         canSlide = Time.time + 5f;
    43.                     }
    44.                
    45.                
    46.             }
    47.         }