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 Cant i Jump

Discussion in '2D' started by Razills, Aug 5, 2020.

  1. Razills

    Razills

    Joined:
    Aug 4, 2020
    Posts:
    4
    Hello,
    So I Tried Programming The Player To Move and Jump (2D) For Now it only worked on moving
    when I first started jumping worked but I had a bug where I can just press space and jump forever so I added a Grounded Variable and that's where it all went downhill
    Please Help[ me by telling me what's wrong with my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Platformer : MonoBehaviour
    6.  
    7. {
    8. public float speed;
    9. public float jumpForce;
    10. bool isGrounded = false;
    11. public Transform isGroundedChecker;
    12. public float checkGroundRadius;
    13. public LayerMask groundLayer;
    14.  
    15.  
    16.    Rigidbody2D rb;
    17. void Start() {
    18.     rb = GetComponent<Rigidbody2D>();
    19. }
    20.  
    21.     // Update is called once per frame
    22.    void Update()
    23. {
    24.     Move();
    25.     Jump();
    26.     CheckIfGrounded();
    27. }
    28. void Move() {
    29.     float x = Input.GetAxisRaw("Horizontal");
    30.     float moveBy = x * speed;
    31.     rb.velocity = new Vector2(moveBy, rb.velocity.y);
    32.     }
    33. void Jump() {
    34.     if (Input.GetKeyDown(KeyCode.Space) && isGrounded) {
    35.         rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    36.     }
    37. }
    38. void CheckIfGrounded() {
    39.     Collider2D collider = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);
    40.     if (collider != null) {
    41.         isGrounded = true;
    42.     } else {
    43.         isGrounded = false;
    44.     }
    45. }
    46. }
    47.  
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Debug.Log(isGrounded) at the start of your jump to see if your grounded; could be the issue.
    Maybe CheckIfGrounded should happen before Jump?
     
  3. Razills

    Razills

    Joined:
    Aug 4, 2020
    Posts:
    4
    i Didnt understand I'm new to C# can you please explain?
    Thank You
     
  4. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    If you debug when hitting space it will tell you if your isGrounded variable is working properly. If the console prints true then your player is grounded so you can eliminate that as an issue.
    Code (CSharp):
    1. void Jump()
    2. {
    3.    if (Input.GetKeyDown(KeyCode.Space))
    4.    {
    5.        Debug.Log(isGrounded); //print to console to see if you are grounded when pressing space
    6.        if(isGrounded)
    7.         {
    8.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    9.         }
    10.     }
    11.  }
     
    Last edited: Aug 5, 2020
  5. Razills

    Razills

    Joined:
    Aug 4, 2020
    Posts:
    4
    Ok This Is The New Code But After i add it, it gives me this error
    "Assets\Platformer.cs(44,39): error CS1026: ) expected"
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Platformer : MonoBehaviour
    5. {
    6. public float speed;
    7. public float jumpForce;
    8. bool isGrounded = false;
    9. public Transform isGroundedChecker;
    10. public float checkGroundRadius;
    11. public LayerMask groundLayer;
    12.    Rigidbody2D rb;
    13. void Start() {
    14.     rb = GetComponent<Rigidbody2D>();
    15. }
    16.     // Update is called once per frame
    17.    void Update()
    18. {
    19.     Move();
    20.     CheckIfGrounded();
    21.     Jump();
    22.  
    23. }
    24. void Move() {
    25.     float x = Input.GetAxisRaw("Horizontal");
    26.     float moveBy = x * speed;
    27.     rb.velocity = new Vector2(moveBy, rb.velocity.y);
    28.     }
    29. void CheckIfGrounded() {
    30.     Collider2D collider = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);
    31.     if (collider != null) {
    32.         isGrounded = true;
    33.     } else {
    34.         isGrounded = false;
    35.     }
    36. }
    37. void Jump()
    38. {
    39.    if (Input.GetKeyDown(KeyCode.Space);
    40.    {
    41.        Debug.Log(isGrounded); //print to console to see if you are grounded when pressing space
    42.        if(isGrounded);
    43.         {
    44.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    45.         }
    46.     }
    47. }
    48. }
     
  6. Razills

    Razills

    Joined:
    Aug 4, 2020
    Posts:
    4
    Sorry its
    "Assets\Platformer.cs(39,39): error CS1026: ) expected"
     
  7. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    line 39 should have another closing parenthesizes instead of semicolon.
    Line 42 should not have semicolon.