- Home /
Dashing stops when i let go of WASD keys
Hello, I have a dashing script that allows you to dash based on your input (I.e. W + D is forward right). Now, as i stated in my question, The dashing seems to stop whenever I let go of wasd.
Here are the codes that are important to the dash:
private void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
float targetangle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
target = Quaternion.Euler(0f, targetangle, 0f);
if (Input.GetKey(KeyCode.Q) && candash)
{
StartCoroutine(Dash(dashtime, dashcd));
}
if (isdashing)
{
dashspeed = Mathf.Lerp(dashspeed, enddashspd, dashrate);
cc.Move(_dashDirection * dashspeed * Time.deltaTime);
}
}
IEnumerator Dash(float s, float cd)
{
isdashing = true;
candash = false;
_dashDirection = target * Vector3.forward;
yield return new WaitForSeconds(s);
isdashing = false;
dashspeed = initialdashspeed;
yield return new WaitForSeconds(cd - s);
candash = true;
}
The thing i used for the player is a charactercontroller (as shown by the "cc" in the script). Im aware that the problem is that im using input.getaxis but my main problem is how can i fix/change the code to make it so that, Whenever I press the dash button, I still dash in the desired direction i wanted to go in even if i let go of the WASD keys.
Are you using any other extra code to move the character? I can't see any issue with your code
Well, yes i do have extra code that i use to move my character, I didn't add them in the code i presented because I thought it was irrelevant.
Answer by pieandseal · Jan 01 at 11:52 PM
Store direction right before the function is called and then use the stored direction instead of the current direction
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
void DashExample()
{
Vector3 startDir = direction;
// dash code (move towards startDir, not direction)
}
hope this helped
He is already storing the direction in a variable, this won't fix his issue
but he overwrites the variable every frame, so clearly it's not going to be carried over, although i don't see how @pieandseal 's solution will help
No, he is not overriding the dash direction everyframe
Sorry, I already found a solution to my problem. Apparently all i had to do was disable the input/changing of Vector3 direction via using bools. So now the Vector3 direction part code looks a bit something like this:
if (cangetdir)
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
float targetangle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
target = Quaternion.Euler(0f, targetangle, 0f);
}
When isdashing is false, I can get the dash direction i need and when i press the dash button, it sets cangetdir to false and apparently that freezes my input to the direction i was pressing before the dash.
Thank you tho