I am currently trying to add a sort of "dash"-ability to my sidescroller.
What I try to do, is to let the player press a key where a simple key-press causes the player to move quickly into one direction by a predefined distance. During that dash, the character controller is disabled so the player is not able to cancle the dash itself. after the dash, the character controller is enabled again and the dash ability is disabled, so to say on cooldown. Though the "predefined distance" is what isn't working.
The code for it I have so far is as following. I have a script for choosing the animations and reacting to key Input, this script includes the following in it's Update method ("a" and "d" are set for the Dash). pstats is my object for the variables in the Player-script.
float dashing = Input.GetAxis ("Dash");
if (Input.GetKey ("a") && pstats.dashUsable)
{
pstats.DashLeft();
}
if (Input.GetKey ("d") && pstats.dashUsable)
{
pstats.DashRight();
}
if (!pstats.dashUsable)
{
dashing = 0.0f;
}
My Playerscript contains:
public bool dashUsable = true;
public bool dashUsing = false;
public bool attacking = false;
private IEnumerator WaitForNextDash() {
yield return new WaitForSeconds(1.0f);
dashUsable = true;
Debug.Log("WaitForNextDash Done");
}
private IEnumerator WaitForDashToEnd() {
yield return new WaitForSeconds(1.0f);
Debug.Log ("dashUsable being changed");
dashUsable = false;
dashUsing = false;
attacking = false;
controller.enabled = true;
StartCoroutine (WaitForNextDash ());
Debug.Log("WaitForDashToEnd Done");
}
// The two lines below are to create the proper facing-direction for the player
Quaternion targetf = Quaternion.Euler(0, 270, 0);
Quaternion targetb = Quaternion.Euler(0, 90, 0);
public void DashLeft()
{
attacking = true;
dashUsing = true;
controller.enabled = false;
Debug.Log ("Dash-Left-Skill in use");
transform.rotation = Quaternion.Slerp (transform.rotation, targetf, Time.deltaTime * 5000.0f);
transform.position = Vector3.MoveTowards (transform.position, Vector3.right * -100f, 20 * Time.deltaTime);
StartCoroutine (WaitForDashToEnd ());
}
public void DashRight()
{
attacking = true;
dashUsing = true;
controller.enabled = false;
Debug.Log ("Dash-Right-Skill in use");
transform.rotation = Quaternion.Slerp(transform.rotation, targetb, Time.deltaTime * 5000.0f);
transform.position = Vector3.MoveTowards (transform.position, Vector3.right * 100f, 20 * Time.deltaTime);
StartCoroutine (WaitForDashToEnd ());
}
(Sorry for the large blocks of Code, but I thought to better include both)
The problem now is that instead of one button press causing a movement by a set distance... like 100 on the x-axis, the player moves as long as the key is hold. Also, the cooldown for some reason is direction dependant, meaning, if I dash to the right, the cooldown works for the right side, though I can dash to the left directily after without cooldown.
What I am trying to get is to have the WaitForDashToEnd to have a duration that fits the duration of the dash movement itself, I can fix that up as soon as I have the actual dash working. Since the dash movement doesn't work as intented yet, the "stun" during the dash isn't fully functional yet either, but that should more or less fix itself once the dash works.
So... how can I manage to have the dash work as a movement over a predefined distance, no matter how long the key is held?
edit: on a sidenote, during dash, the player is oddly lifted a bit above the ground, is there a way to fix that too? In either case, the dash itself has priority.