Help With Dash Mechanic.

Hi there, ill get straight to the point. I’ve been learning unity for a while and decided to start making my own. Everything was going fine unitl I decided to add a dash mechanic, similar to Megaman x dash, pushing the button for dashing works fine, but while moving it causes a jitter effect and im not sure how to fix it.

Here is the code:

void dash () {

if (Input.GetKeyDown (KeyCode.Q) && canDash == true) {
dashcoolDown = 1.0f;
if (facingRight == true) {
while (dashTime >= 0) {
if (rightMoving == true ) {

rigidbody2D.velocity += new Vector2 (rigidbody2D.velocity.x * 2 , rigidbody2D.velocity.y)* Time.deltaTime;
dashTime -= Time.deltaTime * 1;
}
else if (rightMoving == false) {
rigidbody2D.velocity = new Vector2 (500 , rigidbody2D.velocity.y)* Time.deltaTime;
dashTime -= Time.deltaTime * 1;
}

}
canDash = false;
} else if (facingRight == false) {
while (dashTime >= 0) {

if (leftMoving == true) {
rigidbody2D.velocity += new Vector2(-10, rigidbody2D.velocity.y) * Time.deltaTime;
dashTime -= Time.deltaTime * 1;

}
else if (leftMoving == false) {
rigidbody2D.velocity = new Vector2 (-500 , rigidbody2D.velocity.y) * Time.deltaTime;
dashTime -= Time.deltaTime * 1;
}

}
canDash = false;
}

}

if (canDash == false) {
while (dashcoolDown >= 0) {
dashcoolDown -= Time.deltaTime * 1;
Debug.Log(" dash cool down: " + dashcoolDown + " can dash: " + canDash);
canDash = true;
dashTime = 0.5f;

}

}
}

I think, velocity don’t need to be maked frame independent and multiplicated with time.deltatime. Velocity is moving vector and you can just set this vector to needed speed. And change interpolate to interpolate in the settings for Rigidbody2D (if not alredy done). void dash(): where are you calling it?

In the fixed update method. Thanks alot ill try doing what you said

Just a couple of notes. 1 is about posting code on the forums: You should please read the pinned thread about how to post code properly using code tags.

the second comment is about your code is “while(dashtime >= 0)”. I think this would be a bad idea in the update function. Even if you remove the part about the velocity, because you just set it once (as mentioned by the previous poster). I think you should count down the dashtime in each loop (here, I mean game loop, naturally occuring Update() - once per frame), rather than using the while loop statement.
something simple like:

if(dashing) {
   dashTime -= Time.deltaTime;
   if(dashTime <= 0) dashing = false;
 }

:slight_smile:

1 Like

Thanks a lot for your help man. I didn’t notice the pinned threat ill definitely give that a read, also, thanks for your help on the code ill give it a shot and see what happens. :slight_smile:

Cool… and to elaborate on why I thought it was a bad idea to count a timer down in a while loop like that inside update is because you’d essentially be halting the game loop cycle, the loop would spin for the time to pass, but that’s all the game would be doing. Anyways, hope you get the dash mechanic working :slight_smile: