Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Why won't my character jump?

Discussion in '2D' started by D0rzy, Oct 16, 2020.

  1. D0rzy

    D0rzy

    Joined:
    Oct 16, 2020
    Posts:
    3
    i was working on making my character jump which I was able to accomplish but then ran into an issue because it could continue to jump. I found a youtube tutorial but for some reason it wont jump now? I followed the code from the video but nothing. Maybe there's something i'm missing or if anyone has another way of ground checking for jumps?

    Code:
    Code (CSharp):
    1.   float jumpHeight = 10f;
    2.     Rigidbody2D playerRigid;
    3.     BoxCollider2D playerCollider;
    4.     bool onGround;
    5.     RaycastHit2D raycast;
    6.  
    7.     void Start()
    8.     {
    9.         playerRigid = GetComponent<Rigidbody2D>();
    10.         playerCollider = GetComponent<BoxCollider2D>();
    11.  
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         if(ObjectGrounded() && Input.GetKeyDown("space"))
    17.         {
    18.             //This is registering
    19.             Debug.Log("Space has been hit");
    20.             //This is not
    21.             playerRigid.velocity = Vector2.up * jumpHeight;
    22.  
    23.         }
    24.         Move();
    25.        
    26.        
    27.        
    28.     }
    29. //Setting up Boxcast to check if it's grounded, returning bool.
    30.      private bool ObjectGrounded()
    31.     {
    32.         RaycastHit2D rayCast2D = Physics2D.BoxCast(playerCollider.bounds.center, playerCollider.bounds.size, 0f, Vector2.down * 0.1f);
    33.         return rayCast2D.collider != null;
    34.     }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,345
    I have a feeling it is registering but your jumpHeight isnt high enough value. This messed me up a lot when i first began since the code seems to work and the Debug.Log calls and nothing happened. Try changing jumpHeight to like 5000 or something crazy so you know it works.
     
  3. D0rzy

    D0rzy

    Joined:
    Oct 16, 2020
    Posts:
    3
    So turns out I hadn't even set a value to my jumpHeight after I serialized the variable lol. It's jumping now BUT, It is still jumping each click and isn't waiting for a ground check.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,345
    Try debugging your ObjectGrounded and see if it ever goes false.
     
  5. D0rzy

    D0rzy

    Joined:
    Oct 16, 2020
    Posts:
    3
    Seems to be only returning true, i'll have to just keep tinkering with it I guess. The issue lies somewhere in that function. Did I setup the boxcast properly?
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,345
    So i dont work with Raycasts too much anymore (I set them up a while ago for my game and havent touched since) but it looks that last line: return rayCast2D.collider != null; will return true if the collider is not null, but doesnt check to see if it is touching the ground or not. You set up a boxCast but you arent checking anywhere if it touches a Layer called Ground or a Tag called Ground.

    I may be wrong on that but i think thats it. Check out this video for how i did it (Brackeys is the bomb).