dashing mechanic with lerp.

hi all, im trying to create a dash mechanic where. when i double tap keyb the dash mechanic would run.

i ran into an issue where, it is not giving me a nice result. especially towards the end of the loop. it seems like stuck for a moment before resuming to move. or sometimes seems glitchy if you would spam the dash…

is there any alt. method i could experiment to boost speed or almost like a dash in megamanX series.

is there any better way than this? and if i would like to have a left and right dash. means i have to split it into 2 func? one for postive and negative?

here is the code

#pragma strict
public var DashDist : float = 0.8f; //amount of distant of the dash.


function Start () {

}

function Update () {
	
	Test();


}

function Test(){
	
	if (Input.GetButtonDown("lazerFire")){
		Dash();
		print("Dash!");
	}
}
	

function Dash(){
	var DashPos : Vector3; 
	var t : float = 0f;
	
	DashPos.x = transform.position.x + DashDist;
	
	while (t<1){
		t += Time.deltaTime*2;
		print ("t= "+ t);
		transform.position = Vector3.Lerp(transform.position,DashPos,t);
		yield;
		}
		
	
	
	


}

You want to dash after double tap, but there is no double tap check in your code, so Dash is called after each key press. When you do ‘double tap’, Dash is called twice, causing two coroutines to run simultaneously for a while. This causes your stuck/resume issue.

There are two thing to implement:

  1. Actual check for double tap. You have to do it by yourself, based on some difference of time between key presses of the same key. For an example please check answer here.

  2. If you can start dash after previous one finished, then you require some boolean variable, to control if Dash could be executed. Example:

    var _canDash : boolean = true;

    function Update()
    {
    if (Input.GetKeyDown(KeyCode.Space) && _canDash) // no double tap check here! you need to change this
    {
    _canDash = false;
    Dash();
    }
    }

    function Dash()
    {
    // whole dash code
    _canDash = true;
    }

If, on the other hand, you have to be able to start Dash before previous one finished, then you need a different code, to stop the previous Dash coroutine before new one is started.