- Home /
Why doesn't my jump reset?
public Vector2 jump; public float jumpForce = 2.0f;
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;
jump = new Vector2(0.0f, 4.0f);
}
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 OnCollisionStay()
{
isGrounded = true;
}
private void FixedUpdate()
{
rb.AddForce(new Vector2(horizontal * 20, 0));
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(jump * jumpForce, 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
Best Answer
Answer by HellsHand · Apr 24, 2021 at 02:58 PM
Chances are judging by the fact your are using a RigidBody2D
you have 2D colliders. In that case you want to use OnCollisionStay2D
as opposed to OnCollisionStay
.
Your answer

Follow this Question
Related Questions
Jumps never reset 2 Answers
2D C# Jump Script Addforce 2 Answers
Touch and move in any axis 0 Answers
Enable/Disable CharMotor jumping 1 Answer
Camera follow player jumping? 2 Answers