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

Resolved My simple jump unintentionally works as multi jump

Discussion in '2D' started by Skelbo, Aug 2, 2021.

  1. Skelbo

    Skelbo

    Joined:
    Dec 28, 2020
    Posts:
    38
    Hi, I'm making a 2D game and I need a simple jump code. I've learnt how to do it, have done it before in 3D games, but for some reason it's bugging out in this one and I can't figure out why.
    This is the bit of my jump and movement code:

    Code (CSharp):
    1. public bool onGround;
    2.  
    3.     public void Start()
    4.     {
    5.         runSpeed = 2.5f;
    6.         jumpForce = 13f;
    7.         onGround = true;
    8.         rb = GetComponent<Rigidbody2D>();
    9.     }
    10.     private void Awake()
    11.     {
    12.         rb = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         Horizontal = Input.GetAxis("Horizontal") * runSpeed;
    19.  
    20.         rb.velocity = new Vector2(Horizontal * runSpeed, rb.velocity.y);
    21.  
    22.         jump = Input.GetButtonDown("Jump");
    23.  
    24.         if ((onGround) && (jump) == true)
    25.         {
    26.             jumpVelocity = new Vector2(0f, jumpForce);
    27.             rb.velocity += jumpVelocity;
    28.             onGround = false;
    29.         }
    30.         onGround = true;
    31.     }
    If onGround and jump are both true, then the jump action should occur. Else, onGround is set to false and no jumping should occur, even if the player presses the jump key. Yet, I can jump in the air. onGround is perma set to true for some reason. I even tried setting it to false when declaring the bool at the top.
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    You are setting onGround = true after your if statement so next FixedUpdate it will be true.
     
  3. Skelbo

    Skelbo

    Joined:
    Dec 28, 2020
    Posts:
    38
    If I remove that, it only allows me one jump per entire runtime of the game.
     
  4. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    You have to check for the ground to make it true. Once true the player will be allowed to jump, when the player does jump you can make jumping invalidated for a short period of time and/or wait for a button release before allowing another jump; to give the player time to leave the ground.
     
  5. Skelbo

    Skelbo

    Joined:
    Dec 28, 2020
    Posts:
    38
    I thought I was already doing that. If my code isn't doing that, can you point out where exactly I'm wrong? To my understanding I already implemented the logic you described.


    if ((onGround) && (jump) == true) {... //here I check for the ground to see if true


    jumpVelocity = new Vector2(0f, jumpForce);
    rb.velocity += jumpVelocity; //once ground is true, this executes the jump action


    onGround = false; // jumping is invalidated here by setting onGround to false.
    The jump action can only execute if both onGround and jump key are true.
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,452
    You arent checking if they are actually on the ground. You are just setting it via code, not checking by collision or layer if a player is on the layer or tag of Ground. As mentioned above, onGround is set in FixedUpdate so it is become true (almost) every single frame instead of only being true when the Player comes in contact with the ground.

    I recommend watching this great foundation tutorial and reworking your code. It will give you a great framework to move, jump, add animations and more custom functions to your player's movement.

     
  7. Skelbo

    Skelbo

    Joined:
    Dec 28, 2020
    Posts:
    38
    I watched that video when I first started. It helped but the jump was still awkward. I think it's because I already knew one method of implementing movement and jump and then tried to integrate parts of what he explained in the video in my already written script and it didn't work. But doing it entirely his way worked!
    Thanks for reminding me of the video.

    However, I ran into another bug (a feature that worked before I reformatted my movement and jump code). Should I open a new thread for that?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    You seem to be talking over the main point and answer above which is simply setting a field to "isGround = true" doesn't mean you're on the ground. You need to know that's true by querying the environment, typically by using physics queries to determine that.

    You cannot simply do it using logic such as the player not moving in the upward direction because this can easily be fooled such as when gravity slows the player to a standstill in the air while falling.

    Whether the jump was "awkward" or not isn't the purpose of the tutorial. It's not about telling you how you want the feel of your game to be but more of how you do the basics of determining things like touching the ground, getting the player to move.
     
  9. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,452
    If you want better feeling movement, I have another video for you. This one is really great and I use almost all of the concepts shown:

     
  10. Skelbo

    Skelbo

    Joined:
    Dec 28, 2020
    Posts:
    38
    Um, what? I'm not talking over any point or saying anything bad about the answer above or the video. I thanked the user for reminding me of the video. I said doing it exactly like the tutorial worked. What's with the random hostility?
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Sorry if you thought it was hostile and I didn't say you said anything bad; those are your words not mine.

    As I said, you didn't address the point being made above about you not actually checking if you're grounded and I go on to explain what that means.
     
  12. Skelbo

    Skelbo

    Joined:
    Dec 28, 2020
    Posts:
    38
    Don't worry, I don't need another video. I didn't say anything about the movement not being smooth after following the previous video you shared. I don't know what the above user is on about.
    I'm grateful for your answer and I expressed that.

    The only thing I can think of that might be confusing you guys is that I added extra details on top of just saying "thanks" lol. It was simply to add context about what I tried before and emphasise that following the video exactly as it is works.
     
  13. Skelbo

    Skelbo

    Joined:
    Dec 28, 2020
    Posts:
    38
    Alright, I see. No worries then, just a misunderstanding. I did understand what checking the player is grounded meant after following the video and realised part of what I was doing wrong. So yes, the video was helpful with that!
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    So you know, I am in charge of 2D physics for Unity so I'm in a good position to help you which is what I was trying to do.

    I think so yes. We get such a massive spectrum of skill levels posting here on the forums that's it's difficult know who you're talking to and to what level you should help. Your code indicated a complete misunderstanding of what grounded meant so you have to go with what you see. We don't always know!

    Anyway, good luck with your project.
     
  15. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,452
    @Skelbo I still recommend that video because of the early and late input forgiveness mechanics that are used in just about all games. I dont just recommend it, i highly recommend it :)
     
  16. Skelbo

    Skelbo

    Joined:
    Dec 28, 2020
    Posts:
    38
    Yeah I'm quite a noob at game programming. There's so much information to take in, it's hard to remember it all!
     
  17. Skelbo

    Skelbo

    Joined:
    Dec 28, 2020
    Posts:
    38
    I'll have a look at it, thanks!