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

Question Unable to Ground Check with A Bool Method

Discussion in 'Scripting' started by Swimmtrunk, Jun 25, 2021.

  1. Swimmtrunk

    Swimmtrunk

    Joined:
    May 27, 2021
    Posts:
    22
    I have been working on a 2D Jump Script and I'm running into problems involving a function to check if the player is grounded, I have the code below and my main focus is on the IsGrounded function I made at the bottom and it's inclusion in the void Update() to check if I can jump or technically "if my jump key was pressed."

    Code (CSharp):
    1. private bool jumpKeyWasPressed;
    2. private Rigidbody2D rigidbodyComponent;
    3. private BoxCollider2D groundColl;
    4.  
    5. [SerializeField] private LayerMask jumpableGround;
    6.  
    7. void Start()
    8.     {
    9.         rigidbodyComponent = GetComponent<Rigidbody2D>();
    10.         groundColl = GetComponent<BoxCollider2D>();
    11.     }
    12.  
    13. void Update()
    14.     {
    15.         if (Input.GetKeyDown(KeyCode.Space) && IsGrounded)
    16.         {
    17.             jumpKeyWasPressed = true;
    18.         }
    19.              
    20.     }
    21.  
    22.     private void FixedUpdate()
    23.     {
    24.         if (jumpKeyWasPressed)
    25.         {
    26.             rigidbodyComponent.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
    27.             jumpKeyWasPressed = false;
    28.         }
    29.  
    30.     }
    31.  
    32.     private bool IsGrounded()
    33.     {
    34.         return Physics2D.BoxCast(groundColl.bounds.center, groundColl.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    35.     }
    I have a compiler error telling me "Operator '&&' cannot be applied to operands of type 'bool' and 'method group'"

    I thought the function returns a bool value so I don't understand why it's not letting me use this in conjunction with the button press bool I already have.

    Any help is appreciated!
     
  2. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Hi error says what is wrong you setting condition to "IsGrounded" but you have no bool variable in your script so you have more options how you can fix it.

    Most simple is change "IsGrounded" to "IsGrounded()" , and then if your IsGrounded() method return true, code inside if will work.

    Something like this should work but i didnt try your IsGrounded method so make sure that it return what you need, for this check i better use OnCollisionStay Unity method.


    Code (CSharp):
    1. public class UnityForumA : MonoBehaviour
    2.     {
    3.         private Rigidbody2D rigidbodyComponent;
    4.         private BoxCollider2D groundColl;
    5.         float jumpHeight = 2;
    6.  
    7.         [SerializeField] private LayerMask jumpableGround;
    8.         void Start()
    9.         {
    10.             rigidbodyComponent = GetComponent<Rigidbody2D>();
    11.             groundColl = GetComponent<BoxCollider2D>();
    12.         }
    13.         void FixedUpdate()
    14.         {
    15.             if (Input.GetKeyDown(KeyCode.Space) && !IsGrounded())
    16.             {
    17.                 rigidbodyComponent.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
    18.             }
    19.         }
    20.         bool IsGrounded()
    21.         {
    22.             return Physics2D.BoxCast(groundColl.bounds.center, groundColl.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    23.         }
    24.     }
     
    Last edited: Jul 6, 2021
  3. Swimmtrunk

    Swimmtrunk

    Joined:
    May 27, 2021
    Posts:
    22
    Much appreciated! Works as intended =)