title: (Unity, 2D, C#) How can I make the player let go of a button before needing to press it again?
body: <p>This is a weirdly phrased question...and I don't know how to explain it well. I'm making a ledge hang mechanic for my game I'm making, you press either of the directions to get onto the ledge, but you also press the directions to <em>get off of</em> the ledge. This means while you're trying to get on the ledge, he instantly falls off. Making a fixed wait time also doesn't feel good...I'm thinking that it would set the axis to 0 until the button is let go, but the problem is that if it's 0, the button is let go. It's a conundrum I can't solve.</p>
  <p>Here is the code that detects movements pressed on the left or right arrow keys.</p>
  <pre><code>mx = Input.GetAxisRaw(&quot;Horizontal&quot;);
  //Detects if the left or right arrow keys are pressed, and turns it into a number.
  </code></pre>
  <p>Here's some of the code that detects movement while you're hanging There's already a problem...there's an &quot;if hanging&quot; statement right before that's supposed to restrict movement until you let go or jump, but since moving is supposed to ALSO get you out of the hanging, it makes this point moot. I could really just use different buttons, like restricting it to just the jump button or down key (since that uses a different variable), but I want to see if a solution is possible.</p>
  <pre><code>if (IsGrabbing == true)
      {
          rb.velocity = new Vector2(0f, 0f);
          rb.gravityScale = 0f;
  // mx2 detects vertical movement. This is still a problem, if you're pressing down before you hang, it'll instantly hang instead of waiting for another press, but it's not as bad because you don't press down to hang on a ledge
          if (mx != 0 || mx2 &lt; 0)
          {
  //Just the code that makes you let go and unable to hang, this is reset when you land.
              IsGrabbing = false;
              anim.SetBool(&quot;IsHanging&quot;, false);
              redXSize = 10f;
              redYSize = 10f;
              greenXSize = 0f;
              greenYSize = 0f;
              rb.gravityScale = 5f;
          }
  </code></pre>
  <p>I've tried many things, a lot of &quot;while&quot; loops I've tried just crash the game. Does anyone know how to fix this issue? I'll provide all the code if needed.</p>
  <p>Edit: With the help of the reply, I've found the solution! There needs to be a flag set like this, where by default it's false. However, it also only checks if it's false, and the else statement means it's true, not to mention the only way to set it to true bypasses that gate anyways. This gives the result needed! Also, while &quot;while&quot; loops seem to break it, adding a condition to every check to not do it while grabbing seemed to fix it. Thanks, everyone!</p>
  <pre><code>if (IsGrabbing == true)
      {
          if (mx != 0 &amp;&amp; InitiateGrab == false)
          {
              InitiateGrab = false;
          }
          else
          {
              InitiateGrab = true;
          }
          rb.velocity = new Vector2(0f, 0f);
          rb.gravityScale = 0f;
          if (mx != 0 &amp;&amp; InitiateGrab == true || mx2 &lt; 0 &amp;&amp; InitiateGrab == true)
          {
              IsGrabbing = false;
              InitiateGrab = false;
              anim.SetBool(&quot;IsHanging&quot;, false);
              anim.SetBool(&quot;IsRising&quot;, false);
              redXSize = 10f;
              redYSize = 10f;
              greenXSize = 0f;
              greenYSize = 0f;
              rb.gravityScale = 5f;
          }
      }
  </code></pre>