title: Unity2D not checking conditions properly
body: <p>I've been asking so many Unity questions in such little time and I really appreciate all those who answer. I don't know why I always face such issues for which any of the answers I see on the net aren't able to help me out. Okay so here's my problem:</p>
  
  <pre><code>if (Input.GetKey(KeyCode.Q) &amp;&amp; (rightHit.collider != null || leftHit.collider != null) &amp;&amp; groundHit.collider == null)
  </code></pre>
  
  <p>This line of code here in the Update function checks every frame if I'm holding down the Q button on any wall. If the condition is satisfied, then</p>
  
  <pre><code>void Grapple()
      {
          if (wallGrapple) 
          {
              x = 0f;
              rb.gravityScale = 0;
              rb.velocity = new Vector2(speed * x, speed * y);
              if (Input.GetKeyDown(KeyCode.Space)) { jumpRequest = true; }
  
          }
      }
  </code></pre>
  
  <p>this function is called which allows the player to stick and slide on the wall based on Vertical axis input(which feeds the y variable). Now the problem is that if I only press Q once on a wall then it stays on the wall even though its supposed to be held down for Grapple() to be called [I've used Input.GetKey instead of Input.GetKeyDown] and I am also able to slide above the wall (like there's some sorta invisible wall) even though in the if line, the condition for detecting a wall is being checked. 
  <a href="https://i.stack.imgur.com/M4IBB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/M4IBB.png" alt="see, no wall and still moving only vertically"></a></p>
  
  <p>I've been at it for around 30-45 minutes and I'm unable to crack it. Another workaround I thought of implementing was using a coroutine but I don't know how to use as I haven't found any basic explanation of how it works and how its supposed to be used. Here's the full code,</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic; 
  using UnityEngine;
  using UnityEngine.SocialPlatforms;
  
  public class Move : MonoBehaviour
  {
      [SerializeField] private LayerMask groundMask;
      [SerializeField] private LayerMask Wall;
  
      [Header("Horizontal")]
      public float x;
      float dirX;
      public float speed;
      float initialSpeed;
      [SerializeField] [Range(0, 1)] float LerpConstant;
      public float dashForce = 100f;
      public bool canDash;
      public bool dashRequest;
      [Header("Vertical")]
      public float y;
      public bool canJump;
      public bool jumpRequest;
      public float initialJumpForce;
      public float jumpForce;
      public bool canWallJump;
      public float modifiedJumpForce;
      public float sideJumpForce;
      public bool wallGrapple;
      [Header("Other")]
      public Rigidbody2D rb;
      public float gScale = 10f;
      BoxCollider2D boxcollider;
  
  
      RaycastHit2D groundHit;
      RaycastHit2D leftHit;
      RaycastHit2D rightHit;
  
  
      // Start is called before the first frame update
      void Start()
      {
          rb = gameObject.GetComponent&lt;Rigidbody2D&gt;();
          boxcollider = gameObject.GetComponent&lt;BoxCollider2D&gt;();
          jumpForce = initialJumpForce;
          initialSpeed = speed;
          rb.gravityScale = gScale;
  
      }
  
  
      // Update is called once per frame
      void Update()
      {
          //Instant 1, 0, -1 inputs
          x = Input.GetAxisRaw("Horizontal");
          y = Input.GetAxisRaw("Vertical");
          if(x &lt; 0) 
              { dirX = -1; } 
          else if(x &gt; 0) 
              { dirX = 1; }
          if (Input.GetKeyDown(KeyCode.Space) &amp;&amp; (canJump || canWallJump)) //for jumps
              { jumpRequest = true; } 
          if (Input.GetKey(KeyCode.Q) &amp;&amp; (rightHit.collider != null || leftHit.collider != null) &amp;&amp; groundHit.collider == null) //for wall grappling
              { wallGrapple = true; } 
          if (Input.GetKeyDown(KeyCode.LeftShift) &amp;&amp; (canDash)) //for dashing
              { dashRequest = true; } 
  
          Detection();
          JumpDash();
          Grapple();
      }
      void FixedUpdate()
      {        
          Movement();
      }
  
      void Detection()
      {
          groundHit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.down, 0.1f, groundMask);
          if (groundHit.collider != null) //If player is on ground
          {
              canJump = true;
              canWallJump = false;
              canDash = false;
              jumpForce = initialJumpForce;
              sideJumpForce = 0;
              LerpConstant = 0.25f;
          }
          else if (groundHit.collider == null)//not on ground 
          {
              LerpConstant = 0.12f;
              canJump = false;
          }
  
          //Wall detection
  
          //Left wall
          leftHit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.left, 0.1f, Wall);
          if (leftHit.collider != null &amp;&amp; groundHit.collider == null) //if player on left wall and not on ground
          {
              canWallJump = true;
              jumpForce = modifiedJumpForce;
              sideJumpForce = jumpForce;
  
          }
          //Right wall
          rightHit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.right, 0.1f, Wall);
          if (rightHit.collider != null &amp;&amp; groundHit.collider == null) //if player on right wall and not on ground
          {
              canWallJump = true;
              jumpForce = modifiedJumpForce;
              sideJumpForce = -jumpForce; // negative of jump force to jump in left direction
  
  
          }
  
          if (rightHit.collider == null &amp;&amp; leftHit.collider == null) //if both walls are not detected
          {
              canWallJump = false;
          }
      }
  
      void Movement()
      {
          Vector2 move = new Vector2(x * speed, rb.velocity.y);
          rb.velocity = Vector2.Lerp(rb.velocity, move, LerpConstant);
      }
  
  
  
      void JumpDash()
      {
  
          //Jump
          if (jumpRequest)
          {
              wallGrapple = false;
              rb.gravityScale = gScale;
              rb.AddForce(new Vector2(sideJumpForce * 2.1f, jumpForce), ForceMode2D.Impulse);
              speed = initialSpeed;
              jumpRequest = false; //setting jumpRequest to false after jumping to prevent unlimited jumps
              canDash = true;
          }
  
          /*if (dashRequest)
          {
              rb.velocity = Vector2.zero;
              rb.AddForce((dashForce, 0f), ForceMode2D.Impulse);
          }*/ //remember to put this code in later
  
      }
  
      void Grapple()
      {
          if (wallGrapple) 
          {
              x = 0f;
              rb.gravityScale = 0;
              rb.velocity = new Vector2(speed * x, speed * y);
              if (Input.GetKeyDown(KeyCode.Space)) { jumpRequest = true; }
  
          }
      }
  
  }
  
  </code></pre>
  
  <p>P.S. I originally wanted to use Space itself instead of Q for the stick and slide but more errors pop up like inconsistent wall jumping so that's why I used Q.</p>
  
  <p><a href="https://streamable.com/3v6qrq" rel="nofollow noreferrer">https://streamable.com/3v6qrq</a> &lt;- Video showing the problem.</p>