- Home /
Unity 2D -- Dash instantly stops
Hello, I've been working on a simple 2D platformer for a school project! I'm currently making a dash ability for my game, but I've encountered an issue. Whenever I try to dash, it instantly ends barely making me move forward. Can somebody help me fix this issue? I've tried to apply many methods but I couldn't figure it out. Here's the script:
[SerializeField] private float speed;
[SerializeField] private float jumpPower;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private LayerMask wallLayer;
private Rigidbody2D body;
private Animator anim;
private BoxCollider2D boxCollider;
private float wallJumpCooldown;
private float horizontalInput;
private float waitTime = 2.0f;
private float timer = 0.0f;
private float visualTime = 0.0f;
private bool canDash;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
if (horizontalInput > 0.01f)
transform.localScale = Vector3.one;
else if (horizontalInput < -0.01f)
transform.localScale = new Vector3(-1, 1, 1);
anim.SetBool("Running", horizontalInput != 0);
anim.SetBool("grounded", isGrounded());
anim.SetBool("onWall", onWall());
if (wallJumpCooldown > 0.2f)
{
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
if (Input.GetKey(KeyCode.Space))
Jump();
if (Input.GetKey(KeyCode.C))
Dash();
}
wallJumpCooldown = 3f;
if (wallJumpCooldown < 0.2f)
{
speed = 0;
}
}
private void Jump()
{
if (isGrounded())
{
body.velocity = new Vector2(body.velocity.x, jumpPower);
anim.SetTrigger("Jump");
canDash = true;
}
else if (onWall() && !isGrounded())
{
body.velocity = new Vector2(-Mathf.Sign(transform.localScale.x) * 16, 14);
wallJumpCooldown = 0.1f;
}
}
//Here is the Dash, need help here.
private void Dash()
{
if (!isGrounded() && !onWall() && canDash)
{
body.velocity = new Vector2(Mathf.Sign(transform.localScale.x) * 20, 0);
canDash = false;
}
}
private bool isGrounded()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
return raycastHit.collider != null;
}
private bool onWall()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer);
return raycastHit.collider != null;
}
I added the canDash = false; so the player can't continue dashing forever, just so you know.
Answer by MarekRimal · Dec 03, 2021 at 03:41 PM
I dont understand how your wallJumpCooldown works but my guess is that you are immediately overriding the dash by horizontalInput in the next Update at line 37.
Way to go can be for example store all the velocities to separate variables and then add them all at once to the RigidBody.
Also a tip: Use Time.deltaTime to scale the velocities applying on the rigidbody since then it will works the same on differently fast computers.