Search Unity

Jumping does not work

Discussion in '2D' started by FesterTY, Feb 21, 2019.

  1. FesterTY

    FesterTY

    Joined:
    May 18, 2018
    Posts:
    3
    Hello all, I am having a problem with jumping in my game.

    Inside Update()
    Code (CSharp):
    1. // Check to see if player is on the ground
    2.         CheckIsGrounded(groundCheck.position, checkRadius, groundLayerMask, out isGrounded);
    3.  
    4. checkForInputs(KeyCode.Space);

    Inside FixedUpdate()
    Code (CSharp):
    1.  
    2.         if (shouldBeJumping == true && isGrounded == true)
    3.         {
    4.             controller.Jump(jumpForce);
    5.         }
    6.  

    Inside CheckIsGrounded()
    Code (CSharp):
    1.  
    2.    // Created an OverlapCircle to check if player is on the ground
    3.     private void CheckIsGrounded(Vector2 _circlePosition, float _circleRadius, LayerMask _whatToCheck, out bool _isGrounded)
    4.     {
    5.         _isGrounded = Physics2D.OverlapCircle(_circlePosition, _circleRadius, _whatToCheck);
    6.     }
    7.  
    8.  

    Inside CheckForInputs()
    Code (CSharp):
    1.  
    2.     public void CheckForInputs(KeyCode _key)
    3.     {
    4.         switch (_key)
    5.         {
    6.             case KeyCode.Space:
    7.                 Debug.Log("should be jumping: set true");
    8.                 Debug.Log("character is on ground: " + isGrounded);
    9.                 shouldBeJumping = true;
    10.                 break;
    11.             default:
    12.                 Debug.Log("KeyCode not listed");
    13.                 break;
    14.         }
    15.     }
    16.  
    Inside controller.Jump()
    Code (CSharp):
    1.  
    2.     public void Jump(float _jumpForce)
    3.     {
    4.         rb2d.velocity = new Vector2(rb2d.velocity.x, _jumpForce);
    5.         Debug.Log("Jumping!");
    6.     }
    7.  
    A very straightforward process. If the player presses the spacebar, and is on the ground, jump. Currently during my testing, all the Debug.Log() works. If the player is on the ground, the console logs that it's on the ground. The Debug.Log("Jumping!") inside Jump() works, too. Problem is, the player isn't jumping.

    Is there something I have done wrong in my code? Here are all the values I set for the variables.
    settings.JPG

    Any advice are appreciated. Thank you.
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    one thing i'm noticing is you're not setting shouldBeJumping to false anywhere, this is going to be an issue. alternatively if you are setting it and didn't post the code, that's likely happening and undoing your spacebar press.

    confirm that jumpforce is actually the number you expect it to be, and that it will produce the effect you are expecting - the first with debug.log, the second with manually setting the values in inspector