- Home /
 
Jump problem (2D)
Upon collision (instead of being able to jump straight away) there seems to be a 2 or 3 second delay until I'm able to jump, here's my code:
 float horizontal;
 float vertical;
 Rigidbody2D rb;
 IEnumerator dashCoroutine;
 public bool isGrounded;
 bool isDashing;
 bool canDash = true;
 float direction = 1;
 float normalGravity;
 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
     normalGravity = rb.gravityScale;
 }
 void Update()
 {
     if (horizontal != 0)
     {
         direction = horizontal;
     }
     horizontal = Input.GetAxisRaw("Horizontal");
     vertical = Input.GetAxis("Jump");
     if (Input.GetKeyDown(KeyCode.LeftShift))
     {
         if (dashCoroutine != null)
         {
             StopCoroutine(dashCoroutine);
         }
         dashCoroutine = Dash(.1f, 1);
         StartCoroutine(dashCoroutine);
     }
 }
 void OnCollisionStay2D()
 {
     isGrounded = true;
 }
 private void FixedUpdate()
 {
     rb.AddForce(new Vector2(horizontal * 20, 0));
     if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
     {
         rb.AddForce(new Vector2(vertical, 10), ForceMode2D.Impulse);
         isGrounded = false;
     }
     if (isDashing)
     {
         rb.AddForce(new Vector2(direction * 10, 0), ForceMode2D.Impulse);
     }
 }
 IEnumerator Dash(float dashDuration, float dashCooldown)
 {
     isDashing = true;
     canDash = false;
     rb.gravityScale = 0;
     rb.velocity = Vector2.zero;
     yield return new WaitForSeconds(dashDuration);
     isDashing = false;
     rb.gravityScale = normalGravity;
     rb.velocity = Vector2.zero;
     yield return new WaitForSeconds(dashCooldown);
     canDash = true;
 }
 
              
               Comment
              
 
               
              not sure but onCollisionStay is really heavy.. You should look into using raycasts or have it in onCollisionEnter.
Your answer